ByteArrayinputStream Java Help


ByteArrayinputStream is an implementation of an input stream that USes a byte array as the source. This class has two constructors, each of which requires a byte array to provide the data source:

ByteArraylnputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int null mBytes)

Here, array is the input source. The second constructor creates an InputStream from a subset of your byte array.that begins with the character at the index specified by start and is mmr Bytes long.

The following example creates a pair of ByteArrayinputStream, initializing them with the byte representation of the alphabet:

A ByteArrayinputStream implements both mark( ) and reset( J. However, if mark( ) has not been called, then reset( ) sets the stream pointer to the start of the stream-which in this case is the start of the byte array passed to the constructor. The next example shows how to use the reset( ) method to read the same input twice, Iri this case, we read an9- print the letters “abc” once in lowercase and then aghast in uppercase.

This example first reads each character from the stream and prints it as is, in lowercase. It then resets the stream and begins reading again, this time converting each character to uppercase before printing. Here’s the output:


ByteArrayOutputStream\ is an implementation of an output stream that uses a byte array as the destination. ByteArrayOutputStream has two. constructors, shown here:

ByteArrayOutputStream cram(in t uum Bytes)

In the first form, a buffer of 32 bytes is created. In the second a buffer is created with a size equal to that specified by 1tl mBytes. The buffer is held in the protected buf field of ByteArrayOutputStrEam. The buffer size will be increased automatically, If needed. The number of bytes held by the buffer is contained in the protected count field of ByteArrayOutputStream.

The following example demonstrates ByteArrayOutputStream

This example uses the writeTo() convenience.method to write the contents of f to test.txt. Examining the contents ofthe test.txt file created in the preceding example shows the result we expected.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in Input/output: Exploring

Share the Story

About the Author

Back to Top