Package com.google.protobuf
Class ByteBufferWriter
java.lang.Object
com.google.protobuf.ByteBufferWriter
Utility class to provide efficient writing of
ByteBuffer
s to OutputStream
s.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal
<SoftReference<byte[]>> Keeping a soft reference to a thread-local buffer.private static final float
The fraction of the requested buffer size under which the buffer will be reallocated.private static final long
private static final Class
<?> This is a hack for GAE, whereFileOutputStream
is unavailable.private static final int
Maximum size for a cached buffer.private static final int
Minimum size for a cached buffer. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static void
For testing purposes only.private static byte[]
private static long
getChannelFieldOffset
(Class<?> clazz) 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) (package private) static void
write
(ByteBuffer buffer, OutputStream output) Writes the remaining content of the buffer to the given stream.private static boolean
writeToChannel
(ByteBuffer buffer, OutputStream output)
-
Field Details
-
MIN_CACHED_BUFFER_SIZE
private static final int MIN_CACHED_BUFFER_SIZEMinimum size for a cached buffer. This prevents us from allocating buffers that are too small to be easily reused.- See Also:
-
MAX_CACHED_BUFFER_SIZE
private static final int MAX_CACHED_BUFFER_SIZEMaximum size for a cached buffer. If a larger buffer is required, it will be allocated but not cached.- See Also:
-
BUFFER_REALLOCATION_THRESHOLD
private static final float BUFFER_REALLOCATION_THRESHOLDThe fraction of the requested buffer size under which the buffer will be reallocated.- See Also:
-
BUFFER
Keeping a soft reference to a thread-local buffer. This buffer is used for writing aByteBuffer
to anOutputStream
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). -
FILE_OUTPUT_STREAM_CLASS
This is a hack for GAE, whereFileOutputStream
is unavailable. -
CHANNEL_FIELD_OFFSET
private static final long CHANNEL_FIELD_OFFSET
-
-
Constructor Details
-
ByteBufferWriter
private ByteBufferWriter()
-
-
Method Details
-
clearCachedBuffer
static void clearCachedBuffer()For testing purposes only. Clears the cached buffer to force a new allocation on the next invocation. -
write
Writes the remaining content of the buffer to the given stream. The bufferposition
will remain unchanged by this method.- Throws:
IOException
-
getOrCreateBuffer
private static byte[] getOrCreateBuffer(int requestedSize) -
needToReallocate
private static boolean needToReallocate(int requestedSize, int bufferLength) -
getBuffer
private static byte[] getBuffer() -
setBuffer
private static void setBuffer(byte[] value) -
writeToChannel
- Throws:
IOException
-
safeGetClass
-
getChannelFieldOffset
-