public byte[] readByteArray() throws IOException { final int size = readRawVarint32(); if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final byte[] copy = new byte[size]; System.arraycopy(buffer, bufferPos, copy, 0, size); bufferPos += size; return copy; } else { // Slow path: Build a byte array first then copy it. return readRawBytes(size); } }
public void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) throws IOException { final int size = readRawVarint32(); if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer output.writeByteRange(utf8String, fieldNumber, buffer, bufferPos, size, repeated); bufferPos += size; } else { // Slow path: Build a byte array first then copy it. output.writeByteRange(utf8String, fieldNumber, readRawBytes(size), 0, size, repeated); } }
/** Read a {@code string} field value from the stream. */ public String readString() throws IOException { final int size = readRawVarint32(); if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final String result = STRING.deser(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. return STRING.deser(readRawBytes(size)); } }
/** Read a {@code bytes} field value from the stream. */ public ByteString readBytes() throws IOException { final int size = readRawVarint32(); if (size == 0) { return ByteString.EMPTY; } if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final ByteString result = ByteString.copyFrom(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. //return ByteString.copyFrom(readRawBytes(size)); return ByteString.wrap(readRawBytes(size)); } }