1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static java.lang.Math.max;
34 import static java.lang.Math.min;
35 
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.lang.ref.SoftReference;
40 import java.nio.ByteBuffer;
41 
42 /**
43  * Utility class to provide efficient writing of {@link ByteBuffer}s to {@link OutputStream}s.
44  */
45 final class ByteBufferWriter {
ByteBufferWriter()46   private ByteBufferWriter() {}
47 
48   /**
49    * Minimum size for a cached buffer. This prevents us from allocating buffers that are too
50    * small to be easily reused.
51    */
52   // TODO(nathanmittler): tune this property or allow configuration?
53   private static final int MIN_CACHED_BUFFER_SIZE = 1024;
54 
55   /**
56    * Maximum size for a cached buffer. If a larger buffer is required, it will be allocated
57    * but not cached.
58    */
59   // TODO(nathanmittler): tune this property or allow configuration?
60   private static final int MAX_CACHED_BUFFER_SIZE = 16 * 1024;
61 
62   /**
63    * The fraction of the requested buffer size under which the buffer will be reallocated.
64    */
65   // TODO(nathanmittler): tune this property or allow configuration?
66   private static final float BUFFER_REALLOCATION_THRESHOLD = 0.5f;
67 
68   /**
69    * Keeping a soft reference to a thread-local buffer. This buffer is used for writing a
70    * {@link ByteBuffer} to an {@link OutputStream} when no zero-copy alternative was available.
71    * Using a "soft" reference since VMs may keep this reference around longer than "weak"
72    * (e.g. HotSpot will maintain soft references until memory pressure warrants collection).
73    */
74   private static final ThreadLocal<SoftReference<byte[]>> BUFFER =
75       new ThreadLocal<SoftReference<byte[]>>();
76 
77   /**
78    * For testing purposes only. Clears the cached buffer to force a new allocation on the next
79    * invocation.
80    */
clearCachedBuffer()81   static void clearCachedBuffer() {
82     BUFFER.set(null);
83   }
84 
85   /**
86    * Writes the remaining content of the buffer to the given stream. The buffer {@code position}
87    * will remain unchanged by this method.
88    */
write(ByteBuffer buffer, OutputStream output)89   static void write(ByteBuffer buffer, OutputStream output) throws IOException {
90     final int initialPos = buffer.position();
91     try {
92       if (buffer.hasArray()) {
93         // Optimized write for array-backed buffers.
94         // Note that we're taking the risk that a malicious OutputStream could modify the array.
95         output.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
96       } else if (output instanceof FileOutputStream) {
97         // Use a channel to write out the ByteBuffer. This will automatically empty the buffer.
98         ((FileOutputStream) output).getChannel().write(buffer);
99       } else {
100         // Read all of the data from the buffer to an array.
101         // TODO(nathanmittler): Consider performance improvements for other "known" stream types.
102         final byte[] array = getOrCreateBuffer(buffer.remaining());
103         while (buffer.hasRemaining()) {
104           int length = min(buffer.remaining(), array.length);
105           buffer.get(array, 0, length);
106           output.write(array, 0, length);
107         }
108       }
109     } finally {
110       // Restore the initial position.
111       buffer.position(initialPos);
112     }
113   }
114 
getOrCreateBuffer(int requestedSize)115   private static byte[] getOrCreateBuffer(int requestedSize) {
116     requestedSize = max(requestedSize, MIN_CACHED_BUFFER_SIZE);
117 
118     byte[] buffer = getBuffer();
119     // Only allocate if we need to.
120     if (buffer == null || needToReallocate(requestedSize, buffer.length)) {
121       buffer = new byte[requestedSize];
122 
123       // Only cache the buffer if it's not too big.
124       if (requestedSize <= MAX_CACHED_BUFFER_SIZE) {
125         setBuffer(buffer);
126       }
127     }
128     return buffer;
129   }
130 
needToReallocate(int requestedSize, int bufferLength)131   private static boolean needToReallocate(int requestedSize, int bufferLength) {
132     // First check against just the requested length to avoid the multiply.
133     return bufferLength < requestedSize
134         && bufferLength < requestedSize * BUFFER_REALLOCATION_THRESHOLD;
135   }
136 
getBuffer()137   private static byte[] getBuffer() {
138     SoftReference<byte[]> sr = BUFFER.get();
139     return sr == null ? null : sr.get();
140   }
141 
setBuffer(byte[] value)142   private static void setBuffer(byte[] value) {
143     BUFFER.set(new SoftReference<byte[]>(value));
144   }
145 }
146