@Override public byte readRawByte() throws IOException { if (pos == limit) { throw InvalidProtocolBufferException.truncatedMessage(); } return buffer[pos++]; }
@Override public int readRawLittleEndian32() throws IOException { int tempPos = pos; if (limit - tempPos < FIXED32_SIZE) { throw InvalidProtocolBufferException.truncatedMessage(); } final byte[] buffer = this.buffer; pos = tempPos + FIXED32_SIZE; return (((buffer[tempPos] & 0xff)) | ((buffer[tempPos + 1] & 0xff) << 8) | ((buffer[tempPos + 2] & 0xff) << 16) | ((buffer[tempPos + 3] & 0xff) << 24)); }
@Override public long readRawLittleEndian64() throws IOException { int tempPos = pos; if (limit - tempPos < FIXED64_SIZE) { throw InvalidProtocolBufferException.truncatedMessage(); } final byte[] buffer = this.buffer; pos = tempPos + FIXED64_SIZE; return (((buffer[tempPos] & 0xffL)) | ((buffer[tempPos + 1] & 0xffL) << 8) | ((buffer[tempPos + 2] & 0xffL) << 16) | ((buffer[tempPos + 3] & 0xffL) << 24) | ((buffer[tempPos + 4] & 0xffL) << 32) | ((buffer[tempPos + 5] & 0xffL) << 40) | ((buffer[tempPos + 6] & 0xffL) << 48) | ((buffer[tempPos + 7] & 0xffL) << 56)); }
public void seek(long pointer) throws IOException { if (pointer - totalBytesRetired >= 0 && pointer - totalBytesRetired < bufferSize) { if (pointer > currentLimit) { // Then fail. throw InvalidProtocolBufferException.truncatedMessage(); } bufferPos = (int) (pointer - totalBytesRetired); } else { totalBytesRetired = (int) pointer; bufferSizeAfterLimit = 0; raf.seek(pointer); bufferPos = 0; bufferSize = 0; } } }
/** * Reads a varint from the input one byte at a time, so that it does not read any bytes after the * end of the varint. If you simply wrapped the stream in a CodedInputStream and used {@link * #readRawVarint32(InputStream)} then you would probably end up reading past the end of the * varint since CodedInputStream buffers its input. */ static int readRawVarint32(final InputStream input) throws IOException { final int firstByte = input.read(); if (firstByte == -1) { throw InvalidProtocolBufferException.truncatedMessage(); } return readRawVarint32(firstByte, input); }
/** * Reads a varint from the input one byte at a time, so that it does not * read any bytes after the end of the varint. If you simply wrapped the * stream in a CodedInputStream and used {@link #readRawVarint32(InputStream)} * then you would probably end up reading past the end of the varint since * CodedInputStream buffers its input. */ static int readRawVarint32(final InputStream input) throws IOException { final int firstByte = input.read(); if (firstByte == -1) { throw InvalidProtocolBufferException.truncatedMessage(); } return readRawVarint32(firstByte, input); }
private ByteBuffer slice(int begin, int end) throws IOException { int prevPos = currentByteBuffer.position(); int prevLimit = currentByteBuffer.limit(); try { currentByteBuffer.position(begin); currentByteBuffer.limit(end); return currentByteBuffer.slice(); } catch (IllegalArgumentException e) { throw InvalidProtocolBufferException.truncatedMessage(); } finally { currentByteBuffer.position(prevPos); currentByteBuffer.limit(prevLimit); } } }
@Override public byte readRawByte() throws IOException { if (pos == limit) { throw InvalidProtocolBufferException.truncatedMessage(); } return UnsafeUtil.getByte(pos++); }
/** To get the next ByteBuffer from {@code input}, and then update the parameters */ private void getNextByteBuffer() throws InvalidProtocolBufferException { if (!iterator.hasNext()) { throw InvalidProtocolBufferException.truncatedMessage(); } tryGetNextByteBuffer(); }
@Override public void skipRawBytes(final int length) throws IOException { if (length >= 0 && length <= (limit - pos)) { // We have all the bytes we need already. pos += length; return; } if (length < 0) { throw InvalidProtocolBufferException.negativeSize(); } throw InvalidProtocolBufferException.truncatedMessage(); } }
@Override public byte[] readRawBytes(final int length) throws IOException { if (length > 0 && length <= (limit - pos)) { final int tempPos = pos; pos += length; return Arrays.copyOfRange(buffer, tempPos, pos); } if (length <= 0) { if (length == 0) { return Internal.EMPTY_BYTE_ARRAY; } else { throw InvalidProtocolBufferException.negativeSize(); } } throw InvalidProtocolBufferException.truncatedMessage(); }
private ByteBuffer slice(long begin, long end) throws IOException { int prevPos = buffer.position(); int prevLimit = buffer.limit(); try { buffer.position(bufferPos(begin)); buffer.limit(bufferPos(end)); return buffer.slice(); } catch (IllegalArgumentException e) { throw InvalidProtocolBufferException.truncatedMessage(); } finally { buffer.position(prevPos); buffer.limit(prevLimit); } } }
@Override public void skipRawBytes(final int length) throws IOException { if (length >= 0 && length <= remaining()) { // We have all the bytes we need already. pos += length; return; } if (length < 0) { throw InvalidProtocolBufferException.negativeSize(); } throw InvalidProtocolBufferException.truncatedMessage(); }
@Override public int pushLimit(int byteLimit) throws InvalidProtocolBufferException { if (byteLimit < 0) { throw InvalidProtocolBufferException.negativeSize(); } byteLimit += totalBytesRetired + pos; final int oldLimit = currentLimit; if (byteLimit > oldLimit) { throw InvalidProtocolBufferException.truncatedMessage(); } currentLimit = byteLimit; recomputeBufferSizeAfterLimit(); return oldLimit; }
@Override public byte[] readRawBytes(final int length) throws IOException { if (length >= 0 && length <= remaining()) { byte[] bytes = new byte[length]; slice(pos, pos + length).get(bytes); pos += length; return bytes; } if (length <= 0) { if (length == 0) { return EMPTY_BYTE_ARRAY; } else { throw InvalidProtocolBufferException.negativeSize(); } } throw InvalidProtocolBufferException.truncatedMessage(); }
@Override public int pushLimit(int byteLimit) throws InvalidProtocolBufferException { if (byteLimit < 0) { throw InvalidProtocolBufferException.negativeSize(); } byteLimit += getTotalBytesRead(); final int oldLimit = currentLimit; if (byteLimit > oldLimit) { throw InvalidProtocolBufferException.truncatedMessage(); } currentLimit = byteLimit; recomputeBufferSizeAfterLimit(); return oldLimit; }
@Override public int pushLimit(int byteLimit) throws InvalidProtocolBufferException { if (byteLimit < 0) { throw InvalidProtocolBufferException.negativeSize(); } byteLimit += getTotalBytesRead(); final int oldLimit = currentLimit; if (byteLimit > oldLimit) { throw InvalidProtocolBufferException.truncatedMessage(); } currentLimit = byteLimit; recomputeBufferSizeAfterLimit(); return oldLimit; }
@Override public int pushLimit(int byteLimit) throws InvalidProtocolBufferException { if (byteLimit < 0) { throw InvalidProtocolBufferException.negativeSize(); } byteLimit += getTotalBytesRead(); final int oldLimit = currentLimit; if (byteLimit > oldLimit) { throw InvalidProtocolBufferException.truncatedMessage(); } currentLimit = byteLimit; recomputeBufferSizeAfterLimit(); return oldLimit; }
@Override public int readRawLittleEndian32() throws IOException { long tempPos = pos; if (limit - tempPos < FIXED32_SIZE) { throw InvalidProtocolBufferException.truncatedMessage(); } pos = tempPos + FIXED32_SIZE; return (((UnsafeUtil.getByte(tempPos) & 0xff)) | ((UnsafeUtil.getByte(tempPos + 1) & 0xff) << 8) | ((UnsafeUtil.getByte(tempPos + 2) & 0xff) << 16) | ((UnsafeUtil.getByte(tempPos + 3) & 0xff) << 24)); }
@Override public long readRawLittleEndian64() throws IOException { long tempPos = pos; if (limit - tempPos < FIXED64_SIZE) { throw InvalidProtocolBufferException.truncatedMessage(); } pos = tempPos + FIXED64_SIZE; return (((UnsafeUtil.getByte(tempPos) & 0xffL)) | ((UnsafeUtil.getByte(tempPos + 1) & 0xffL) << 8) | ((UnsafeUtil.getByte(tempPos + 2) & 0xffL) << 16) | ((UnsafeUtil.getByte(tempPos + 3) & 0xffL) << 24) | ((UnsafeUtil.getByte(tempPos + 4) & 0xffL) << 32) | ((UnsafeUtil.getByte(tempPos + 5) & 0xffL) << 40) | ((UnsafeUtil.getByte(tempPos + 6) & 0xffL) << 48) | ((UnsafeUtil.getByte(tempPos + 7) & 0xffL) << 56)); }