Class ByteBufferWriter


  • final class ByteBufferWriter
    extends java.lang.Object
    Utility class to provide efficient writing of ByteBuffers to OutputStreams.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.ThreadLocal<java.lang.ref.SoftReference<byte[]>> BUFFER
      Keeping a soft reference to a thread-local buffer.
      private static float BUFFER_REALLOCATION_THRESHOLD
      The fraction of the requested buffer size under which the buffer will be reallocated.
      private static long CHANNEL_FIELD_OFFSET  
      private static java.lang.Class<?> FILE_OUTPUT_STREAM_CLASS
      This is a hack for GAE, where FileOutputStream is unavailable.
      private static int MAX_CACHED_BUFFER_SIZE
      Maximum size for a cached buffer.
      private static int MIN_CACHED_BUFFER_SIZE
      Minimum size for a cached buffer.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ByteBufferWriter()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static void clearCachedBuffer()
      For testing purposes only.
      private static byte[] getBuffer()  
      private static long getChannelFieldOffset​(java.lang.Class<?> clazz)  
      private static byte[] getOrCreateBuffer​(int requestedSize)  
      private static boolean needToReallocate​(int requestedSize, int bufferLength)  
      private static java.lang.Class<?> safeGetClass​(java.lang.String className)  
      private static void setBuffer​(byte[] value)  
      (package private) static void write​(java.nio.ByteBuffer buffer, java.io.OutputStream output)
      Writes the remaining content of the buffer to the given stream.
      private static boolean writeToChannel​(java.nio.ByteBuffer buffer, java.io.OutputStream output)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MIN_CACHED_BUFFER_SIZE

        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.
        See Also:
        Constant Field Values
      • MAX_CACHED_BUFFER_SIZE

        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.
        See Also:
        Constant Field Values
      • BUFFER_REALLOCATION_THRESHOLD

        private static final float BUFFER_REALLOCATION_THRESHOLD
        The fraction of the requested buffer size under which the buffer will be reallocated.
        See Also:
        Constant Field Values
      • BUFFER

        private static final java.lang.ThreadLocal<java.lang.ref.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).
      • FILE_OUTPUT_STREAM_CLASS

        private static final java.lang.Class<?> FILE_OUTPUT_STREAM_CLASS
        This is a hack for GAE, where FileOutputStream is unavailable.
      • CHANNEL_FIELD_OFFSET

        private static final long CHANNEL_FIELD_OFFSET
    • Constructor Detail

      • ByteBufferWriter

        private ByteBufferWriter()
    • Method Detail

      • clearCachedBuffer

        static void clearCachedBuffer()
        For testing purposes only. Clears the cached buffer to force a new allocation on the next invocation.
      • write

        static void write​(java.nio.ByteBuffer buffer,
                          java.io.OutputStream output)
                   throws java.io.IOException
        Writes the remaining content of the buffer to the given stream. The buffer position will remain unchanged by this method.
        Throws:
        java.io.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

        private static boolean writeToChannel​(java.nio.ByteBuffer buffer,
                                              java.io.OutputStream output)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • safeGetClass

        private static java.lang.Class<?> safeGetClass​(java.lang.String className)
      • getChannelFieldOffset

        private static long getChannelFieldOffset​(java.lang.Class<?> clazz)