ByteArrayInputStream and ByteArrayOutputStream classes and its methods with examples

ByteArrayInputStream class:
  • Syntax: 
         java.lang.Object
                 java.io.InputStream
                         java.io.ByteArrayInputStream
  • The Byte Array InputStream is composed of three words.
    • The Byte means a collection of 8 bits.
    • An Array means a collection of the same data type.
      • The ByteArray means an array of bytes.
    • The InputStream is a class to read from file. 
  • So, it can be used to read byte array as an input stream.
  • A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. 
  • An internal counter keeps track of the next byte to be supplied by the read method.
  • Closing a ByteArrayInputStream has no effect. 
  • The methods in this class can be called after the stream has been closed without generating an IOException.
  • It has four fields, two constructors, and seven methods.
  • Field: 
    • 1. buf:
      • Syntax:protected byte[] buf
      • Use:
        • An array of bytes that was provided by the creator of the stream. 
        • Elements buf[0] through buf[count-1] are the only bytes that can ever be read from the stream; element buf[pos] is the next byte to be read.
    • 2. pos:
      • Syntax: protected int pos
      • Use: 
        • The index of the next character to read from the input stream buffer. 
        • This value should always be nonnegative and not larger than the value of count. 
        • The next byte to be read from the input stream buffer will be buf[pos].
    • 3. mark:
      • Syntax: protected int mark
      • Use:
        • The currently marked position in the stream. 
        • ByteArrayInputStream objects are marked at position zero by default when constructed. 
        • They may be marked at another position within the buffer by the mark() method. 
        • The current buffer position is set to this point by the reset() method.
    • 4. count:
      • Syntax: protected int count
      • Use:
        • The index one greater than the last valid character in the input stream buffer.
        • This value should always be nonnegative and not larger than the length of buf. 
        • It is one greater than the position of the last byte within buf that can ever be read from the input stream buffer.
  • Constructor: 
    • Syntax:
      • public ByteArrayInputStream(byte[] buf)
      • public ByteArrayInputStream(byte[] buf, int offset, int length)
    • Use:
      • Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied. The initial value of pos is 0 and the initial value of count is the length of buf.
      • The initial value of pos is offset and the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied. The buffer's mark is set to the specified offset.
    • Parameters:
      • buf - the input buffer.
      • offset - the offset in the buffer of the first byte to read.
      • length - the maximum number of bytes to read from the buffer.
  • Methods:
ByteArrayOutputStream class:
  • Syntax: 
         java.lang.Object
                 java.io.OutputStream
                         java.io.ByteArrayOutputStream
  • The Byte Array OutputStream is composed of three words.
    • The Byte means a collection of 8 bits.
    • An Array means a collection of the same data type.
      • The ByteArray means an array of bytes.
    • The OutputStream is a class to write into file. 
  • This class implements an output stream in which the data is written into a byte array. 
  • The buffer automatically grows as data is written to it. 
  • The data can be retrieved using toByteArray() and toString().
  • It has two fields, two constructors, and seven methods.
    • The four methods derived from the OutputStreamReader class.
    • The two methods derived from the Writer class.
    • The nine methods derived from the Object class.
  • Field: 
    • 1. buf: protected byte[] buf
      • Use: The buffer where data is stored.
    • 2. count: protected int count
      • Use: The number of valid bytes in the buffer.
  • Constructor: 
    • Syntax:
      • public ByteArrayOutputStream()
      • public ByteArrayOutputStream(int size)
    • Use:
      • Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
      • Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
    • Parameters: size - the initial size.
    • Throws: IllegalArgumentException - if size is negative.
  • Methods:
    • 1. close
    • 2. reset
      • Syntax: public void reset()
      • Use: 
        • Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. 
        • The output stream can be used again, reusing the already allocated buffer space.
    • 3. size
      • Syntax: public int size()
      • Use: Returns the current size of the buffer.
      • Returns:
        • The value of the count field, which is the number of valid bytes in this output stream.
    • 4. toByteArray
      • Syntax: public byte[] toByteArray()
      • Use: 
        • Creates a newly allocated byte array. 
        • Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
      • Returns:
        • The current contents of this output stream, as a byte array.
    • 5. toString
      • Syntax: public String toString()
      • Use:
        • Converts the buffer's contents into a string decoding bytes using the platform's default character set. 
        • The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer.
      • Returns:
        • String decoded from the buffer's contents.
    • 6. write
    • 7. writeTo
      • Syntax: public void writeTo(OutputStream out) throws IOException
      • Use:
        • Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
      • Parameters:
        • out - the output stream to which to write the data.
      • Throws:
        • IOException - if an I/O error occurs.
Example (Read Byte Array):
  • Step 01: Write a JAVA program.
  • Step 02: Enjoy the Output.
Example (Write Byte Array):
  • Step 01: Write a JAVA program.
  • Step 02: Enjoy the Output.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form