Class ByteBufferOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.DataOutput, java.io.Flushable, java.lang.AutoCloseable

    public class ByteBufferOutputStream
    extends java.io.OutputStream
    implements java.io.DataOutput
    This is an OutputStream implementation which places the output into a java.nio.ByteBuffer. The constructor accepts an initial buffer and an instance of ByteBufferSink. When an output operation would cause the buffer to overflow, it is handed to the sink's acceptBuffer() method, which is expected to dispose of the existing contents and return a buffer in which writing can continue. Note that the stream contains no state other than the buffer itself, so the buffer and/or the sink can be switched at any time by calling setBuffer() or setSink().
    • Field Detail

      • buf

        protected volatile java.nio.ByteBuffer buf
    • Constructor Detail

      • ByteBufferOutputStream

        public ByteBufferOutputStream​(java.nio.ByteBuffer b,
                                      ByteBufferSink sink)
        Returns a new ByteBufferOutputStream with the specified initial buffer and sink.
        Parameters:
        b - The initial buffer
        sink - The sink that should be used to accept full buffers
    • Method Detail

      • setBuffer

        public void setBuffer​(java.nio.ByteBuffer b)
        Install a new buffer for all future writes.
      • setSink

        public void setSink​(ByteBufferSink sink)
        Install a new sink for all future writes.
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] ba)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] ba,
                          int off,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(java.nio.ByteBuffer b)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBoolean

        public void writeBoolean​(boolean v)
                          throws java.io.IOException
        Specified by:
        writeBoolean in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeByte

        public void writeByte​(int v)
                       throws java.io.IOException
        Specified by:
        writeByte in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeShort

        public void writeShort​(int v)
                        throws java.io.IOException
        Specified by:
        writeShort in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeChar

        public void writeChar​(int v)
                       throws java.io.IOException
        Specified by:
        writeChar in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeInt

        public void writeInt​(int v)
                      throws java.io.IOException
        Specified by:
        writeInt in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeLong

        public void writeLong​(long v)
                       throws java.io.IOException
        Specified by:
        writeLong in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeFloat

        public void writeFloat​(float f)
                        throws java.io.IOException
        Specified by:
        writeFloat in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeDouble

        public void writeDouble​(double d)
                         throws java.io.IOException
        Specified by:
        writeDouble in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeBytes

        public void writeBytes​(java.lang.String s)
                        throws java.io.IOException
        Specified by:
        writeBytes in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeChars

        public void writeChars​(java.lang.String s)
                        throws java.io.IOException
        Specified by:
        writeChars in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeUTF

        public void writeUTF​(java.lang.String str)
                      throws java.io.IOException
        Specified by:
        writeUTF in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeUTF

        public void writeUTF​(java.lang.CharSequence str)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • appendByteBuffer

        public ByteBufferOutputStream appendByteBuffer​(java.nio.ByteBuffer bb)
                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • appendByte

        public ByteBufferOutputStream appendByte​(byte n)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • appendShort

        public ByteBufferOutputStream appendShort​(short n)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • appendInt

        public ByteBufferOutputStream appendInt​(int n)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • appendLong

        public ByteBufferOutputStream appendLong​(long n)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • appendDouble

        public ByteBufferOutputStream appendDouble​(double p)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • appendChars

        public ByteBufferOutputStream appendChars​(java.lang.CharSequence s)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • appendChars

        public ByteBufferOutputStream appendChars​(java.lang.CharSequence s,
                                                  int position,
                                                  int len)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • appendBytes

        public ByteBufferOutputStream appendBytes​(java.lang.CharSequence s)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • appendBytes

        public ByteBufferOutputStream appendBytes​(java.lang.CharSequence s,
                                                  int position,
                                                  int len)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • getBuffer

        public java.nio.ByteBuffer getBuffer()