Top Description Fields Constructors Methods
com.google.protobuf

pack-priv final Class ByteBufferWriter

extends Object
Class Inheritance
Static Imports
java.lang.Math.max, .Math.min

Utility class to provide efficient writing of ByteBuffers to OutputStreams.

Field Summary

Modifier and TypeField and Description
private static final ThreadLocal<SoftReference<byte[]>>
BUFFER

Keeping a soft reference to a thread-local buffer.

private static final float
BUFFER_REALLOCATION_THRESHOLD

The fraction of the requested buffer size under which the buffer will be reallocated.

private static final long
private static final Class<?>
FILE_OUTPUT_STREAM_CLASS

This is a hack for GAE, where FileOutputStream is unavailable.

private static final int
MAX_CACHED_BUFFER_SIZE

Maximum size for a cached buffer.

private static final int
MIN_CACHED_BUFFER_SIZE

Minimum size for a cached buffer.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static void
clearCachedBuffer()

For testing purposes only.

private static byte[]
private static long
private static byte[]
getOrCreateBuffer(int requestedSize)

private static boolean
needToReallocate(int requestedSize, int bufferLength)

private static Class<?>
safeGetClass(String className)

private static void
setBuffer(byte[] value)

pack-priv static void
write(ByteBuffer buffer, OutputStream output)

Writes the remaining content of the buffer to the given stream.

private static boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFERback to summary
private static final ThreadLocal<SoftReference<byte[]>> BUFFER

Keeping a soft reference to a thread-local buffer. This buffer is used for writing a ByteBuffer to an OutputStream when no zero-copy alternative was available. Using a "soft" reference since VMs may keep this reference around longer than "weak" (e.g. HotSpot will maintain soft references until memory pressure warrants collection).

BUFFER_REALLOCATION_THRESHOLDback to summary
private static final float BUFFER_REALLOCATION_THRESHOLD

The fraction of the requested buffer size under which the buffer will be reallocated.

CHANNEL_FIELD_OFFSETback to summary
private static final long CHANNEL_FIELD_OFFSET
FILE_OUTPUT_STREAM_CLASSback to summary
private static final Class<?> FILE_OUTPUT_STREAM_CLASS

This is a hack for GAE, where FileOutputStream is unavailable.

MAX_CACHED_BUFFER_SIZEback to summary
private static final int MAX_CACHED_BUFFER_SIZE

Maximum size for a cached buffer. If a larger buffer is required, it will be allocated but not cached.

MIN_CACHED_BUFFER_SIZEback to summary
private static final int MIN_CACHED_BUFFER_SIZE

Minimum size for a cached buffer. This prevents us from allocating buffers that are too small to be easily reused.

Constructor Detail

ByteBufferWriterback to summary
private ByteBufferWriter()

Method Detail

clearCachedBufferback to summary
pack-priv static void clearCachedBuffer()

For testing purposes only. Clears the cached buffer to force a new allocation on the next invocation.

getBufferback to summary
private static byte[] getBuffer()
getChannelFieldOffsetback to summary
private static long getChannelFieldOffset(Class<?> clazz)
getOrCreateBufferback to summary
private static byte[] getOrCreateBuffer(int requestedSize)
needToReallocateback to summary
private static boolean needToReallocate(int requestedSize, int bufferLength)
safeGetClassback to summary
private static Class<?> safeGetClass(String className)
setBufferback to summary
private static void setBuffer(byte[] value)
writeback to summary
pack-priv static void write(ByteBuffer buffer, OutputStream output) throws IOException

Writes the remaining content of the buffer to the given stream. The buffer position will remain unchanged by this method.

writeToChannelback to summary
private static boolean writeToChannel(ByteBuffer buffer, OutputStream output) throws IOException