com.sun.xml.ws.util
Class ByteArrayBuffer

java.lang.Object
  extended by java.io.OutputStream
      extended by com.sun.xml.ws.util.ByteArrayBuffer
All Implemented Interfaces:
Closeable, Flushable

public class ByteArrayBuffer
extends OutputStream

Read/write buffer that stores a sequence of bytes.

It works in a way similar to ByteArrayOutputStream but this class works better in the following ways:

  1. no synchronization
  2. offers a newInputStream() that creates a new InputStream that won't cause buffer reallocation.
  3. less parameter correctness checking
  4. offers a write(InputStream) method that reads the entirety of the given InputStream without using a temporary buffer.


Field Summary
protected  byte[] buf
          The buffer where data is stored.
 
Constructor Summary
ByteArrayBuffer()
          Creates a new byte array output stream.
ByteArrayBuffer(byte[] data)
           
ByteArrayBuffer(byte[] data, int length)
           
ByteArrayBuffer(int size)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 void close()
           
 byte[] getRawData()
          Gets the underlying buffer that this ByteArrayBuffer uses.
 InputStream newInputStream()
          Creates a new InputStream that reads from this buffer.
 InputStream newInputStream(int start, int length)
          Creates a new InputStream that reads a part of this bfufer.
 void reset()
           
 int size()
           
 byte[] toByteArray()
          Deprecated. this method causes a buffer reallocation. Use it only when you have to.
 String toString()
          Decodes the contents of this buffer by the default encoding and returns it as a string.
 void write(byte[] b, int off, int len)
           
 void write(InputStream in)
          Reads all the data of the given InputStream and appends them into this buffer.
 void write(int b)
           
 void writeTo(OutputStream out)
           
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected byte[] buf
The buffer where data is stored.

Constructor Detail

ByteArrayBuffer

public ByteArrayBuffer()
Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.


ByteArrayBuffer

public ByteArrayBuffer(int size)
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.

ByteArrayBuffer

public ByteArrayBuffer(byte[] data)

ByteArrayBuffer

public ByteArrayBuffer(byte[] data,
                       int length)
Method Detail

write

public final void write(InputStream in)
                 throws IOException
Reads all the data of the given InputStream and appends them into this buffer.

Throws:
IOException - if the read operation fails with an IOException.

write

public final void write(int b)
Specified by:
write in class OutputStream

write

public final void write(byte[] b,
                        int off,
                        int len)
Overrides:
write in class OutputStream

writeTo

public final void writeTo(OutputStream out)
                   throws IOException
Throws:
IOException

reset

public final void reset()

toByteArray

public final byte[] toByteArray()
Deprecated. this method causes a buffer reallocation. Use it only when you have to.

Gets the copy of exact-size byte[] that represents the written data.

Since this method needs to allocate a new byte[], this method will be costly.


size

public final int size()

getRawData

public final byte[] getRawData()
Gets the underlying buffer that this ByteArrayBuffer uses. It's never small than its size(). Use with caution.


close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

newInputStream

public final InputStream newInputStream()
Creates a new InputStream that reads from this buffer.


newInputStream

public final InputStream newInputStream(int start,
                                        int length)
Creates a new InputStream that reads a part of this bfufer.


toString

public String toString()
Decodes the contents of this buffer by the default encoding and returns it as a string.

Meant to aid debugging, but no more.

Overrides:
toString in class Object


Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.