/** * Attempts to extract a protocol buffer from the specified extra. * @throws MalformedDataException if the intent is null, the extra is missing or not a byte * array, or the protocol buffer could not be parsed. */ @NonNull public static <T extends MessageLite> T extract( @NonNull String extraName, @NonNull Parser<T> protoParser, @NonNull String failureDescription, @Nullable Intent intent) throws MalformedDataException { if (intent == null) { throw new MalformedDataException(failureDescription); } byte[] protoBytes = intent.getByteArrayExtra(extraName); if (protoBytes == null) { throw new MalformedDataException(failureDescription); } try { return protoParser.parseFrom(protoBytes); } catch (IOException ex) { throw new MalformedDataException(failureDescription, ex); } }
public Map<MessageType, Parser<com.google.protobuf.Message>> populateParsersFromClasspath() { Map<MessageType, Parser<com.google.protobuf.Message>> parsers = new HashMap<>(); List<Class<? extends com.google.protobuf.GeneratedMessageV3>> foundProtoMessages = new ArrayList<>(); new FastClasspathScanner() .matchSubclassesOf(com.google.protobuf.GeneratedMessageV3.class, matchingClass -> foundProtoMessages.add(matchingClass)).scan(); // This algorithm adds parsers for all protobuf messages in the classpath including base types such as com.google.protobuf.DoubleValue. for (Class<? extends com.google.protobuf.GeneratedMessageV3> clazz : foundProtoMessages) { try { java.lang.reflect.Method method = clazz.getMethod("parser"); // static method, no arguments @SuppressWarnings("unchecked") Parser<com.google.protobuf.Message> parser = (Parser<com.google.protobuf.Message>) method.invoke(null, (Object[]) null); // static method, no arguments parsers.put(MessageType.of(clazz), parser); // too noisy: logger.debug("Added parser for protobuf type {}", clazz.getTypeName()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) { // too noisy: logger.debug("Ignoring protobuf type {} as we cannot invoke static method parse().", clazz.getTypeName()); } } return parsers; }
private void lectura() { final byte[] buf = new byte[65536]; Parser<Protos.Respuesta> parser = Protos.Respuesta.parser(); try { while (pending-- > 0) { Protos.Respuesta resp = Utils.read(buf, bin, parser); if (resp.getRcode() == 0) { log.debug("OK conf {}", resp.getConfirmacion()); } else { log.debug("ERR {}", resp.getError()); } if (timers[resp.getId()] != null) { timers[resp.getId()].stop(); } } } catch (IOException ex) { log.error("Leyendo de socket", ex); } }
/** * Reads a list of protos, using the provided parser, from the provided input stream. * @throws IOException if the proto list could not be parsed. */ public static <T extends MessageLite> List<T> readMessageList( InputStream stream, Parser<T> parser) throws IOException { DataInputStream dis = new DataInputStream(stream); int messageCount = dis.readInt(); ArrayList<T> messages = new ArrayList<>(messageCount); for (int i = 0; i < messageCount; i++) { messages.add(parser.parseDelimitedFrom(stream)); } return messages; }
public static ScanMetrics toScanMetrics(final byte[] bytes) { Parser<MapReduceProtos.ScanMetrics> parser = MapReduceProtos.ScanMetrics.PARSER; MapReduceProtos.ScanMetrics pScanMetrics = null; try { pScanMetrics = parser.parseFrom(bytes); } catch (InvalidProtocolBufferException e) { //Ignored there are just no key values to add. } ScanMetrics scanMetrics = new ScanMetrics(); if (pScanMetrics != null) { for (HBaseProtos.NameInt64Pair pair : pScanMetrics.getMetricsList()) { if (pair.hasName() && pair.hasValue()) { scanMetrics.setCounter(pair.getName(), pair.getValue()); } } } return scanMetrics; }
/** * Subscribes to the entity states of the given type. * * <p>The method returns a {@link LiveData} of map (string ID -> entity state). The ID is * the {@linkplain io.spine.Identifier#toString(Object) string representation} of * the corresponding entity ID. * * <p>Currently, the element removal is not supported. If a {@link DocumentChange} of type other * than {@link DocumentChange.Type#ADDED ADDED} or {@link DocumentChange.Type#MODIFIED MODIFIED} * is encountered, an {@link UnsupportedOperationException} is thrown. * * @param targetType the class of the entity to subscribe to * @param <T> the type of the entity to subscribe to * @return an instance of {@link LiveData} for the observers to subscribe to */ public <T extends Message> LiveData<Map<String, T>> subscribeTo(Class<T> targetType) { checkNotNull(targetType); final CollectionReference targetCollection = collectionFor(targetType); final MutableLiveData<Map<String, T>> result = new MutableLiveData<>(); targetCollection.addSnapshotListener((documentSnapshots, error) -> { if (error != null) { final String errorMsg = format( "Error encountered while listening for the %s state updates.", targetType ); Log.e(TAG, errorMsg, error); } else { final Parser<T> parser = getParserFor(targetType); for (DocumentChange change : documentSnapshots.getDocumentChanges()) { deliverUpdate(change, result, parser); } } }); return result; }
/** * Delivers the entity state update represented by the given {@link DocumentChange} to * the observers of the given {@link LiveData}. * * @param change the Firestore document change * @param destination the {@link LiveData} publishing the update * @param parser the {@link Parser} for the target entity state type * @param <T> the entity state type */ private static <T extends Message> void deliverUpdate(DocumentChange change, MutableLiveData<Map<String, T>> destination, Parser<T> parser) { final DocumentChange.Type type = change.getType(); final Map<String, T> currentData = destination.getValue(); final Map<String, T> newData = currentData == null ? newHashMap() : newHashMap(currentData); final DocumentSnapshot doc = change.getDocument(); final String id = parseMessageId(doc); final T newMessage = parseMessage(doc, parser); if (type == ADDED || type == MODIFIED) { newData.put(id, newMessage); } else { throw newIllegalArgumentException("Unexpected document change: %s", type.toString()); } destination.postValue(newData); }
private static <T> Optional<T> readFirstMatching(Path path, Parser<T> parser, Predicate<T> predicate) { try (InputStream inputStream = Files.newInputStream(path)) { while (true) { T message = parser.parseDelimitedFrom(inputStream); if (message == null) { break; } if (predicate.test(message)) { return Optional.of(message); } } } catch (IOException e) { throw new IllegalStateException("unexpected error while parsing protobuf file: " + path, e); } return Optional.empty(); }
public Subscriber() { Parser<T> parser = null; try { Class<?> cl = getClass(); while (!Subscriber.class.equals(cl.getSuperclass())) { // case of multiple inheritance, we are trying to get the // first available generic info if (cl.getGenericSuperclass() instanceof ParameterizedType) { break; } cl = cl.getSuperclass(); } Class<T> type = ((Class<T>) ((ParameterizedType) cl.getGenericSuperclass()) .getActualTypeArguments()[0]); parser = (Parser<T>) type.getDeclaredField("PARSER").get(null); } catch (Exception e) { System.out.println("Error: callback creation failed"); e.printStackTrace(); } this.parser = parser; }
@Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { if (!(type instanceof Class<?>)) { return null; } Class<?> c = (Class<?>) type; if (!MessageLite.class.isAssignableFrom(c)) { return null; } Parser<MessageLite> parser; try { Field field = c.getDeclaredField("PARSER"); //noinspection unchecked parser = (Parser<MessageLite>) field.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { throw new IllegalArgumentException( "Found a protobuf message but " + c.getName() + " had no PARSER field."); } return new ProtoResponseBodyConverter<>(parser, registry); }
/** * De-serializes a protobuf from the given buffer. * <p> * The protobuf is assumed to be prefixed by a varint indicating its size. * @param buf The buffer to de-serialize the protobuf from. * @param parser The protobuf parser to use for this type of protobuf. * @return An instance of the de-serialized type. * @throws InvalidResponseException if the buffer contained an invalid * protobuf that couldn't be de-serialized. */ static <T> T readProtobuf(final ChannelBuffer buf, final Parser<T> parser) { final int length = HBaseRpc.readProtoBufVarint(buf); HBaseRpc.checkArrayLength(buf, length); final byte[] payload; final int offset; if (buf.hasArray()) { // Zero copy. payload = buf.array(); offset = buf.arrayOffset() + buf.readerIndex(); buf.readerIndex(buf.readerIndex() + length); } else { // We have to copy the entire payload out of the buffer :( payload = new byte[length]; buf.readBytes(payload); offset = 0; } try { return parser.parseFrom(payload, offset, length); } catch (InvalidProtocolBufferException e) { final String msg = "Invalid RPC response: length=" + length + ", payload=" + Bytes.pretty(payload); LOG.error("Invalid RPC from buffer: " + buf); throw new InvalidResponseException(msg, e); } }
@Override public Converter<ResponseData, ?> fromResponseBody(Type type, Annotation[] annotations) { if (!(type instanceof Class<?>)) { return null; } Class<?> c = (Class<?>) type; if (!MessageLite.class.isAssignableFrom(c)) { return null; } Parser<MessageLite> parser; try { Field field = c.getDeclaredField("PARSER"); //noinspection unchecked parser = (Parser<MessageLite>) field.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { throw new IllegalArgumentException( "Found a protobuf message but " + c.getName() + " had no PARSER field."); } return new ProtoResponseBodyConverter<>(parser); }
public static <T extends GeneratedMessage> MessageInputStream<T> createBinaryStream(final InputStream is, String msgType) throws IOException { final Registry reg = Registry.getInstance(); final ExtensionRegistry extReg = reg.getExtensionRegistry(); final GeneratedMessage msg = reg.getInstanceForType(msgType); if (msg == null) { throw new RuntimeException("Type " + msgType + " not found."); } final Parser parser = msg.getParserForType(); return new MessageInputStream<T>() { { next = readNext(); } @Override protected T readNext() throws IOException { if (is.available() > 0) { return (T) parser.parseDelimitedFrom(is, extReg); } return null; } }; }
public static <T extends /*@NonNull*/ AbstractMessage> List<T> parseDelimitedFrom( @Nullable ByteBuffer byteBuf, Parser<T> parser) throws IOException { if (byteBuf == null) { return ImmutableList.of(); } SizeLimitBypassingParser<T> sizeLimitBypassingParser = new SizeLimitBypassingParser<>(parser); List<T> messages = Lists.newArrayList(); try (InputStream input = new ByteBufferInputStream(byteBuf)) { T message; while ((message = sizeLimitBypassingParser.parseDelimitedFrom(input)) != null) { messages.add(message); } } return messages; }
@Override public Converter<NetworkResponse, ?> fromResponse(Type type, Annotation[] annotations) { if (!(type instanceof Class<?>)) { return null; } Class<?> c = (Class<?>) type; if (!MessageLite.class.isAssignableFrom(c)) { return null; } Parser<MessageLite> parser; try { Field field = c.getDeclaredField("PARSER"); //noinspection unchecked parser = (Parser<MessageLite>) field.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { throw new IllegalArgumentException( "Found a protobuf message but " + c.getName() + " had no PARSER field."); } return new ProtoResponseConverter<>(parser); }
@SuppressWarnings("unchecked") public static <T extends MessageLite> T decodeNoLengthPrefix(ByteBuf msgBytes, Class<T> messageClass) throws Exception { if(msgBytes==null){ msgBytes=Unpooled.EMPTY_BUFFER; } final byte[] array; final int offset; final int length = msgBytes.readableBytes(); if (msgBytes.hasArray()) { array = msgBytes.array(); offset = msgBytes.arrayOffset() + msgBytes.readerIndex(); } else { array = new byte[length]; msgBytes.getBytes(msgBytes.readerIndex(), array, 0, length); offset = 0; } com.google.protobuf.Parser<T> PARSER=(Parser<T>) messageClass.getField("PARSER").get(null); return PARSER.parseFrom(array, offset, length); }
private Message<? extends com.google.protobuf.Message> parseMessage() { Envelope envelope = null; try { envelope = Envelope.parseFrom(record.value()); } catch (InvalidProtocolBufferException parseError) { markAsConsumed(record.offset()); parsingFailed(envelope, parseError); return null; } try { MessageType type = new MessageType(envelope.getMessageType()); Parser<com.google.protobuf.Message> parser = typeDictionary.parserFor(type); if (parser == null) { throw new UnknownMessageTypeException(type); } com.google.protobuf.Message innerMessage = parser.parseFrom(envelope.getInnerMessage()); return Messages.fromKafka(innerMessage, envelope, record); } catch (InvalidProtocolBufferException | UnknownMessageTypeException unrecoverableParsingError) { markAsConsumed(record.offset()); parsingFailed(envelope, unrecoverableParsingError); return null; } }
public static <T> T get(ByteBuf pBody, Parser<T> parser) throws RpcException{ try { ByteBufInputStream is = new ByteBufInputStream(pBody); return parser.parseFrom(is); } catch (InvalidProtocolBufferException e) { throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e); } }
public static <Proto> Proto read(byte[] buf, InputStream stream, Parser<Proto> parser) throws IOException { int leidos = stream.read(buf, 0, 2); if (leidos == 2) { final int tam = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff); leidos = stream.read(buf, 0, tam); if (leidos > 0) { return parser.parseFrom(buf, 0, leidos); } } return null; }
/** * Reads a list of protos, using the provided parser, from the provided {@link ByteString}. * @throws IOException if the proto list could not be parsed. */ public static <T extends MessageLite> List<T> readMessageList( ByteString bytes, Parser<T> parser) throws IOException { InputStream stream = bytes.newInput(); return readMessageList(stream, parser); }
/** * Reads a list of protos, using the provided parser, from the provided byte array. * @throws IOException if the proto list could not be parsed. */ public static <T extends MessageLite> List<T> readMessageList( byte[] bytes, Parser<T> parser) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); return readMessageList(bais, parser); }
/** * Serialize to JSON the message encoded in binary protobuf format in {@code encodedMessage}. Used * to write the content of type wrappers in {@link com.google.protobuf.Any}. */ void writeValue(ByteString encodedMessage, JsonGenerator gen) throws IOException { // getParserForTYpe for T returns Parser<T> @SuppressWarnings("unchecked") Parser<T> parser = (Parser<T>) prototype.getParserForType(); writeValue(parser.parseFrom(encodedMessage), gen); }
/** * Serialize to JSON the message encoded in binary protobuf format in {@code encodedMessage} * without starting or ending a new JSON object. Used to write the content of normal messages in * {@link com.google.protobuf.Any}, which will take care of creating the JSON object to store the * type url. */ void doWrite(ByteString encodedMessage, JsonGenerator gen) throws IOException { // getParserForTYpe for T returns Parser<T> @SuppressWarnings("unchecked") Parser<T> parser = (Parser<T>) prototype.getParserForType(); doWrite(parser.parseFrom(encodedMessage), gen); }
public static <T> T get(ByteBuf pBody, Parser<T> parser) throws RpcException { try { ByteBufInputStream is = new ByteBufInputStream(pBody); return parser.parseFrom(is); } catch (InvalidProtocolBufferException e) { throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e); } }
public static <T> T get(byte[] pBody, Parser<T> parser) throws RpcException { try { return parser.parseFrom(pBody); } catch (InvalidProtocolBufferException e) { throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e); } }
public static <T> T get(ByteString pBody, Parser<T> parser) throws RpcException { try { return parser.parseFrom(pBody); } catch (InvalidProtocolBufferException e) { throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e); } }
public DelegateCASMap( ContentAddressableStorage contentAddressableStorage, Parser<V> parser) { this.contentAddressableStorage = contentAddressableStorage; this.parser = parser; this.digestMap = new ConcurrentHashMap<>(); }
private static <T extends Message> T parseMessage(DocumentSnapshot doc, Parser<T> parser) { final Blob blob = doc.getBlob(BYTES_KEY); final byte[] bytes = blob.toBytes(); try { final T message = parser.parseFrom(bytes); return message; } catch (InvalidProtocolBufferException e) { throw illegalStateWithCauseOf(e); } }
private <T> T parseAny(Any value, Parser<T> parser) { try { return parser.parseFrom(value.getValue()); } catch (InvalidProtocolBufferException e) { throw new RuntimeException(e); } }
public Subscriber(Class<T> type) { Parser<T> parser = null; try { parser = (Parser<T>) type.getDeclaredField("PARSER").get(null); } catch (Exception e) { System.out.println("Error: callback creation failed"); e.printStackTrace(); } this.parser = parser; }
/** Get the memoized {@link Parser}, possibly initializing it lazily. */ private Parser<T> getParser() { if (memoizedParser == null) { try { @SuppressWarnings("unchecked") T protoMessageInstance = (T) protoMessageClass.getMethod("getDefaultInstance").invoke(null); @SuppressWarnings("unchecked") Parser<T> tParser = (Parser<T>) protoMessageInstance.getParserForType(); memoizedParser = tParser; } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IllegalArgumentException(e); } } return memoizedParser; }
public Parser<? extends MessageLite> getParser(String name) throws InvalidProtocolBufferException { Parser<? extends MessageLite> parser = parses.get(name); if (parser == null) { throw new InvalidProtocolBufferException("did not found parse by name " + name); } return parser; }
@SuppressWarnings("unchecked") @Override public <REQUEST extends MessageLite, RESPONSE extends MessageLite> void registerCustomHandler(int messageTypeId, CustomMessageHandler<REQUEST, RESPONSE> handler, Parser<REQUEST> parser) { handlerRegistry.registerCustomHandler( messageTypeId, handler, new ControlTunnel.ProtoSerDe<REQUEST>(parser), (CustomSerDe<RESPONSE>) new ControlTunnel.ProtoSerDe<Message>(null)); }
@SuppressWarnings("unchecked") public <SEND extends MessageLite, RECEIVE extends MessageLite> CustomTunnel<SEND, RECEIVE> getCustomTunnel( int messageTypeId, Class<SEND> clazz, Parser<RECEIVE> parser) { return new CustomTunnel<SEND, RECEIVE>( messageTypeId, ((CustomSerDe<SEND>) new ProtoSerDe<Message>(null)), new ProtoSerDe<RECEIVE>(parser)); }
public static <T> T get(ByteBuf pBody, Parser<T> parser) throws RpcException { try { ByteBufInputStream is = new ByteBufInputStream(pBody); return parser.parseFrom(is); } catch (InvalidProtocolBufferException e) { throw new RpcException( String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e); } }