Class ByteArrayByteWriter

java.lang.Object
com.adobe.internal.io.ByteArrayByteWriter
All Implemented Interfaces:
ByteReader, ByteWriter

public final class ByteArrayByteWriter extends Object implements ByteWriter
This class implements a ByteWriter in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved by using toByteReader() and toByteArray(). This class uses an array of byte buffers which are ascending powers of two in size. They are added automatically as they are needed. The array starts with TWO buffers of size mMinBufSize, then one each of buffers sized in ascending powers of two until mMaxBufSize is reached. There may then be an arbitrary number of buffers sized mMaxBufSize. Starting with TWO buffers of size mMinBufSize may seem odd, but it has the nice quality of making it easy to compute the buffer array index from ONLY the high order bit of the position address. This class is not threadsafe. It is not safe to pass an instance of this class to multiple threads. It is not safe to pass an instance of this class to multiple users even if in the same thread. It is not safe to give the same byte buffers to multiple instances of this class.
  • Field Summary

    Fields inherited from interface com.adobe.internal.io.ByteReader

    EOF
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new ByteArrayByteWriter with a default sized backing byte array.
    ByteArrayByteWriter(byte[] buffer)
    Create a new ByteArrayByteReader with the given byte array.
    ByteArrayByteWriter(byte[] buffer, int offset, int length)
    Create a new ByteArrayByteWriter with a backing byte array given.
    Create a new ByteArrayByteWriter with a backing byte array of the size given.
    ByteArrayByteWriter(ArrayList bufferArray, int length)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this ByteReader and releases any system resources associated with this stream.
    void
    Flushes this ByteWriter and forces any unwritten buffered output bytes to be written out to the underlying repository.
    long
    Returns the number of total bytes in the repository that are visible through this API.
    int
    read(long position)
    Read a single byte from the underlying bytes at the given position.
    int
    read(long position, byte[] b, int offset, int length)
    Transfers bytes from the underlying repository into the given destination array.
    byte[]
    Copy out the backing store to a single byte array.
     
    void
    write(long position, byte[] b, int offset, int length)
    Write an array of bytes at the position given.
    void
    write(long position, int b)
    Write the byte given at the position given.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ByteArrayByteWriter

      public ByteArrayByteWriter()
      Create a new ByteArrayByteWriter with a default sized backing byte array.
    • ByteArrayByteWriter

      public ByteArrayByteWriter(byte[] buffer)
      Create a new ByteArrayByteReader with the given byte array. The buffer is not copied.
      Parameters:
      buffer - the byte array to use.
    • ByteArrayByteWriter

      public ByteArrayByteWriter(int size)
      Create a new ByteArrayByteWriter with a backing byte array of the size given.
      Parameters:
      size - the initial buffer size in bytes.
    • ByteArrayByteWriter

      public ByteArrayByteWriter(byte[] buffer, int offset, int length)
      Create a new ByteArrayByteWriter with a backing byte array given.
      Parameters:
      buffer -
      offset -
      length -
    • ByteArrayByteWriter

      public ByteArrayByteWriter(ArrayList bufferArray, int length)
      Parameters:
      bufferArray -
      length -
  • Method Details

    • toByteArray

      public byte[] toByteArray()
      Copy out the backing store to a single byte array. This requires us to allocate a single byte array equal in size to the sum of all the component arrays. Clients are warned that if the total size is very large, they may get an OutOfMemory exception by doing this.
      Returns:
      the byte array copy.
    • write

      public void write(long position, int b) throws IOException
      Description copied from interface: ByteWriter
      Write the byte given at the position given. If during the write operation the byte to be written is beyond the end of the virtual array visible through this interface then the length of that virtual array becomes the position of the byte written plus one (because of zero based counting). length of virtual array = max(length of virtual array, position of byte written + 1)
      Specified by:
      write in interface ByteWriter
      Parameters:
      position - the zero-based offset within the byte array.
      b - the byte to write.
      Throws:
      IOException - if an error occurs during the write operation
      See Also:
    • write

      public void write(long position, byte[] b, int offset, int length) throws IOException
      Description copied from interface: ByteWriter
      Write an array of bytes at the position given. If during the write operation any of the bytes are to be written beyond the end of the virtual array visible through this interface then the length of that virtual array becomes the position of the byte written plus one (because of zero based counting). for each byte written : length of virtual array = max(length of virtual array, position of byte written + 1)
      Specified by:
      write in interface ByteWriter
      Parameters:
      position - the zero-based offset within the byte array.
      b - the array of bytes to write from.
      offset - the offset within the byte array to start writing from.
      length - the number of bytes to write from the byte array.
      Throws:
      IOException - if an error occurs during the write operation
      See Also:
    • length

      public long length() throws IOException
      Description copied from interface: ByteReader
      Returns the number of total bytes in the repository that are visible through this API.
      Specified by:
      length in interface ByteReader
      Returns:
      the number of bytes in the repository.
      Throws:
      IOException - if an error occurs while trying to determine the length
      See Also:
    • flush

      public void flush() throws IOException
      Description copied from interface: ByteWriter
      Flushes this ByteWriter and forces any unwritten buffered output bytes to be written out to the underlying repository.
      Specified by:
      flush in interface ByteWriter
      Throws:
      IOException - if an error occurs while trying to flush the buffer
      See Also:
    • close

      public void close() throws IOException
      Description copied from interface: ByteReader
      Closes this ByteReader and releases any system resources associated with this stream.
      Specified by:
      close in interface ByteReader
      Throws:
      IOException - if an error occurs while trying to close
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also:
    • read

      public int read(long position) throws IOException
      Description copied from interface: ByteReader
      Read a single byte from the underlying bytes at the given position.
      Specified by:
      read in interface ByteReader
      Parameters:
      position - the position to read the byte from.
      Returns:
      the byte at the postion or EOF if the position is outside of the legal range.
      Throws:
      IOException - if an error occurs during the read operation
      See Also:
    • read

      public int read(long position, byte[] b, int offset, int length) throws IOException
      Description copied from interface: ByteReader
      Transfers bytes from the underlying repository into the given destination array. Copy up to length bytes from the repository starting at the postion given into the given array starting at the given offset.
      Specified by:
      read in interface ByteReader
      Parameters:
      position - the position to read the byte from.
      b - the array to write the bytes into.
      offset - the offset in the array at which the first byte is written.
      length - the maximum number of bytes to write into the array.
      Returns:
      the number of bytes actually written to the array.
      Throws:
      IOException - if an error occurs during the read operation
      See Also: