net.rim.device.api.io
Class NoCopyByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by net.rim.device.api.io.NoCopyByteArrayOutputStream

public class NoCopyByteArrayOutputStream
extends ByteArrayOutputStream

Byte array output stream that returns handle to the underlying data buffer, not a copy.

This class acts exactly like a standard byte array output stream, with two exceptions:


Sample code

Example 1:

NoCopyByteArrayOutputStream bytesOut = new NoCopyByteArrayOutputStream(bytes, 0);
DataOutputStream dout = new DataOutputStream(bytesOut);

Example 2:

NoCopyByteArrayOutputStream out = (NoCopyByteArrayOutputStream) outputStream;     
byte[] data = out.getByteArray();
int dataLength = out.size();
outputStream2.write( data, 0, dataLength );
outputStream2.close();


Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
NoCopyByteArrayOutputStream()
          Creates a new NoCopyByteArrayOutputStream instance.
NoCopyByteArrayOutputStream(byte[] buffer, int offset)
          Creates a new NoCopyByteArrayOutputStream instance built from buffer.
NoCopyByteArrayOutputStream(int size)
          Creates a new NoCopyByteArrayOutputStream instance of initial size.
 
Method Summary
 byte[] getByteArray()
          Retrieves the underlying byte array used as this stream's buffer.
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, write, write
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 



Constructor Detail

NoCopyByteArrayOutputStream

public NoCopyByteArrayOutputStream()
Creates a new NoCopyByteArrayOutputStream instance.

Builds a new byte array output stream with an initial buffer size of 32 bytes.


NoCopyByteArrayOutputStream

public NoCopyByteArrayOutputStream(int size)
Creates a new NoCopyByteArrayOutputStream instance of initial size.

Parameters:
size - Initial size for the buffer.

NoCopyByteArrayOutputStream

public NoCopyByteArrayOutputStream(byte[] buffer,
                                   int offset)
Creates a new NoCopyByteArrayOutputStream instance built from buffer.

Parameters:
buffer - Byte array to share as output stream buffer.
offset - Initial write position.


Method Detail

getByteArray

public byte[] getByteArray()
Retrieves the underlying byte array used as this stream's buffer.

Unlike ByteArrayOutputStream.toByteArray(), this method does not create a copy of the buffer. The returned byte array will probably be bigger than the actual size of the output stream. Use ByteArrayOutputStream.size() to get the actual size of the stream.

Returns:
This stream's current data buffer.





Copyright 1999-2011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Java is a trademark of Oracle America Inc. in the US and other countries.
Legal