OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 12 matching lines...) Expand all Loading... |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
| 33 import static com.google.protobuf.Internal.checkNotNull; |
| 34 |
33 import java.io.IOException; | 35 import java.io.IOException; |
34 import java.io.InputStream; | 36 import java.io.InputStream; |
35 import java.io.InvalidObjectException; | 37 import java.io.InvalidObjectException; |
36 import java.io.ObjectInputStream; | 38 import java.io.ObjectInputStream; |
37 import java.io.OutputStream; | 39 import java.io.OutputStream; |
38 import java.nio.ByteBuffer; | 40 import java.nio.ByteBuffer; |
39 import java.nio.ByteOrder; | 41 import java.nio.ByteOrder; |
40 import java.nio.InvalidMarkException; | 42 import java.nio.InvalidMarkException; |
41 import java.nio.charset.Charset; | 43 import java.nio.charset.Charset; |
42 import java.util.Collections; | 44 import java.util.Collections; |
43 import java.util.List; | 45 import java.util.List; |
44 | 46 |
45 /** | 47 /** |
46 * A {@link ByteString} that wraps around a {@link ByteBuffer}. | 48 * A {@link ByteString} that wraps around a {@link ByteBuffer}. |
47 */ | 49 */ |
48 final class NioByteString extends ByteString.LeafByteString { | 50 final class NioByteString extends ByteString.LeafByteString { |
49 private final ByteBuffer buffer; | 51 private final ByteBuffer buffer; |
50 | 52 |
51 NioByteString(ByteBuffer buffer) { | 53 NioByteString(ByteBuffer buffer) { |
52 if (buffer == null) { | 54 checkNotNull(buffer, "buffer"); |
53 throw new NullPointerException("buffer"); | |
54 } | |
55 | 55 |
| 56 // Use native byte order for fast fixed32/64 operations. |
56 this.buffer = buffer.slice().order(ByteOrder.nativeOrder()); | 57 this.buffer = buffer.slice().order(ByteOrder.nativeOrder()); |
57 } | 58 } |
58 | 59 |
59 // ================================================================= | 60 // ================================================================= |
60 // Serializable | 61 // Serializable |
61 | 62 |
62 /** | 63 /** |
63 * Magic method that lets us override serialization behavior. | 64 * Magic method that lets us override serialization behavior. |
64 */ | 65 */ |
65 private Object writeReplace() { | 66 private Object writeReplace() { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 | 260 |
260 len = Math.min(len, buf.remaining()); | 261 len = Math.min(len, buf.remaining()); |
261 buf.get(bytes, off, len); | 262 buf.get(bytes, off, len); |
262 return len; | 263 return len; |
263 } | 264 } |
264 }; | 265 }; |
265 } | 266 } |
266 | 267 |
267 @Override | 268 @Override |
268 public CodedInputStream newCodedInput() { | 269 public CodedInputStream newCodedInput() { |
269 return CodedInputStream.newInstance(buffer); | 270 return CodedInputStream.newInstance(buffer, true); |
270 } | 271 } |
271 | 272 |
272 /** | 273 /** |
273 * Creates a slice of a range of this buffer. | 274 * Creates a slice of a range of this buffer. |
274 * | 275 * |
275 * @param beginIndex the beginning index of the slice (inclusive). | 276 * @param beginIndex the beginning index of the slice (inclusive). |
276 * @param endIndex the end index of the slice (exclusive). | 277 * @param endIndex the end index of the slice (exclusive). |
277 * @return the requested slice. | 278 * @return the requested slice. |
278 */ | 279 */ |
279 private ByteBuffer slice(int beginIndex, int endIndex) { | 280 private ByteBuffer slice(int beginIndex, int endIndex) { |
280 if (beginIndex < buffer.position() || endIndex > buffer.limit() || beginInde
x > endIndex) { | 281 if (beginIndex < buffer.position() || endIndex > buffer.limit() || beginInde
x > endIndex) { |
281 throw new IllegalArgumentException( | 282 throw new IllegalArgumentException( |
282 String.format("Invalid indices [%d, %d]", beginIndex, endIndex)); | 283 String.format("Invalid indices [%d, %d]", beginIndex, endIndex)); |
283 } | 284 } |
284 | 285 |
285 ByteBuffer slice = buffer.slice(); | 286 ByteBuffer slice = buffer.slice(); |
286 slice.position(beginIndex - buffer.position()); | 287 slice.position(beginIndex - buffer.position()); |
287 slice.limit(endIndex - buffer.position()); | 288 slice.limit(endIndex - buffer.position()); |
288 return slice; | 289 return slice; |
289 } | 290 } |
290 } | 291 } |
OLD | NEW |