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 | |
35 import java.io.IOException; | 33 import java.io.IOException; |
36 import java.io.InputStream; | 34 import java.io.InputStream; |
37 import java.io.InvalidObjectException; | 35 import java.io.InvalidObjectException; |
38 import java.io.ObjectInputStream; | 36 import java.io.ObjectInputStream; |
39 import java.io.OutputStream; | 37 import java.io.OutputStream; |
40 import java.nio.ByteBuffer; | 38 import java.nio.ByteBuffer; |
41 import java.nio.ByteOrder; | 39 import java.nio.ByteOrder; |
42 import java.nio.InvalidMarkException; | 40 import java.nio.InvalidMarkException; |
43 import java.nio.charset.Charset; | 41 import java.nio.charset.Charset; |
44 import java.util.Collections; | 42 import java.util.Collections; |
45 import java.util.List; | 43 import java.util.List; |
46 | 44 |
47 /** | 45 /** |
48 * A {@link ByteString} that wraps around a {@link ByteBuffer}. | 46 * A {@link ByteString} that wraps around a {@link ByteBuffer}. |
49 */ | 47 */ |
50 final class NioByteString extends ByteString.LeafByteString { | 48 final class NioByteString extends ByteString.LeafByteString { |
51 private final ByteBuffer buffer; | 49 private final ByteBuffer buffer; |
52 | 50 |
53 NioByteString(ByteBuffer buffer) { | 51 NioByteString(ByteBuffer buffer) { |
54 checkNotNull(buffer, "buffer"); | 52 if (buffer == null) { |
| 53 throw new NullPointerException("buffer"); |
| 54 } |
55 | 55 |
56 // Use native byte order for fast fixed32/64 operations. | |
57 this.buffer = buffer.slice().order(ByteOrder.nativeOrder()); | 56 this.buffer = buffer.slice().order(ByteOrder.nativeOrder()); |
58 } | 57 } |
59 | 58 |
60 // ================================================================= | 59 // ================================================================= |
61 // Serializable | 60 // Serializable |
62 | 61 |
63 /** | 62 /** |
64 * Magic method that lets us override serialization behavior. | 63 * Magic method that lets us override serialization behavior. |
65 */ | 64 */ |
66 private Object writeReplace() { | 65 private Object writeReplace() { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 259 |
261 len = Math.min(len, buf.remaining()); | 260 len = Math.min(len, buf.remaining()); |
262 buf.get(bytes, off, len); | 261 buf.get(bytes, off, len); |
263 return len; | 262 return len; |
264 } | 263 } |
265 }; | 264 }; |
266 } | 265 } |
267 | 266 |
268 @Override | 267 @Override |
269 public CodedInputStream newCodedInput() { | 268 public CodedInputStream newCodedInput() { |
270 return CodedInputStream.newInstance(buffer, true); | 269 return CodedInputStream.newInstance(buffer); |
271 } | 270 } |
272 | 271 |
273 /** | 272 /** |
274 * Creates a slice of a range of this buffer. | 273 * Creates a slice of a range of this buffer. |
275 * | 274 * |
276 * @param beginIndex the beginning index of the slice (inclusive). | 275 * @param beginIndex the beginning index of the slice (inclusive). |
277 * @param endIndex the end index of the slice (exclusive). | 276 * @param endIndex the end index of the slice (exclusive). |
278 * @return the requested slice. | 277 * @return the requested slice. |
279 */ | 278 */ |
280 private ByteBuffer slice(int beginIndex, int endIndex) { | 279 private ByteBuffer slice(int beginIndex, int endIndex) { |
281 if (beginIndex < buffer.position() || endIndex > buffer.limit() || beginInde
x > endIndex) { | 280 if (beginIndex < buffer.position() || endIndex > buffer.limit() || beginInde
x > endIndex) { |
282 throw new IllegalArgumentException( | 281 throw new IllegalArgumentException( |
283 String.format("Invalid indices [%d, %d]", beginIndex, endIndex)); | 282 String.format("Invalid indices [%d, %d]", beginIndex, endIndex)); |
284 } | 283 } |
285 | 284 |
286 ByteBuffer slice = buffer.slice(); | 285 ByteBuffer slice = buffer.slice(); |
287 slice.position(beginIndex - buffer.position()); | 286 slice.position(beginIndex - buffer.position()); |
288 slice.limit(endIndex - buffer.position()); | 287 slice.limit(endIndex - buffer.position()); |
289 return slice; | 288 return slice; |
290 } | 289 } |
291 } | 290 } |
OLD | NEW |