/** * Creates a protobuf pipe from a byte array. */ public static Pipe newPipe(byte[] data, int offset, int len) { final ByteArrayInput byteArrayInput = new ByteArrayInput(data, offset, len, false); return new Pipe() { protected Input begin(Pipe.Schema<?> pipeSchema) throws IOException { return byteArrayInput; } protected void end(Pipe.Schema<?> pipeSchema, Input input, boolean cleanupOnly) throws IOException { if(cleanupOnly) return; assert input == byteArrayInput; } }; }
public <T> int readFieldNumber(Schema<T> schema) throws IOException { final int fieldNumber = input.readFieldNumber(schema); if(WireFormat.getTagWireType(input.getLastTag()) == WIRETYPE_REFERENCE) { // a reference. lastRef = input.readUInt32(); messageReference = true; } else { // always unset. messageReference = false; } return fieldNumber; }
public ByteString readBytes() throws IOException { return ByteString.wrap(readByteArray()); }
@Override public <T> void handleUnknownField(int fieldNumber, Schema<T> schema) throws IOException { if (!messageReference) input.skipField(input.getLastTag()); }
/** * Merges the {@code message} with the byte array using the given {@code schema}. */ static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema, boolean decodeNestedMessageAsGroup) { try { final ByteArrayInput input = new ByteArrayInput(data, offset, length, decodeNestedMessageAsGroup); schema.mergeFrom(input, message); input.checkLastTagWas(0); } catch(ArrayIndexOutOfBoundsException ae) { throw new RuntimeException("Truncated.", ProtobufException.truncatedMessage(ae)); } catch (IOException e) { throw new RuntimeException("Reading from a byte array threw an IOException (should " + "never happen).",e); } }
readInt32(); return true; case WIRETYPE_FIXED64: readRawLittleEndian64(); return true; case WIRETYPE_LENGTH_DELIMITED: final int size = readRawVarint32(); if(size < 0) throw ProtobufException.negativeSize(); return true; case WIRETYPE_START_GROUP: skipMessage(); checkLastTagWas(makeTag(getTagFieldNumber(tag), WIRETYPE_END_GROUP)); return true; case WIRETYPE_END_GROUP: return false; case WIRETYPE_FIXED32: readRawLittleEndian32(); return true; default:
this.input.setBounds(offset, limit) : new ByteArrayInput(inBuffer, offset, limit, true); input.checkLastTagWas(0);
public <T> T mergeObject(T value, final Schema<T> schema) throws IOException { if(decodeNestedMessageAsGroup) return mergeObjectEncodedAsGroup(value, schema); final int length = readRawVarint32(); if(length < 0) throw ProtobufException.negativeSize(); // save old limit final int oldLimit = this.limit; this.limit = offset + length; if(value == null) value = schema.newMessage(); schema.mergeFrom(this, value); if(!schema.isInitialized(value)) throw new UninitializedMessageException(value, schema); checkLastTagWas(0); // restore old limit this.limit = oldLimit; return value; }
private <T> T mergeObjectEncodedAsGroup(T value, final Schema<T> schema) throws IOException { if(value == null) value = schema.newMessage(); schema.mergeFrom(this, value); if(!schema.isInitialized(value)) throw new UninitializedMessageException(value, schema); // handling is in #readFieldNumber checkLastTagWas(0); return value; }
/** Read an {@code sfixed32} field value from the internal buffer. */ public int readSFixed32() throws IOException { return readRawLittleEndian32(); }
/** Read an {@code sfixed64} field value from the internal buffer. */ public long readSFixed64() throws IOException { return readRawLittleEndian64(); }
@SuppressWarnings("unchecked") public <T> T mergeObject(T value, Schema<T> schema) throws IOException { if(messageReference) { // a reference. return (T)references.get(lastRef); } lastSchema = (Schema<Object>)schema; if(value == null) value = schema.newMessage(); references.add(value); input.mergeObject(value, this); return value; }
/** * Merges the {@code message} with the byte array using the given {@code schema}. */ public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) { try { final ByteArrayInput input = new ByteArrayInput(data, offset, length, true); final GraphByteArrayInput graphInput = new GraphByteArrayInput(input); schema.mergeFrom(graphInput, message); input.checkLastTagWas(0); } catch(ArrayIndexOutOfBoundsException ae) { throw new RuntimeException("Truncated.", ProtobufException.truncatedMessage(ae)); } catch (IOException e) { throw new RuntimeException("Reading from a byte array threw an IOException (should " + "never happen).",e); } }
/** Read a {@code fixed32} field value from the internal buffer. */ public int readFixed32() throws IOException { return readRawLittleEndian32(); }
/** Read a {@code fixed64} field value from the internal buffer. */ public long readFixed64() throws IOException { return readRawLittleEndian64(); }
final ByteArrayInput input = new ByteArrayInput(buffer.buffer, buffer.offset, size, true); try input.checkLastTagWas(0);
/** * Creates a protostuff pipe from a byte array. */ public static Pipe newPipe(byte[] data, int offset, int len) { final ByteArrayInput byteArrayInput = new ByteArrayInput(data, offset, len, true); return new Pipe() { protected Input begin(Pipe.Schema<?> pipeSchema) throws IOException { return byteArrayInput; } protected void end(Pipe.Schema<?> pipeSchema, Input input, boolean cleanupOnly) throws IOException { if(cleanupOnly) return; assert input == byteArrayInput; } }; }
/** Read a {@code float} field value from the internal buffer. */ public float readFloat() throws IOException { return Float.intBitsToFloat(readRawLittleEndian32()); }
/** Read a {@code double} field value from the internal buffer. */ public double readDouble() throws IOException { return Double.longBitsToDouble(readRawLittleEndian64()); }
final ByteArrayInput input = new ByteArrayInput(buffer.buffer, buffer.offset, size, false); try input.checkLastTagWas(0);