com.google.protobuf.CodedInputStream Java Examples
The following examples show how to use
com.google.protobuf.CodedInputStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StateTest.java From zetasketch with Apache License 2.0 | 7 votes |
@Test public void parseUnknownField() throws IOException { // Create an aggregator state proto with an unknown field in the middle. ByteArrayOutputStream stream = new ByteArrayOutputStream(); CodedOutputStream coded = CodedOutputStream.newInstance(stream); coded.writeInt32(AggregatorStateProto.NUM_VALUES_FIELD_NUMBER, 42); coded.writeString(999, "foobar"); coded.writeInt32(AggregatorStateProto.ENCODING_VERSION_FIELD_NUMBER, 43); coded.flush(); // Check that we can parse the proto, despite the unknown field. State state = new State(); state.parse(CodedInputStream.newInstance(stream.toByteArray())); // Check that the fields before and after the unknown fields were correctly read. assertEquals(42, state.numValues); assertEquals(43, state.encodingVersion); }
Example #2
Source File: Root.java From bazel with Apache License 2.0 | 6 votes |
@Override public Root deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { if (codedIn.readBool()) { RootCodecDependencies codecDeps = context.getDependency(RootCodecDependencies.class); return codecDeps.likelyPopularRoot; } if (codedIn.readBool()) { Path path = context.deserialize(codedIn); return new PathRoot(path); } FileSystem fileSystem = context.deserialize(codedIn); return new AbsoluteRoot(fileSystem); }
Example #3
Source File: UnsafeJdk9StringCodec.java From bazel with Apache License 2.0 | 6 votes |
@Override public String deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { int length = codedIn.readInt32(); byte coder; if (length >= 0) { coder = 0; } else { coder = 1; length = -length; } byte[] value = codedIn.readRawBytes(length); try { return stringUnsafe.newInstance(value, coder); } catch (ReflectiveOperationException e) { throw new SerializationException( "Could not instantiate string: " + Arrays.toString(value) + ", " + coder, e); } }
Example #4
Source File: CodedConstant.java From jprotobuf with Apache License 2.0 | 6 votes |
public static <K, V> void putMapValue(CodedInputStream input, Map<K, V> map, com.google.protobuf.WireFormat.FieldType keyType, K defaultKey, com.google.protobuf.WireFormat.FieldType valueType, V defalutValue, EnumHandler<V> handler) throws IOException { com.baidu.bjf.remoting.protobuf.MapEntry<K, V> valuesDefaultEntry = com.baidu.bjf.remoting.protobuf.MapEntry .<K, V> newDefaultInstance(null, keyType, defaultKey, valueType, defalutValue); com.baidu.bjf.remoting.protobuf.MapEntry<K, V> values = input.readMessage(valuesDefaultEntry.getParserForType(), null); Object value = values.getValue(); if (handler != null) { V value1 = handler.handle((int) value); map.put(values.getKey(), value1); } else { map.put(values.getKey(), values.getValue()); } }
Example #5
Source File: RecoveryParser.java From tez with Apache License 2.0 | 6 votes |
public static List<HistoryEvent> parseDAGRecoveryFile(FSDataInputStream inputStream) throws IOException { List<HistoryEvent> historyEvents = new ArrayList<HistoryEvent>(); CodedInputStream codedInputStream = CodedInputStream.newInstance(inputStream); codedInputStream.setSizeLimit(Integer.MAX_VALUE); while (true) { HistoryEvent historyEvent = getNextEvent(codedInputStream); if (historyEvent == null) { LOG.info("Reached end of stream"); break; } LOG.debug("Read HistoryEvent, eventType={}, event={}", historyEvent.getEventType(), historyEvent); historyEvents.add(historyEvent); } return historyEvents; }
Example #6
Source File: NestedSetCodecWithStore.java From bazel with Apache License 2.0 | 6 votes |
@Override public NestedSet<?> deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { Order order = context.deserialize(codedIn); NestedSetSize nestedSetSize = NestedSetSize.values()[codedIn.readEnum()]; switch (nestedSetSize) { case EMPTY: return NestedSetBuilder.emptySet(order); case LEAF: Object contents = context.deserialize(codedIn); return intern(order, 1, contents); case NONLEAF: int depth = context.deserialize(codedIn); ByteString fingerprint = ByteString.copyFrom(codedIn.readByteArray()); return intern(order, depth, nestedSetStore.getContentsAndDeserialize(fingerprint, context)); } throw new IllegalStateException("NestedSet size " + nestedSetSize + " not known"); }
Example #7
Source File: MethodCodec.java From bazel with Apache License 2.0 | 6 votes |
@Override public Method deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { Class<?> clazz = context.deserialize(codedIn); String name = context.deserialize(codedIn); Class<?>[] parameters = new Class<?>[codedIn.readInt32()]; for (int i = 0; i < parameters.length; i++) { parameters[i] = context.deserialize(codedIn); } try { return clazz.getDeclaredMethod(name, parameters); } catch (NoSuchMethodException e) { throw new SerializationException( "Couldn't get method " + name + " in " + clazz + " with parameters " + Arrays.toString(parameters), e); } }
Example #8
Source File: Artifact.java From bazel with Apache License 2.0 | 6 votes |
@Override public DerivedArtifact deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { ArtifactRoot root = context.deserialize(codedIn); ActionLookupData generatingActionKey = context.deserialize(codedIn); DerivedArtifact artifact = new DerivedArtifact( root, validateAndGetRootExecPath(root, generatingActionKey, context, codedIn), generatingActionKey.getActionLookupKey(), /*contentBasedPath=*/ false); artifact.setGeneratingActionKey(generatingActionKey); return context .getDependency(ArtifactResolver.ArtifactResolverSupplier.class) .intern(artifact); }
Example #9
Source File: ImmutableMapCodec.java From bazel with Apache License 2.0 | 6 votes |
static <K, V, M extends ImmutableMap.Builder<K, V>> M deserializeEntries( M builder, int length, DeserializationContext context, CodedInputStream codedIn) throws IOException, SerializationException { for (int i = 0; i < length; i++) { K key = context.deserialize(codedIn); V value; try { value = context.deserialize(codedIn); } catch (SerializationException | IOException e) { throw SerializationException.propagate( String.format("Exception while deserializing value for key '%s'", key), e); } builder.put(key, value); } return builder; }
Example #10
Source File: Artifact.java From bazel with Apache License 2.0 | 6 votes |
@Override public SpecialArtifact deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { ArtifactRoot root = context.deserialize(codedIn); ActionLookupData generatingActionKey = context.deserialize(codedIn); SpecialArtifactType type = context.deserialize(codedIn); SpecialArtifact artifact = new SpecialArtifact( root, DerivedArtifactCodec.validateAndGetRootExecPath( root, generatingActionKey, context, codedIn), generatingActionKey.getActionLookupKey(), type); artifact.setGeneratingActionKey(generatingActionKey); return (SpecialArtifact) context.getDependency(ArtifactResolverSupplier.class).intern(artifact); }
Example #11
Source File: IMPacketDispatcher.java From sctalk with Apache License 2.0 | 6 votes |
/** * @param commandId * @param buffer * * 有没有更加优雅的方式 */ public static void loginPacketDispatcher(int commandId,CodedInputStream buffer){ try { switch (commandId) { // case IMBaseDefine.LoginCmdID.CID_LOGIN_RES_USERLOGIN_VALUE : // IMLogin.IMLoginRes imLoginRes = IMLogin.IMLoginRes.parseFrom(buffer); // IMLoginManager.instance().onRepMsgServerLogin(imLoginRes); // return; case IMBaseDefine.LoginCmdID.CID_LOGIN_RES_LOGINOUT_VALUE: IMLogin.IMLogoutRsp imLogoutRsp = IMLogin.IMLogoutRsp.parseFrom(buffer); IMLoginManager.instance().onRepLoginOut(imLogoutRsp); return; case IMBaseDefine.LoginCmdID.CID_LOGIN_KICK_USER_VALUE: IMLogin.IMKickUser imKickUser = IMLogin.IMKickUser.parseFrom(buffer); IMLoginManager.instance().onKickout(imKickUser); } } catch (IOException e) { logger.e("loginPacketDispatcher# error,cid:%d",commandId); } }
Example #12
Source File: StateTest.java From zetasketch with Apache License 2.0 | 6 votes |
/** Verify that the data is aliased when possible. */ @Test @SuppressWarnings("boxing") public void parseSparseData_AliasesArray() throws IOException { byte[] bytes = build(HyperLogLogPlusUniqueStateProto.newBuilder() .setSparseData(ByteString.copyFrom(new byte[] {1, 2, 3}))); CodedInputStream stream = CodedInputStream.newInstance(bytes); stream.enableAliasing(true); State state = new State(); state.parse(stream); // Preconditions. int idx = Bytes.indexOf(bytes, new byte[] {1, 2, 3}); assertThat(idx).isAtLeast(0); assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 2, 3}); // Modify the bytes, make sure that it is reflected in builder. bytes[idx + 1] = 4; assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 4, 3}); }
Example #13
Source File: DeserializationContextTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void descriptorDeserialize() throws Exception { ObjectCodecRegistry.CodecDescriptor codecDescriptor = Mockito.mock(ObjectCodecRegistry.CodecDescriptor.class); ObjectCodecRegistry registry = Mockito.mock(ObjectCodecRegistry.class); when(registry.getCodecDescriptorByTag(1)).thenReturn(codecDescriptor); CodedInputStream codedInputStream = Mockito.mock(CodedInputStream.class); when(codedInputStream.readSInt32()).thenReturn(1); DeserializationContext deserializationContext = new DeserializationContext(registry, ImmutableMap.of()); Object returnValue = new Object(); when(codecDescriptor.deserialize(deserializationContext, codedInputStream)) .thenReturn(returnValue); assertThat((Object) deserializationContext.deserialize(codedInputStream)) .isSameInstanceAs(returnValue); Mockito.verify(codedInputStream).readSInt32(); Mockito.verify(registry).getCodecDescriptorByTag(1); Mockito.verify(codecDescriptor).deserialize(deserializationContext, codedInputStream); }
Example #14
Source File: ProtobufTranslationImpl.java From calcite-avatica with Apache License 2.0 | 6 votes |
@Override public Request parseRequest(byte[] bytes) throws IOException { ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes); CodedInputStream inputStream = byteString.newCodedInput(); // Enable aliasing to avoid an extra copy to get at the serialized Request inside of the // WireMessage. inputStream.enableAliasing(true); WireMessage wireMsg = WireMessage.parseFrom(inputStream); String serializedMessageClassName = wireMsg.getName(); try { RequestTranslator translator = getParserForRequest(serializedMessageClassName); // The ByteString should be logical offsets into the original byte array return translator.transform(wireMsg.getWrappedMessage()); } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg)); } throw e; } }
Example #15
Source File: FSImageLoader.java From hadoop with Apache License 2.0 | 6 votes |
private static byte[][] loadINodeSection(InputStream in) throws IOException { FsImageProto.INodeSection s = FsImageProto.INodeSection .parseDelimitedFrom(in); LOG.info("Loading " + s.getNumInodes() + " inodes."); final byte[][] inodes = new byte[(int) s.getNumInodes()][]; for (int i = 0; i < s.getNumInodes(); ++i) { int size = CodedInputStream.readRawVarint32(in.read(), in); byte[] bytes = new byte[size]; IOUtils.readFully(in, bytes, 0, size); inodes[i] = bytes; } LOG.debug("Sorting inodes"); Arrays.sort(inodes, INODE_BYTES_COMPARATOR); LOG.debug("Finished sorting inodes"); return inodes; }
Example #16
Source File: ProtobufReader.java From sql-layer with GNU Affero General Public License v3.0 | 6 votes |
private void loadFromBuffer(ByteBuffer buffer) { final String MESSAGE_NAME = AISProtobuf.AkibanInformationSchema.getDescriptor().getFullName(); checkBuffer(buffer); final int serializedSize = buffer.getInt(); final int initialPos = buffer.position(); final int bufferSize = buffer.limit() - initialPos; if(bufferSize < serializedSize) { throw new ProtobufReadException(MESSAGE_NAME, "Buffer corrupt, serialized size greater than remaining"); } CodedInputStream codedInput = CodedInputStream.newInstance(buffer.array(), buffer.position(), Math.min(serializedSize, bufferSize)); try { pbAISBuilder.mergeFrom(codedInput, storageFormatRegistry.getExtensionRegistry()); // Successfully consumed, update byte buffer buffer.position(initialPos + serializedSize); } catch(IOException e) { // CodedInputStream really only throws InvalidProtocolBufferException, but declares IOE throw new ProtobufReadException(MESSAGE_NAME, e.getMessage()); } }
Example #17
Source File: MapEntryLite.java From jprotobuf with Apache License 2.0 | 6 votes |
/** * Parses the field. * * @param <T> the generic type * @param input the input * @param extensionRegistry the extension registry * @param type the type * @param value the value * @return the t * @throws IOException Signals that an I/O exception has occurred. */ @SuppressWarnings("unchecked") static <T> T parseField(CodedInputStream input, ExtensionRegistryLite extensionRegistry, WireFormat.FieldType type, T value) throws IOException { switch (type) { case MESSAGE: int length = input.readRawVarint32(); final int oldLimit = input.pushLimit(length); Codec<? extends Object> codec = ProtobufProxy.create(value.getClass()); T ret = (T) codec.decode(input.readRawBytes(length)); input.popLimit(oldLimit); return ret; case ENUM: return (T) (java.lang.Integer) input.readEnum(); case GROUP: throw new RuntimeException("Groups are not allowed in maps."); default: return (T) CodedConstant.readPrimitiveField(input, type, true); } }
Example #18
Source File: HashSetCodec.java From bazel with Apache License 2.0 | 5 votes |
@Override public LinkedHashSet<E> deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { int size = codedIn.readInt32(); LinkedHashSet<E> set = Sets.newLinkedHashSetWithExpectedSize(size); for (int i = 0; i < size; i++) { set.add(context.deserialize(codedIn)); } return set; }
Example #19
Source File: Memoizer.java From bazel with Apache License 2.0 | 5 votes |
private <T> T deserializeMemoBeforeContent( DeserializationContext context, ObjectCodec<T> codec, CodedInputStream codedIn) throws SerializationException, IOException { this.tagForMemoizedBefore = codedIn.readInt32(); T value = castedDeserialize(context, codec, codedIn); Object initial = memoizedBeforeStackForSanityChecking.removeLast(); if (value != initial) { // This indicates a bug in the particular codec subclass. throw new SerializationException( String.format( "codec did not return the initial instance: %s but was %s with codec %s", value, initial, codec)); } return value; }
Example #20
Source File: ImmutableListCodec.java From bazel with Apache License 2.0 | 5 votes |
@Override public ImmutableList<T> deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { int length = codedIn.readInt32(); if (length < 0) { throw new SerializationException("Expected non-negative length: " + length); } ImmutableList.Builder<T> builder = ImmutableList.builder(); for (int i = 0; i < length; i++) { builder.add(codec.deserialize(context, codedIn)); } return builder.build(); }
Example #21
Source File: XProtocolDecoder.java From FoxTelem with GNU General Public License v3.0 | 5 votes |
@Override public <T> T decodeByteArray(byte[] bytes, int offset, int length, ValueFactory<T> vf) { try { CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length); // c.f. Streaming_command_delegate::get_string() int size = inputStream.getBytesUntilLimit(); size--; // for null terminator return vf.createFromBytes(inputStream.readRawBytes(size), 0, size); } catch (IOException e) { throw new DataReadException(e); } }
Example #22
Source File: Optional8Codec.java From bazel with Apache License 2.0 | 5 votes |
@Override public Optional<?> deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { if (!codedIn.readBool()) { return Optional.empty(); } return Optional.of(context.deserialize(codedIn)); }
Example #23
Source File: TaskAttemptStartedEvent.java From tez with Apache License 2.0 | 5 votes |
@Override public void fromProtoStream(CodedInputStream inputStream) throws IOException { TaskAttemptStartedProto proto = inputStream.readMessage(TaskAttemptStartedProto.PARSER, null); if (proto == null) { throw new IOException("No data found in stream"); } fromProto(proto); }
Example #24
Source File: AMLaunchedEvent.java From tez with Apache License 2.0 | 5 votes |
@Override public void fromProtoStream(CodedInputStream inputStream) throws IOException { AMLaunchedProto proto = inputStream.readMessage(AMLaunchedProto.PARSER, null); if (proto == null) { throw new IOException("No data found in stream"); } fromProto(proto); }
Example #25
Source File: VertexGroupCommitStartedEvent.java From tez with Apache License 2.0 | 5 votes |
@Override public void fromProtoStream(CodedInputStream inputStream) throws IOException { VertexGroupCommitStartedProto proto = inputStream.readMessage(VertexGroupCommitStartedProto.PARSER, null); if (proto == null) { throw new IOException("No data found in stream"); } fromProto(proto); }
Example #26
Source File: SingletonCodec.java From bazel with Apache License 2.0 | 5 votes |
@Override public T deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { // Get ByteBuffer instead of raw bytes, as it may be a direct view of the data and not a copy, // which is much more efficient. ByteBuffer readMnemonic = codedIn.readByteBuffer(); if (!bytesEqual(mnemonic, readMnemonic)) { throw new SerializationException( "Failed to decode singleton " + value + " expected " + Arrays.toString(mnemonic)); } return value; }
Example #27
Source File: MapEntry.java From jprotobuf with Apache License 2.0 | 5 votes |
/** * Instantiates a new metadata. * * @param descriptor the descriptor * @param defaultInstance the default instance * @param keyType the key type * @param valueType the value type */ public Metadata(Descriptor descriptor, MapEntry<K, V> defaultInstance, WireFormat.FieldType keyType, WireFormat.FieldType valueType) { super(keyType, defaultInstance.key, valueType, defaultInstance.value); this.descriptor = descriptor; this.parser = new AbstractParser<MapEntry<K, V>>() { @Override public MapEntry<K, V> parsePartialFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { return new MapEntry<K, V>(Metadata.this, input, extensionRegistry); } }; }
Example #28
Source File: IMPacketDispatcher.java From sctalk with Apache License 2.0 | 5 votes |
public static void buddyPacketDispatcher(int commandId,CodedInputStream buffer){ try { switch (commandId) { case IMBaseDefine.BuddyListCmdID.CID_BUDDY_LIST_ALL_USER_RESPONSE_VALUE: IMBuddy.IMAllUserRsp imAllUserRsp = IMBuddy.IMAllUserRsp.parseFrom(buffer); IMContactManager.instance().onRepAllUsers(imAllUserRsp); return; case IMBaseDefine.BuddyListCmdID.CID_BUDDY_LIST_USER_INFO_RESPONSE_VALUE: IMBuddy.IMUsersInfoRsp imUsersInfoRsp = IMBuddy.IMUsersInfoRsp.parseFrom(buffer); IMContactManager.instance().onRepDetailUsers(imUsersInfoRsp); return; case IMBaseDefine.BuddyListCmdID.CID_BUDDY_LIST_RECENT_CONTACT_SESSION_RESPONSE_VALUE: IMBuddy.IMRecentContactSessionRsp recentContactSessionRsp = IMBuddy.IMRecentContactSessionRsp.parseFrom(buffer); IMSessionManager.instance().onRepRecentContacts(recentContactSessionRsp); return; case IMBaseDefine.BuddyListCmdID.CID_BUDDY_LIST_REMOVE_SESSION_RES_VALUE: IMBuddy.IMRemoveSessionRsp removeSessionRsp = IMBuddy.IMRemoveSessionRsp.parseFrom(buffer); IMSessionManager.instance().onRepRemoveSession(removeSessionRsp); return; case IMBaseDefine.BuddyListCmdID.CID_BUDDY_LIST_PC_LOGIN_STATUS_NOTIFY_VALUE: IMBuddy.IMPCLoginStatusNotify statusNotify = IMBuddy.IMPCLoginStatusNotify.parseFrom(buffer); IMLoginManager.instance().onLoginStatusNotify(statusNotify); return; case IMBaseDefine.BuddyListCmdID.CID_BUDDY_LIST_DEPARTMENT_RESPONSE_VALUE: IMBuddy.IMDepartmentRsp departmentRsp = IMBuddy.IMDepartmentRsp.parseFrom(buffer); IMContactManager.instance().onRepDepartment(departmentRsp); return; } } catch (IOException e) { logger.e("buddyPacketDispatcher# error,cid:%d",commandId); } }
Example #29
Source File: HashMapCodec.java From bazel with Apache License 2.0 | 5 votes |
@Override public LinkedHashMap<K, V> deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { int size = codedIn.readInt32(); // Load factor is 0.75, so we need an initial capacity of 4/3 actual size to avoid rehashing. LinkedHashMap<K, V> result = new LinkedHashMap<>(4 * size / 3); for (int i = 0; i < size; i++) { result.put(context.deserialize(codedIn), context.deserialize(codedIn)); } return result; }
Example #30
Source File: DelimitedProtobufHandler.java From InflatableDonkey with MIT License | 5 votes |
List<T> apply(InputStream in) throws IOException { List<T> list = new ArrayList<>(); CodedInputStream stream = CodedInputStream.newInstance(in); int size; while (!stream.isAtEnd() && (size = stream.readRawVarint32()) != 0) { ByteArrayInputStream delimited = new ByteArrayInputStream(stream.readRawBytes(size)); list.add(parse.apply(delimited)); } return list; }