Top Description Fields Constructors Methods
org.glassfish.grizzly.utils

public Class BufferOutputStream

extends OutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream, org.glassfish.grizzly.Buffer, org.glassfish.grizzly.memory.Buffers, .CompositeBuffer, .MemoryManager

OutputStream implementation to write to a Buffer.

Field Summary

Modifier and TypeField and Description
private static final int
private CompositeBuffer
private Buffer
pack-priv final MemoryManager<E extends Buffer>
pack-priv final boolean

Constructor Summary

AccessConstructor and Description
public
BufferOutputStream(final MemoryManager<E extends Buffer> mm)

Creates the BufferOutputStream without initial Buffer.

public
BufferOutputStream(final MemoryManager<E extends Buffer> mm, final Buffer
initial Buffer
buffer
)

Creates the BufferOutputStream using passed Buffer as initial.

public
BufferOutputStream(final MemoryManager<E extends Buffer> mm, final Buffer
initial Buffer
buffer
,
final boolean reallocate)

Creates the BufferOutputStream using passed Buffer as initial.

Method Summary

Modifier and TypeMethod and Description
protected Buffer
allocateNewBuffer(final MemoryManager<E extends Buffer> memoryManager, final int size)

public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.

private void
ensureCapacity(final int len)

public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.

private void
public Buffer

Returns:

the result Buffer (not flipped).
getBuffer
()

Get the result Buffer (not flipped).

public boolean

Returns:

true if "reallocate" strategy is used or false otherwise.
isReallocate
()

Returns true if "reallocate" strategy is used or false otherwise.

public void
public void
setInitialOutputBuffer(final Buffer initialBuffer)

public void
write(int b)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(byte[] b)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.

public void
write(byte[] b, int off, int len)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Inherited from java.io.OutputStream:
nullOutputStream

Field Detail

BUFFER_SIZEback to summary
private static final int BUFFER_SIZE
compositeBufferback to summary
private CompositeBuffer compositeBuffer
currentBufferback to summary
private Buffer currentBuffer
mmback to summary
pack-priv final MemoryManager<E extends Buffer> mm
reallocateback to summary
pack-priv final boolean reallocate

Constructor Detail

BufferOutputStreamback to summary
public BufferOutputStream(final MemoryManager<E extends Buffer> mm)

Creates the BufferOutputStream without initial Buffer. Created BufferOutputStream won't use "reallocate" strategy, which means if internal Buffer window gets overloaded - it will be appended to the CompositeBuffer and new window will be allocated.

Parameters
mm:MemoryManager<E extends Buffer>

MemoryManager

BufferOutputStreamback to summary
public BufferOutputStream(final MemoryManager<E extends Buffer> mm, final Buffer buffer)

Creates the BufferOutputStream using passed Buffer as initial. Created BufferOutputStream won't use "reallocate" strategy, which means if internal Buffer window gets overloaded - it will be appended to the CompositeBuffer and new window will be allocated.

Parameters
mm:MemoryManager<E extends Buffer>

MemoryManager

buffer:Buffer

initial Buffer

BufferOutputStreamback to summary
public BufferOutputStream(final MemoryManager<E extends Buffer> mm, final Buffer buffer, final boolean reallocate)

Creates the BufferOutputStream using passed Buffer as initial. Created BufferOutputStream can choose whether use or not "reallocate" strategy. Using "reallocate" strategy means following: if internal Buffer window gets overloaded - it will be reallocated using MemoryManager#reallocate(org.glassfish.grizzly.Buffer, int), which may lead to extra memory allocation and bytes copying (bigger memory chunk might be allocated to keep original stream data plus extra data been written to stream), the benefit of this is that stream data will be located in the single memory chunk. If we don't use "reallocate" strategy - when internal Buffer gets overloaded it will be appended to the CompositeBuffer and new window Buffer will be allocated then.

Parameters
mm:MemoryManager<E extends Buffer>

MemoryManager

buffer:Buffer

initial Buffer

Method Detail

allocateNewBufferback to summary
protected Buffer allocateNewBuffer(final MemoryManager<E extends Buffer> memoryManager, final int size)
closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
ensureCapacityback to summary
private void ensureCapacity(final int len)
Annotations
@SuppressWarnings:unchecked
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
flushCurrentback to summary
private void flushCurrent()
getBufferback to summary
public Buffer getBuffer()

Get the result Buffer (not flipped).

Returns:Buffer

the result Buffer (not flipped).

isReallocateback to summary
public boolean isReallocate()

Returns true if "reallocate" strategy is used or false otherwise. Using "reallocate" strategy means following: if internal Buffer window gets overloaded - it will be reallocated using MemoryManager#reallocate(org.glassfish.grizzly.Buffer, int), which may lead to extra memory allocation and bytes copying (bigger memory chunk might be allocated to keep original stream data plus extra data been written to stream), the benefit of this is that stream data will be located in the single memory chunk. If we don't use "reallocate" strategy - when internal Buffer gets overloaded it will be appended to the CompositeBuffer and new window Buffer will be allocated then.

Returns:boolean

true if "reallocate" strategy is used or false otherwise.

resetback to summary
public void reset()
setInitialOutputBufferback to summary
public void setInitialOutputBuffer(final Buffer initialBuffer)
writeback to summary
public void write(int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.