dev@fi.java.net

AccessibleByteBufferOutputStream

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 18 Feb 2005 17:59:04 +0100

Hi,

I have just committed what i think is a suitable class for encoding
algorithm data for use with the:

    EncodingAlgorithmInterface.encodeToOutputStream.

This allows for reuse of an underlying buffer:

public void encodeToOutputStream(Object data,
                AccessibleByteBufferOutputStream s) {

     do {
        byte[] b = buffer.getBuffer(1024)
        // write X bytes <= 1024 encoded from data
        buffer.commit(X);
     } while(...);

}

For the cases where it is known what the length will be we can have an
additional method:

        toCommit(int length)

which must be called before any of the other methods and the length must
equal the total sum of lengths committed after the encoding has completed.

Alan, is this of any use to you for the X3D algorithms?

Paul.



public abstract class AccessibleByteBufferOutputStream extends
OutputStream {

     /**
      * Get the byte buffer.
      *
      * <p>The application shall modify the byte
      * array within the the range (getStart(),
      * getStart() + getLength()].<p>
      *
      * @return The underlying byte array to write to
      */
     public abstract byte[] getBuffer();

     /**
      * Get the byte buffer.
      *
      * <p>The application shall modify the byte
      * array within the the range (getStart(),
      * getStart() + getLength()].<p>
      *
      * @param length The length of bytes of the byte array that need to
be modified
      * @return The byte array. Null is returned if the length
      * requested is too large.
      */
     public abstract byte[] getBuffer(int length);

     /**
      * Commit bytes to the byte buffer.
      *
      * <p>The bytes committed shall be in the range (getStart(),
getStart() + length].<p>
      *
      * <p>If the method completed without error then getStart() after
the call will be
      * equal to getStart() + length before the call, and getLength()
after the call will
      * be equal to getLength() - length before the call.<p>
      *
      * <p>The length shall be > 0 and <= getLength().<p>
      *
      * @param length The length of bytes in the byte array to commit.
      */
     public abstract void commitBytes(int length);

     /**
      * Get the start position to modify bytes in the byte buffer.
      *
      * @return The start position in the byte array.
      */
     public abstract int getStart();

     /**
      * Get the length of bytes that can be modified in the byte buffer.
      *
      * @return The length of bytes in the byte array.
      */
     public abstract int getLength();
}
-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109