类com.google.protobuf.CodedInputStream源码实例Demo

下面列出了怎么用com.google.protobuf.CodedInputStream的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: jprotobuf   文件: MapEntryLite.java

/**
 * 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);
    }
}
 
源代码2 项目: zetasketch   文件: StateTest.java

/** 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});
}
 
源代码3 项目: zetasketch   文件: StateTest.java

@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);
}
 
源代码4 项目: bazel   文件: MethodCodec.java

@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);
  }
}
 
源代码5 项目: bazel   文件: NestedSetCodecWithStore.java

@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");
}
 
源代码6 项目: bazel   文件: Artifact.java

@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);
}
 
源代码7 项目: bazel   文件: ImmutableMapCodec.java

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;
}
 
源代码8 项目: bazel   文件: Root.java

@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);
}
 
源代码9 项目: hadoop   文件: FSImageLoader.java

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;
}
 

@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;
  }
}
 
源代码11 项目: bazel   文件: DeserializationContextTest.java

@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);
}
 
源代码12 项目: sctalk   文件: IMPacketDispatcher.java

/**
     * @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);
        }
    }
 
源代码13 项目: jprotobuf   文件: CodedConstant.java

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());
    }


}
 
源代码14 项目: tez   文件: RecoveryParser.java

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;
}
 
源代码15 项目: bazel   文件: UnsafeJdk9StringCodec.java

@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);
  }
}
 
源代码16 项目: bazel   文件: Artifact.java

@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);
}
 
源代码17 项目: sql-layer   文件: ProtobufReader.java

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());
    }
}
 
源代码18 项目: bazel   文件: EnumCodec.java

@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);
  }
}
 

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);
  }
}
 
源代码20 项目: bazel   文件: ObjectCodecTester.java

/** 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.
  }
}
 
源代码21 项目: bazel   文件: Package.java

@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);
  }
}
 
源代码22 项目: zetasketch   文件: StateTest.java

/** 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});
}
 
源代码23 项目: zetasketch   文件: StateTest.java

/** 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});
}
 
源代码24 项目: unitime   文件: ReplayLogTest.java

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;
}
 
源代码25 项目: bazel   文件: MessageLiteCodec.java

@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);
  }
}
 

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();
}
 

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));
}
 
源代码28 项目: bazel   文件: StringLiteral.java

@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);
}
 
源代码29 项目: bazel   文件: NativeAspectClass.java

@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);
}
 
源代码30 项目: gsc-core   文件: Message.java

public static CodedInputStream getCodedInputStream(byte[] data) {
    CodedInputStream codedInputStream = CodedInputStream.newInstance(data);
    if (isFilter()) {
        ReflectionUtils.setField(field, codedInputStream, true);
    }
    return codedInputStream;
}
 
 类所在包
 同包方法