~verterok/ubuntu/lucid/protobuf/2.4.0a-backport

« back to all changes in this revision

Viewing changes to java/src/main/java/com/google/protobuf/CodedInputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
   */
68
68
  public static CodedInputStream newInstance(final byte[] buf, final int off,
69
69
                                             final int len) {
70
 
    return new CodedInputStream(buf, off, len);
 
70
    CodedInputStream result = new CodedInputStream(buf, off, len);
 
71
    try {
 
72
      // Some uses of CodedInputStream can be more efficient if they know
 
73
      // exactly how many bytes are available.  By pushing the end point of the
 
74
      // buffer as a limit, we allow them to get this information via
 
75
      // getBytesUntilLimit().  Pushing a limit that we know is at the end of
 
76
      // the stream can never hurt, since we can never past that point anyway.
 
77
      result.pushLimit(len);
 
78
    } catch (InvalidProtocolBufferException ex) {
 
79
      // The only reason pushLimit() might throw an exception here is if len
 
80
      // is negative. Normally pushLimit()'s parameter comes directly off the
 
81
      // wire, so it's important to catch exceptions in case of corrupt or
 
82
      // malicious data. However, in this case, we expect that len is not a
 
83
      // user-supplied value, so we can assume that it being negative indicates
 
84
      // a programming error. Therefore, throwing an unchecked exception is
 
85
      // appropriate.
 
86
      throw new IllegalArgumentException(ex);
 
87
    }
 
88
    return result;
71
89
  }
72
90
 
73
91
  // -----------------------------------------------------------------
263
281
  /** Read a {@code bytes} field value from the stream. */
264
282
  public ByteString readBytes() throws IOException {
265
283
    final int size = readRawVarint32();
266
 
    if (size <= (bufferSize - bufferPos) && size > 0) {
 
284
    if (size == 0) {
 
285
      return ByteString.EMPTY;
 
286
    } else if (size <= (bufferSize - bufferPos) && size > 0) {
267
287
      // Fast path:  We already have the bytes in a contiguous buffer, so
268
288
      //   just copy directly from it.
269
289
      final ByteString result = ByteString.copyFrom(buffer, bufferPos, size);
368
388
   * has already read one byte.  This allows the caller to determine if EOF
369
389
   * has been reached before attempting to read.
370
390
   */
371
 
  static int readRawVarint32(final int firstByte,
372
 
                             final InputStream input) throws IOException {
 
391
  public static int readRawVarint32(
 
392
      final int firstByte, final InputStream input) throws IOException {
373
393
    if ((firstByte & 0x80) == 0) {
374
394
      return firstByte;
375
395
    }
847
867
    } else {
848
868
      // Skipping more bytes than are in the buffer.  First skip what we have.
849
869
      int pos = bufferSize - bufferPos;
850
 
      totalBytesRetired += pos;
851
 
      bufferPos = 0;
852
 
      bufferSize = 0;
 
870
      bufferPos = bufferSize;
853
871
 
854
 
      // Then skip directly from the InputStream for the rest.
855
 
      while (pos < size) {
856
 
        final int n = (input == null) ? -1 : (int) input.skip(size - pos);
857
 
        if (n <= 0) {
858
 
          throw InvalidProtocolBufferException.truncatedMessage();
859
 
        }
860
 
        pos += n;
861
 
        totalBytesRetired += n;
 
872
      // Keep refilling the buffer until we get to the point we wanted to skip
 
873
      // to.  This has the side effect of ensuring the limits are updated
 
874
      // correctly.
 
875
      refillBuffer(true);
 
876
      while (size - pos > bufferSize) {
 
877
        pos += bufferSize;
 
878
        bufferPos = bufferSize;
 
879
        refillBuffer(true);
862
880
      }
 
881
 
 
882
      bufferPos = size - pos; 
863
883
    }
864
884
  }
865
885
}