Friday 26 August 2016

Java Tutorial : Java IO (ByteArrayInputStream Copy to destArray)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=ujGc_2bB_Ec&list=UUhwKlOVR041tngjerWxVccw

ByteArrayInputStreamDemo.java
import java.io.ByteArrayInputStream;
import java.io.IOException;

/*
 * public int read(byte[] b, int off, int len)
 * 
 * b - the buffer into which the data is read.
 * 
 * off - the start offset in the destination
 * array b 
 * 
 * len - the maximum number of bytes
 * read.
 * 
 * Reads up to len bytes of data into an array
 * of bytes from this input stream.
 */
public class ByteArrayInputStreamDemo
{

    public static void main(String[] args) throws IOException
    {
        ByteArrayInputStream byteArrayInputStream = null;

        try
        {
            byte[] srcBuffer = new byte[10];

            int j = 11;
            for (int i = 0; i < srcBuffer.length; i++)
            {
                srcBuffer[i] = (byte) j++;
            }

            System.out.println("All elements form srcBuffer:");
            for (int i = 0; i < srcBuffer.length; i++)
            {
                System.out.print(srcBuffer[i] + " ");
            }

            byteArrayInputStream = new ByteArrayInputStream(srcBuffer);

            byte[] destBuffer = new byte[6];

            /*
             * We put 4 first elements of the
             * ByteArrayInputStream instance
             * 'byteArrayInputStream' to the destBuffer
             * array, starting at the position with index 2.
             * This is why the two first indexes will be
             * '0'.
             */
            byteArrayInputStream.read(destBuffer, 2, 4);

            System.out.println("\n\nAll elements form destBuffer:");
            for (int i = 0; i < destBuffer.length; i++)
            {
                System.out.print(destBuffer[i] + " ");
            }

        }
        finally
        {
            if (byteArrayInputStream != null)
            {
                /*
                 * Closing a ByteArrayInputStream has no
                 * effect. The methods in this class can be
                 * called after the stream has been closed
                 * without generating an IOException.
                 */
                byteArrayInputStream.close();
            }
        }

    }
}
Output
All elements form srcBuffer:
11 12 13 14 15 16 17 18 19 20 

All elements form destBuffer:
0 0 11 12 13 14 
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/94d38fdd49b2d33c14c088526d40f902c21a1e0d/BasicJava/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment