it.geosolutions.imageio.utilities
Class FastByteArrayWrapper

Object
  extended by FastByteArrayWrapper

public class FastByteArrayWrapper
extends Object

This class implements a byteArray wrapper to speed up working with byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Author:
Daniele Romagnoli, GeoSolutions, Giannecchini Simone, GeoSolutions

Field Summary
protected  byte[] buf
          The buffer where data is stored.
protected  int count
          The number of valid bytes in the buffer.
 
Constructor Summary
FastByteArrayWrapper()
          Default constructor
Creates a new byte array output stream.
FastByteArrayWrapper(byte[] buffer)
          Build a FastByteArrayWrapper with the inner buffer specified as argument
FastByteArrayWrapper(byte[] buffer, int count)
          Build a FastByteArrayWrapper with the inner buffer specified as argument.
FastByteArrayWrapper(int size)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 byte[] getByteArray()
          Creates a newly allocated byte array.
 void reset()
          Resets the count field of this byte array wrapper to zero, so that all currently accumulated values in the byte array is discarded.
 int size()
          Returns the current size of the buffer.
 String toString()
          Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array starting at offset 0 to this byte array wrapper.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this byte array wrapper.
 void write(int b)
          Writes the specified byte to this byte array wrapper.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

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


count

protected int count
The number of valid bytes in the buffer.

Constructor Detail

FastByteArrayWrapper

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


FastByteArrayWrapper

public FastByteArrayWrapper(byte[] buffer)
Build a FastByteArrayWrapper with the inner buffer specified as argument

Parameters:
buffer - the inner buffer used by this FastByteArrayWrapper

FastByteArrayWrapper

public FastByteArrayWrapper(byte[] buffer,
                            int count)
Build a FastByteArrayWrapper with the inner buffer specified as argument.

Parameters:
buffer - the inner buffer used by this FastByteArrayWrapper
count - the number of valid bytes in the provided buffer

FastByteArrayWrapper

public FastByteArrayWrapper(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.
Method Detail

write

public void write(int b)
Writes the specified byte to this byte array wrapper.

Parameters:
b - the byte to be written.

write

public void write(byte[] b,
                  int off,
                  int len)
Writes len bytes from the specified byte array starting at offset off to this byte array wrapper.

Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

write

public void write(byte[] b)
           throws IOException
Writes b.length bytes from the specified byte array starting at offset 0 to this byte array wrapper.

Parameters:
b - the data.
Throws:
IOException

reset

public void reset()
Resets the count field of this byte array wrapper to zero, so that all currently accumulated values in the byte array is discarded.


getByteArray

public byte[] getByteArray()
Creates a newly allocated byte array. Its size is the current size of this byte array wrapper and the valid contents of the buffer have been copied into it.

Returns:
the current contents of this output stream, as a byte array.
See Also:
ByteArrayOutputStream.size()

size

public int size()
Returns the current size of the buffer.

Returns:
the value of the count field, which is the number of valid bytes in the buffer.
See Also:
ByteArrayOutputStream.count

toString

public String toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

Overrides:
toString in class Object
Returns:
String translated from the buffer's contents.


Copyright © 2006-2010 GeoSolutions. All Rights Reserved.