Filter Streams   «Prev  Next»

Lesson 12Treating arrays as streams
ObjectiveExplore how to fill a byte Array from a File.

Explore how to fill a byte Array from a File

Sometimes it is useful to be able to write streaming data into a byte array. For example, if you are doing a lot of random access reads and writes to a relatively small file during a program, it would be much faster and simpler to read the entire contents of the file into a byte array, perform all your manipulations on the byte array, then write the byte array back out into the file when the program is done.
The ByteArrayOutputStream class allows you to send the bytes from an output stream into the successive components of a byte array.
The ByteArrayOutputStream class has two constructors, plus the usual write(), close(), and flush() methods. It also has a toByteArray() method that returns a byte array containing the data which was written onto the stream.

public synchronized byte[] toByteArray()

There are also toString() methods that convert the bytes into a string.
One way to convert a number of doubles to bytes is to chain a DataOutputStream to a ByteArrayOutputStream and write the doubles into the byte array:
ByteArrayOutputStream is an output stream that writes to a byte array.
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
DataOutputStream dos = new DataOutputStream(bos);
for (int r = 1; r <= 1024; r++) {
  dos.writeDouble(r * 2.0 * Math.PI);
byte[] theDoubles = bos.toByteArray();


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

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 numBytes. 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:
class ByteArrayOutputStreamDemo {
 public static void main(String args[]) throws IOException {
  ByteArrayOutputStream f = new ByteArrayOutputStream();
  String s = "This should end up in the array";
  byte buf[] = s.getBytes();
  System.out.println("Buffer as a string");
  System.out.println("Into array");
  byte b[] = f.toByteArray();
  for (int i=0; i*lt;b.length; i++) {
   System.out.print((char) b[i]);
  System.out.println("\nTo an OutputStream()");
  OutputStream f2 = new FileOutputStream("test.txt");
  System.out.println("Doing a reset");
  for (int i=0; i<3; i++)

When you run the program, you will create the following output. Notice how after the call to reset( ), the three X’s end up at the beginning.
Buffer as a string
This should end up in the array
Into array
This should end up in the array
To an OutputStream()
Doing a reset

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

Connecting Streams - Quiz

Click the Quiz link below to take a brief multiple-choice quiz on portable formats, connecting streams, and treating arrays as streams.
Connecting Streams - Quiz