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 Project: zetasketch   Author: google   File: StateTest.java    License: Apache License 2.0 7 votes vote down vote up
@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 Project: jprotobuf   Author: jhunters   File: MapEntryLite.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 #3
Source Project: zetasketch   Author: google   File: StateTest.java    License: Apache License 2.0 6 votes vote down vote up
/** 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 #4
Source Project: bazel   Author: bazelbuild   File: MethodCodec.java    License: Apache License 2.0 6 votes vote down vote up
@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 #5
Source Project: bazel   Author: bazelbuild   File: NestedSetCodecWithStore.java    License: Apache License 2.0 6 votes vote down vote up
@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 #6
Source Project: bazel   Author: bazelbuild   File: Artifact.java    License: Apache License 2.0 6 votes vote down vote up
@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 #7
Source Project: bazel   Author: bazelbuild   File: ImmutableMapCodec.java    License: Apache License 2.0 6 votes vote down vote up
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 #8
Source Project: bazel   Author: bazelbuild   File: Root.java    License: Apache License 2.0 6 votes vote down vote up
@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 #9
Source Project: hadoop   Author: naver   File: FSImageLoader.java    License: Apache License 2.0 6 votes vote down vote up
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 #10
Source Project: calcite-avatica   Author: apache   File: ProtobufTranslationImpl.java    License: Apache License 2.0 6 votes vote down vote up
@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 #11
Source Project: bazel   Author: bazelbuild   File: DeserializationContextTest.java    License: Apache License 2.0 6 votes vote down vote up
@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 #12
Source Project: sctalk   Author: ccfish86   File: IMPacketDispatcher.java    License: Apache License 2.0 6 votes vote down vote up
/**
     * @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 #13
Source Project: jprotobuf   Author: jhunters   File: CodedConstant.java    License: Apache License 2.0 6 votes vote down vote up
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 #14
Source Project: tez   Author: apache   File: RecoveryParser.java    License: Apache License 2.0 6 votes vote down vote up
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 #15
Source Project: bazel   Author: bazelbuild   File: UnsafeJdk9StringCodec.java    License: Apache License 2.0 6 votes vote down vote up
@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 #16
Source Project: bazel   Author: bazelbuild   File: Artifact.java    License: Apache License 2.0 6 votes vote down vote up
@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 #17
Source Project: sql-layer   Author: jaytaylor   File: ProtobufReader.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
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 #18
Source Project: bazel   Author: bazelbuild   File: EnumCodec.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public T deserialize(DeserializationContext context, CodedInputStream codedIn)
    throws SerializationException, IOException {
  int ordinal = codedIn.readEnum();
  try {
    return values.get(ordinal);
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new SerializationException(
        "Invalid ordinal for " + enumClass.getName() + " enum: " + ordinal, e);
  }
}
 
Example #19
Source Project: tez   Author: apache   File: RecoveryServiceWithEventHandlingHook.java    License: Apache License 2.0 5 votes vote down vote up
private HistoryEvent decodeHistoryEvent(String eventClass, String base64)
    throws IOException {
  CodedInputStream in = CodedInputStream.newInstance(Base64.decodeBase64(base64));
  try {
    HistoryEvent event = ReflectionUtils.createClazzInstance(eventClass);
    event.fromProtoStream(in);
    return event;
  } catch (TezReflectionException e) {
    throw new IOException(e);
  }
}
 
Example #20
Source Project: bazel   Author: bazelbuild   File: ObjectCodecTester.java    License: Apache License 2.0 5 votes vote down vote up
/** Runs junk-data recognition tests. */
void testDeserializeJunkData() {
  try {
    underTest.deserialize(
        readContext, CodedInputStream.newInstance("junk".getBytes(StandardCharsets.UTF_8)));
    fail("Expected exception");
  } catch (SerializationException | IOException e) {
    // Expected.
  }
}
 
Example #21
Source Project: bazel   Author: bazelbuild   File: Package.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Package deserialize(
    DeserializationContext context,
    CodedInputStream codedIn)
    throws SerializationException, IOException {
  PackageCodecDependencies codecDeps = context.getDependency(PackageCodecDependencies.class);
  try {
    return codecDeps.getPackageSerializer().deserialize(context, codedIn);
  } catch (InterruptedException e) {
    throw new IllegalStateException(
        "Unexpected InterruptedException during Package deserialization", e);
  }
}
 
Example #22
Source Project: zetasketch   Author: google   File: StateTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Verifies that the data can be parsed also when direct aliasing is not possible. */
@Test
public void parseData_ParsesStream() throws IOException {
  byte[] bytes = build(HyperLogLogPlusUniqueStateProto.newBuilder()
      .setData(ByteString.copyFrom(new byte[] {1, 2, 3})));
  State state = new State();
  state.parse(CodedInputStream.newInstance(new ByteArrayInputStream(bytes)));

  assertThat(state.data.toByteArray()).isEqualTo(new byte[] {1, 2, 3});
}
 
Example #23
Source Project: zetasketch   Author: google   File: StateTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Verifies that the sparse data can be parsed also when aliasing is not possible. */
@Test
public void parseSparseData_ParsesStream() throws IOException {
  byte[] bytes = build(HyperLogLogPlusUniqueStateProto.newBuilder()
      .setSparseData(ByteString.copyFrom(new byte[] {1, 2, 3})));
  State state = new State();
  state.parse(CodedInputStream.newInstance(new ByteArrayInputStream(bytes)));

  assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 2, 3});
}
 
Example #24
Source Project: unitime   Author: UniTime   File: ReplayLogTest.java    License: Apache License 2.0 5 votes vote down vote up
private OnlineSectioningLog.ExportedLog readLog(CodedInputStream cin) throws IOException {
	if (cin.isAtEnd()) return null;
	int size = cin.readInt32();
	int limit = cin.pushLimit(size);
	OnlineSectioningLog.ExportedLog ret = OnlineSectioningLog.ExportedLog.parseFrom(cin);
	cin.popLimit(limit);
	cin.resetSizeCounter();
	return ret;
}
 
Example #25
Source Project: bazel   Author: bazelbuild   File: MessageLiteCodec.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public MessageLite deserialize(DeserializationContext unusedContext, CodedInputStream codedIn)
    throws IOException, SerializationException {
  // Don't hold on to full byte array when constructing this proto.
  codedIn.enableAliasing(false);
  try {
    MessageLite.Builder builder = builderSupplier.get();
    codedIn.readMessage(builder, ExtensionRegistryLite.getEmptyRegistry());
    return builder.build();
  } catch (InvalidProtocolBufferException e) {
    throw new SerializationException("Failed to parse proto of type " + type, e);
  } finally {
    codedIn.enableAliasing(true);
  }
}
 
Example #26
Source Project: fastjgame   Author: hl845740757   File: ProtoBufSerializePerformanceTest.java    License: Apache License 2.0 5 votes vote down vote up
private static Class<?> readType(CodedInputStream inputStream) throws IOException {
    final byte nameSpace = inputStream.readRawByte();
    final int classId = inputStream.readFixed32();
    final TypeModel typeModel = TYPE_MODEL_MAPPER.ofId(new TypeId(nameSpace, classId));
    assert null != typeModel;
    return typeModel.type();
}
 
Example #27
Source Project: fastjgame   Author: hl845740757   File: ProtoBufSerializePerformanceTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void codecTest(Message msg, int loopTimes, Map<Class<?>, Parser<? extends Message>> parserMap) throws IOException {
    final long start = System.currentTimeMillis();
    for (int index = 0; index < loopTimes; index++) {
        // 这里需要简单模拟下解码过程
        final CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(buffer);
        writeTypeId(msg, codedOutputStream);
        msg.writeTo(codedOutputStream);

        final CodedInputStream inputStream = CodedInputStream.newInstance(buffer, 0, codedOutputStream.getTotalBytesWritten());
        final Class<?> messageClass = readType(inputStream);
        final Parser<?> parser = parserMap.get(messageClass);
        final Object decodeMsg = parser.parseFrom(inputStream);
    }
    System.out.println("codec " + loopTimes + " times cost timeMs " + (System.currentTimeMillis() - start));
}
 
Example #28
Source Project: bazel   Author: bazelbuild   File: StringLiteral.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public StringLiteral deserialize(DeserializationContext context, CodedInputStream in)
    throws SerializationException, IOException {
  String value =
      context.deserializeWithAdHocMemoizationStrategy(in, MemoizationStrategy.MEMOIZE_AFTER);
  int startOffset = in.readInt32();
  int endOffset = in.readInt32();
  FileLocations locs = context.deserialize(in);
  return new StringLiteral(locs, startOffset, value, endOffset);
}
 
Example #29
Source Project: bazel   Author: bazelbuild   File: NativeAspectClass.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public NativeAspectClass deserialize(DeserializationContext context, CodedInputStream codedIn)
    throws SerializationException, IOException {
  String aspectKey = context.deserialize(codedIn);
  return Preconditions.checkNotNull(
      Preconditions.checkNotNull(context.getDependency(RuleClassProvider.class), aspectKey)
          .getNativeAspectClass(aspectKey),
      aspectKey);
}
 
Example #30
Source Project: gsc-core   Author: gscsocial   File: Message.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
public static CodedInputStream getCodedInputStream(byte[] data) {
    CodedInputStream codedInputStream = CodedInputStream.newInstance(data);
    if (isFilter()) {
        ReflectionUtils.setField(field, codedInputStream, true);
    }
    return codedInputStream;
}