com.google.protobuf.ExtensionRegistryLite Java Examples

The following examples show how to use com.google.protobuf.ExtensionRegistryLite. 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: DesugarMethodAttribute.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
protected Attribute read(
    ClassReader classReader,
    int offset,
    int length,
    char[] charBuffer,
    int codeAttributeOffset,
    Label[] labels) {
  byte[] classAttrBytes = new byte[length];
  for (int i = 0; i < length; i++) {
    classAttrBytes[i] = (byte) classReader.readByte(i + offset);
  }
  try {
    return new DesugarMethodAttribute(
        DesugarMethodInfo.parseFrom(classAttrBytes, ExtensionRegistryLite.getEmptyRegistry()));
  } catch (InvalidProtocolBufferException e) {
    throw new IOError(e);
  }
}
 
Example #2
Source File: MapEntryLite.java    From jprotobuf with 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 File: MapEntryLite.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the entry.
 *
 * @param <K> the key type
 * @param <V> the value type
 * @param input the input
 * @param metadata the metadata
 * @param extensionRegistry the extension registry
 * @return the map. entry
 * @throws IOException Signals that an I/O exception has occurred.
 */
static <K, V> Map.Entry<K, V> parseEntry(CodedInputStream input, Metadata<K, V> metadata,
        ExtensionRegistryLite extensionRegistry) throws IOException {
    K key = metadata.defaultKey;
    V value = metadata.defaultValue;
    while (true) {
        int tag = input.readTag();
        if (tag == 0) {
            break;
        }
        if (tag == CodedConstant.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
            key = parseField(input, extensionRegistry, metadata.keyType, key);
        } else if (tag == CodedConstant.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
            value = parseField(input, extensionRegistry, metadata.valueType, value);
        } else {
            if (!input.skipField(tag)) {
                break;
            }
        }
    }
    return new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
}
 
Example #4
Source File: JRedisSerializationUtils.java    From springJredisCache with Apache License 2.0 5 votes vote down vote up
/**
 * @param bytes       字节数据
 * @param messageLite 序列化对应的类型
 * @return
 * @throws JRedisCacheException
 */
public static MessageLite protoDeserialize(byte[] bytes, MessageLite messageLite) throws JRedisCacheException {
    assert (bytes != null && messageLite != null);
    try {
        return messageLite.getParserForType().parsePartialFrom(CodedInputStream.newInstance(bytes), ExtensionRegistryLite.getEmptyRegistry());
    } catch (InvalidProtocolBufferException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #5
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testExtensionRegistryGetUnmodifiable() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  ExtensionRegistry registry2 = registry.getUnmodifiable();
  registry.add(Typical.myPrimitiveExtension);
  // Extension added to registry should be visible in registry2.
  Descriptor descriptor = TypicalData.getDescriptor();
  ExtensionRegistry.ExtensionInfo extensionInfo =
      registry2.findExtensionByNumber(descriptor, 1001);
  assertNotNull(extensionInfo);

  ExtensionRegistryLite registryLite = ExtensionRegistryLite.newInstance();
  ExtensionRegistryLite registryLite2 = registryLite.getUnmodifiable();
  assertNotNull(registryLite2);
}
 
Example #6
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMessageLiteInterface() throws Exception {
  ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance();
  TypicalData data = TypicalData.newBuilder().build();
  MessageLite messageLite = data;
  MessageLite.Builder builderLite = messageLite.newBuilderForType();
  messageLite.writeTo(new ByteArrayOutputStream());
  messageLite.writeDelimitedTo(new ByteArrayOutputStream());
  builderLite.mergeFrom(new ByteArrayInputStream(new byte[0]));
  builderLite.mergeFrom(new ByteArrayInputStream(new byte[0]), registry);
  builderLite.mergeDelimitedFrom(new ByteArrayInputStream(new byte[0]));
  builderLite.mergeDelimitedFrom(new ByteArrayInputStream(new byte[0]), registry);
  assertEquals(0, messageLite.getSerializedSize());
}
 
Example #7
Source File: ProtobufDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry) {
    if (prototype == null) {
        throw new NullPointerException("prototype");
    }
    this.prototype = prototype.getDefaultInstanceForType();
    this.extensionRegistry = extensionRegistry;
}
 
Example #8
Source File: DynamicMessageMarshaller.java    From grpc-swagger with MIT License 5 votes vote down vote up
@Override
public DynamicMessage parse(InputStream inputStream) {
    try {
        return DynamicMessage.newBuilder(messageDescriptor)
                .mergeFrom(inputStream, ExtensionRegistryLite.getEmptyRegistry())
                .build();
    } catch (IOException e) {
        throw new RuntimeException("Unable to merge from the supplied input stream", e);
    }
}
 
Example #9
Source File: DynamicMessageMarshaller.java    From karate-grpc with MIT License 5 votes vote down vote up
@Override
public DynamicMessage parse(InputStream inputStream) {
    try {
        return DynamicMessage.newBuilder(messageDescriptor)
                .mergeFrom(inputStream, ExtensionRegistryLite.getEmptyRegistry())
                .build();
    } catch (IOException e) {
        throw new RuntimeException("Can't merge from the supplied input stream", e);
    }
}
 
Example #10
Source File: ProtobufDecoder.java    From getty with Apache License 2.0 5 votes vote down vote up
public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry) {
    if (prototype == null) {
        throw new NullPointerException("prototype");
    }
    this.prototype = prototype.getDefaultInstanceForType();
    this.extensionRegistry = extensionRegistry;
}
 
Example #11
Source File: MapEntryLite.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an entry off of the input into the map. This helper avoids allocaton of a {@link MapEntryLite} by parsing
 * directly into the provided {@link MapFieldLite}.
 *
 * @param map the map
 * @param input the input
 * @param extensionRegistry the extension registry
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void parseInto(MapFieldLite<K, V> map, CodedInputStream input, ExtensionRegistryLite extensionRegistry)
        throws IOException {
    int length = input.readRawVarint32();
    final int oldLimit = input.pushLimit(length);
    K key = metadata.defaultKey;
    V value = metadata.defaultValue;

    while (true) {
        int tag = input.readTag();
        if (tag == 0) {
            break;
        }
        if (tag == CodedConstant.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
            key = parseField(input, extensionRegistry, metadata.keyType, key);
        } else if (tag == CodedConstant.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
            value = parseField(input, extensionRegistry, metadata.valueType, value);
        } else {
            if (!input.skipField(tag)) {
                break;
            }
        }
    }

    input.checkLastTagWas(0);
    input.popLimit(oldLimit);
    map.put(key, value);
}
 
Example #12
Source File: ProtobufDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry) {
    if (prototype == null) {
        throw new NullPointerException("prototype");
    }
    this.prototype = prototype.getDefaultInstanceForType();
    this.extensionRegistry = extensionRegistry;
}
 
Example #13
Source File: MapEntry.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #14
Source File: DynamicMessageMarshaller.java    From milkman with MIT License 5 votes vote down vote up
@Override
public DynamicMessage parse(InputStream inputStream) {
  try {
    return DynamicMessage.newBuilder(messageDescriptor)
        .mergeFrom(inputStream, ExtensionRegistryLite.getEmptyRegistry())
        .build();
  } catch (IOException e) {
    throw new RuntimeException("Unable to merge from the supplied input stream", e);
  }
}
 
Example #15
Source File: DynamicMessageMarshaller.java    From grpc-swagger with MIT License 5 votes vote down vote up
@Override
public DynamicMessage parse(InputStream inputStream) {
    try {
        return DynamicMessage.newBuilder(messageDescriptor)
                .mergeFrom(inputStream, ExtensionRegistryLite.getEmptyRegistry())
                .build();
    } catch (IOException e) {
        throw new RuntimeException("Unable to merge from the supplied input stream", e);
    }
}
 
Example #16
Source File: MessageLiteCodec.java    From bazel with 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 #17
Source File: StateTest.java    From zetasketch with Apache License 2.0 4 votes vote down vote up
private static AggregatorStateProto serializeAggregatorProto(State state)
    throws InvalidProtocolBufferException {
  ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance();
  HllplusUnique.registerAllExtensions(registry);
  return AggregatorStateProto.parseFrom(state.toByteArray(), registry);
}
 
Example #18
Source File: SizeLimitBypassingParser.java    From glowroot with Apache License 2.0 4 votes vote down vote up
@Override
public T parsePartialFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
        throws InvalidProtocolBufferException {
    input.setSizeLimit(Integer.MAX_VALUE);
    return parser.parsePartialFrom(input, extensionRegistry);
}
 
Example #19
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testExtensionRegistryGetEmpty() throws Exception {
  assertNotNull(ExtensionRegistry.getEmptyRegistry());
  assertNotNull(ExtensionRegistryLite.getEmptyRegistry());
}
 
Example #20
Source File: ProtobufDecoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry) {
    this(prototype, (ExtensionRegistryLite) extensionRegistry);
}
 
Example #21
Source File: ProtobufDecoder.java    From getty with Apache License 2.0 4 votes vote down vote up
public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry) {
    this(prototype, (ExtensionRegistryLite) extensionRegistry);
}
 
Example #22
Source File: ProtobufDecoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry) {
    this(prototype, (ExtensionRegistryLite) extensionRegistry);
}
 
Example #23
Source File: CodedDataInputStream.java    From fastjgame with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T readMessage(@Nonnull Parser<T> parser) throws IOException {
    return parser.parseFrom(codedInputStream, ExtensionRegistryLite.getEmptyRegistry());
}
 
Example #24
Source File: MapEntryLite.java    From jprotobuf with Apache License 2.0 2 votes vote down vote up
/**
 * Parses an entry off of the input as a {@link Map.Entry}. This helper requires an allocation so using
 * {@link #parseInto} is preferred if possible.
 *
 * @param bytes the bytes
 * @param extensionRegistry the extension registry
 * @return the map. entry
 * @throws IOException Signals that an I/O exception has occurred.
 */
public Map.Entry<K, V> parseEntry(ByteString bytes, ExtensionRegistryLite extensionRegistry) throws IOException {
    return parseEntry(bytes.newCodedInput(), metadata, extensionRegistry);
}
 
Example #25
Source File: ProtoLiteUtils.java    From grpc-nebula-java with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the global registry for proto marshalling shared across all servers and clients.
 *
 * <p>Warning:  This API will likely change over time.  It is not possible to have separate
 * registries per Process, Server, Channel, Service, or Method.  This is intentional until there
 * is a more appropriate API to set them.
 *
 * <p>Warning:  Do NOT modify the extension registry after setting it.  It is thread safe to call
 * {@link #setExtensionRegistry}, but not to modify the underlying object.
 *
 * <p>If you need custom parsing behavior for protos, you will need to make your own
 * {@code MethodDescriptor.Marshaller} for the time being.
 *
 * @since 1.0.0
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistryLite newRegistry) {
  globalRegistry = checkNotNull(newRegistry, "newRegistry");
}
 
Example #26
Source File: ProtoLiteUtils.java    From grpc-java with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the global registry for proto marshalling shared across all servers and clients.
 *
 * <p>Warning:  This API will likely change over time.  It is not possible to have separate
 * registries per Process, Server, Channel, Service, or Method.  This is intentional until there
 * is a more appropriate API to set them.
 *
 * <p>Warning:  Do NOT modify the extension registry after setting it.  It is thread safe to call
 * {@link #setExtensionRegistry}, but not to modify the underlying object.
 *
 * <p>If you need custom parsing behavior for protos, you will need to make your own
 * {@code MethodDescriptor.Marshaller} for the time being.
 *
 * @since 1.0.0
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistryLite newRegistry) {
  globalRegistry = checkNotNull(newRegistry, "newRegistry");
}