Name

ByteArrayInputStream

Synopsis

This is a subclass of InputStream that returns data from an array of bytes passed into one of its constructors. It is useful when you have data stored in a byte array and wish to read it as if it were coming from a file, pipe, or socket. A good example of this is provided by MIDlet RecordStores, which hold their content in byte arrays. MIDlets typically read these records by creating a ByteArrayInputStream from the content of a record and then wrap it with a DataInputStream to recover strings and Java data types without assuming the format used to store them.

This class supports the mark() and reset() operations of InputStream. Once constructed, the byte array from which this stream gets its data cannot be switched. However, because the data is not copied by the stream, the contents of the array can be modified and then re-read following a reset(). Note that such an approach is only practical if the original byte array is large enough to hold any desired modifications.

The java.io hierarchy

Figure 11-1. The java.io hierarchy

                  The java.io hierarchy
public class ByteArrayInputStream extends InputStream {
// Public Constructors
   public ByteArrayInputStream( byte[] buf);  
   public ByteArrayInputStream(byte[] buf, int offset, 
        int length);  
// Public Methods Overriding InputStream
   public int available ...

Get J2ME in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.