Package net.officefloor.server.stream
Class StreamBuffer<B>
- java.lang.Object
-
- net.officefloor.server.stream.StreamBuffer<B>
-
- Type Parameters:
B
- Type of buffer.
- Direct Known Subclasses:
AbstractStreamBufferPool.FileStreamBuffer
,AbstractStreamBufferPool.UnpooledStreamBuffer
,NettyBufferPool
public abstract class StreamBuffer<B> extends java.lang.Object
Buffer that is part of a stream.- Author:
- Daniel Sagenschneider
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StreamBuffer.FileBuffer
FileChannel
content buffer.
-
Field Summary
Fields Modifier and Type Field Description StreamBuffer.FileBuffer
fileBuffer
Obtains theStreamBuffer.FileBuffer
.StreamBuffer<B>
next
NextStreamBuffer
in the stream.B
pooledBuffer
Obtains the pooled buffer.java.nio.ByteBuffer
unpooledByteBuffer
Obtains the non-pooledByteBuffer
.
-
Constructor Summary
Constructors Constructor Description StreamBuffer(B pooledBuffer, java.nio.ByteBuffer unpooledByteBuffer, StreamBuffer.FileBuffer fileBuffer)
Instantiate.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <B> java.lang.Appendable
getAppendable(StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Obtains anAppendable
to write to theStreamBuffer
stream.static <B> StreamBuffer<B>
getWriteStreamBuffer(StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Obtains theStreamBuffer
to use for writing.abstract void
release()
Releases thisStreamBuffer
for re-use.abstract boolean
write(byte datum)
Writes a byte to the pooled buffer.int
write(byte[] data)
Writes all the data to the pooled buffer.abstract int
write(byte[] data, int offset, int length)
Writes the data to the pooled buffer.static <B> void
write(byte[] bytes, int offset, int length, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes the bytes to theStreamBuffer
stream.static <B> void
write(byte[] bytes, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes all the bytes to theStreamBuffer
stream.static <B> void
write(long value, StreamBuffer<B> head, StreamBufferPool<B> bufferPool)
Writes a long value to theStreamBuffer
.static <B> void
write(java.lang.CharSequence characters, int offset, int length, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes theCharSequence
to theStreamBuffer
stream.static <B> void
write(java.lang.CharSequence characters, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes all theCharSequence
to theStreamBuffer
stream.static <B> StreamBuffer<B>
writeByte(byte character, StreamBuffer<B> writeBuffer, StreamBufferPool<B> bufferPool)
Writes a HTTP encoded character.
-
-
-
Field Detail
-
pooledBuffer
public final B pooledBuffer
Obtains the pooled buffer. Will benull
if not pooled.
-
unpooledByteBuffer
public final java.nio.ByteBuffer unpooledByteBuffer
Obtains the non-pooledByteBuffer
. Will benull
if non-pooled.
-
fileBuffer
public final StreamBuffer.FileBuffer fileBuffer
Obtains theStreamBuffer.FileBuffer
. Will benull
if not file.
-
next
public StreamBuffer<B> next
Next
StreamBuffer
in the stream.This allows chaining
StreamBuffer
instances into a linked list (and avoids memory management overheads of creating/destroying lists).
-
-
Constructor Detail
-
StreamBuffer
public StreamBuffer(B pooledBuffer, java.nio.ByteBuffer unpooledByteBuffer, StreamBuffer.FileBuffer fileBuffer)
Instantiate.- Parameters:
pooledBuffer
- Pooled buffer. Must benull
if another buffer provided.unpooledByteBuffer
- UnpooledByteBuffer
. Must benull
if another buffer provided.fileBuffer
-StreamBuffer.FileBuffer
. Must benull
if another buffer provided.- Throws:
java.lang.IllegalArgumentException
- If not providing the one buffer.
-
-
Method Detail
-
write
public static <B> void write(byte[] bytes, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes all the bytes to theStreamBuffer
stream.- Type Parameters:
B
- Buffer type.- Parameters:
bytes
- Bytes to be written to theStreamBuffer
stream.headBuffer
- HeadStreamBuffer
in the linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
should additionalStreamBuffer
instances be required in writing the bytes.
-
write
public static <B> void write(byte[] bytes, int offset, int length, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes the bytes to theStreamBuffer
stream.- Type Parameters:
B
- Buffer type.- Parameters:
bytes
- Bytes to be written to theStreamBuffer
stream.offset
- Offset into the bytes to start writing.length
- Length of bytes to write.headBuffer
- HeadStreamBuffer
in the linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
should additionalStreamBuffer
instances be required in writing the bytes.
-
write
public static <B> void write(java.lang.CharSequence characters, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes all theCharSequence
to theStreamBuffer
stream.- Type Parameters:
B
- Buffer type.- Parameters:
characters
- Characters to be written to theStreamBuffer
stream.headBuffer
- HeadStreamBuffer
in the linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
should additionalStreamBuffer
instances be required in writing the bytes.
-
write
public static <B> void write(java.lang.CharSequence characters, int offset, int length, StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Writes theCharSequence
to theStreamBuffer
stream.- Type Parameters:
B
- Buffer type.- Parameters:
characters
- Characters to be written to theStreamBuffer
stream.offset
- Offset into theCharSequence
to start writing.length
- Length of characters to write.headBuffer
- HeadStreamBuffer
in the linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
should additionalStreamBuffer
instances be required in writing the bytes.
-
write
public static <B> void write(long value, StreamBuffer<B> head, StreamBufferPool<B> bufferPool)
Writes a long value to theStreamBuffer
.- Type Parameters:
B
- Buffer type.- Parameters:
value
- Long value to write to theStreamBuffer
.head
- HeadStreamBuffer
of linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
.
-
getAppendable
public static <B> java.lang.Appendable getAppendable(StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Obtains an
Appendable
to write to theStreamBuffer
stream.Typical use of this is for the
DateTimeFormatter
.- Type Parameters:
B
- Buffer type.- Parameters:
headBuffer
- HeadStreamBuffer
in the linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
should additionalStreamBuffer
instances be required in writing the bytes.- Returns:
Appendable
to write to theStreamBuffer
stream.
-
getWriteStreamBuffer
public static <B> StreamBuffer<B> getWriteStreamBuffer(StreamBuffer<B> headBuffer, StreamBufferPool<B> bufferPool)
Obtains theStreamBuffer
to use for writing.- Type Parameters:
B
- Buffer type.- Parameters:
headBuffer
- HeadStreamBuffer
in the linked list ofStreamBuffer
instances.bufferPool
-StreamBufferPool
should additionalStreamBuffer
instances be required in writing the bytes.- Returns:
StreamBuffer
within the linked list to next write data.
-
writeByte
public static <B> StreamBuffer<B> writeByte(byte character, StreamBuffer<B> writeBuffer, StreamBufferPool<B> bufferPool)
Writes a HTTP encoded character.- Type Parameters:
B
- Buffer type.- Parameters:
character
- Character to write.writeBuffer
- WriteStreamBuffer
.bufferPool
-StreamBufferPool
.- Returns:
- Next write
StreamBuffer
.
-
write
public abstract boolean write(byte datum)
Writes a byte to the pooled buffer.- Parameters:
datum
- Byte value.- Returns:
true
if written value to buffer.false
indicates the pooled buffer is full.
-
write
public abstract int write(byte[] data, int offset, int length)
Writes the data to the pooled buffer.- Parameters:
data
- Data to write to the pooled buffer.offset
- Offset within the data to write the data.length
- Length of data to write the data.- Returns:
- Number of bytes written.
-
write
public int write(byte[] data)
Writes all the data to the pooled buffer.- Parameters:
data
- Data to write to the pooled buffer.- Returns:
- Number of bytes written.
-
release
public abstract void release()
Releases thisStreamBuffer
for re-use.
-
-