@SuppressWarnings("unchecked") @Override public IN apply(byte[] bytes) { try { Message msg = messages.get(type); if(null == msg) { msg = (Message)type.getMethod("getDefaultInstance").invoke(null); messages.put(type, msg); } IN obj = (IN)msg.newBuilderForType().mergeFrom(bytes).build(); if(null != next) { next.accept(obj); return null; } else { return obj; } } catch(Exception e) { throw new IllegalStateException(e.getMessage(), e); } } };
public static <T extends Message.Builder> T readToBuilder(DataInputStream input, T builder) throws IOException { int size = input.readInt(); CodedInputStream codedIn = CodedInputStream.newInstance(input); codedIn.pushLimit(size); builder.mergeFrom(codedIn); return builder; } }
private Message coerceType(Message value) { if (value == null) { return null; } if (mapEntryMessageDefaultInstance.getClass().isInstance(value)) { return value; } // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return mapEntryMessageDefaultInstance.toBuilder().mergeFrom(value).build(); }
public static Message getResponse( org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceResponse result, com.google.protobuf.Message responsePrototype) throws IOException { Message response; if (result.getValue().hasValue()) { Message.Builder builder = responsePrototype.newBuilderForType(); builder.mergeFrom(result.getValue().getValue().newInput()); response = builder.build(); } else { response = responsePrototype.getDefaultInstanceForType(); } if (LOG.isTraceEnabled()) { LOG.trace("Master Result is value=" + response); } return response; }
/** * This version of protobuf's mergeFrom avoids the hard-coded 64MB limit for decoding * buffers when working with byte arrays * @param builder current message builder * @param b byte array * @param offset * @param length * @throws IOException */ public static void mergeFrom(Message.Builder builder, byte[] b, int offset, int length) throws IOException { final CodedInputStream codedInput = CodedInputStream.newInstance(b, offset, length); codedInput.setSizeLimit(length); builder.mergeFrom(codedInput); codedInput.checkLastTagWas(0); }
private Object coerceType(final Object value) { if (type.isInstance(value)) { return value; } else { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) .mergeFrom((Message) value).build(); } }
/** * For internal use only. This is to satisfy the FieldDescriptorLite * interface. */ @Override public MessageLite.Builder internalMergeFrom(MessageLite.Builder to, MessageLite from) { // FieldDescriptors are only used with non-lite messages so we can just // down-cast and call mergeFrom directly. return ((Message.Builder) to).mergeFrom((Message) from); }
private Object coerceType(final Object value) { if (type.isInstance(value)) { return value; } else { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) .mergeFrom((Message) value).buildPartial(); } }
private Object coerceType(final Object value) { if (type.isInstance(value)) { return value; } else { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) .mergeFrom((Message) value).buildPartial(); } }
/** * Creates a new message of type "Type" which is a copy of "source". "source" * must have the same descriptor but may be a different class (e.g. * DynamicMessage). */ @SuppressWarnings("unchecked") private static <Type extends Message> Type copyAsType( final Type typeDefaultInstance, final Message source) { return (Type) typeDefaultInstance .newBuilderForType().mergeFrom(source).build(); }
private Object coerceType(final Object value) { if (type.isInstance(value)) { return value; } else { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) .mergeFrom((Message) value).build(); } }
Message.Builder builder = getMessageBuilder(this.elementType.toClass()); ByteBuffer buffer = this.output.asByteBuffer(); builder.mergeFrom(CodedInputStream.newInstance(buffer), extensionRegistry); messages.add(builder.build()); DataBufferUtils.release(this.output);
/** * For internal use only. This is to satisfy the FieldDescriptorLite * interface. */ public MessageLite.Builder internalMergeFrom( MessageLite.Builder to, MessageLite from) { // FieldDescriptors are only used with non-lite messages so we can just // down-cast and call mergeFrom directly. return ((Message.Builder) to).mergeFrom((Message) from); } }
private Object coerceType(final Object value) { if (type.isInstance(value)) { return value; } else { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) .mergeFrom((Message) value).build(); } }
private Object coerceType(final Object value) { if (type.isInstance(value)) { return value; } else { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) .mergeFrom((Message) value).buildPartial(); } }
/** helper method to handle {@code builder} and {@code extensions}. */ private static void mergeOriginalMessage( Message.Builder builder, FieldSet<FieldDescriptor> extensions, FieldDescriptor field, Message.Builder subBuilder) { Message originalMessage = getOriginalMessage(builder, extensions, field); if (originalMessage != null) { subBuilder.mergeFrom(originalMessage); } }
/** * Creates a new message of type "Type" which is a copy of "source". "source" * must have the same descriptor but may be a different class (e.g. * DynamicMessage). */ @SuppressWarnings("unchecked") private static <Type extends Message> Type copyAsType( final Type typeDefaultInstance, final Message source) { return (Type)typeDefaultInstance.newBuilderForType() .mergeFrom(source) .build(); }
/** * This version of protobuf's mergeFrom avoids the hard-coded 64MB limit for decoding * buffers when working with byte arrays * @param builder current message builder * @param b byte array * @throws IOException */ public static void mergeFrom(Message.Builder builder, byte[] b) throws IOException { final CodedInputStream codedInput = CodedInputStream.newInstance(b); codedInput.setSizeLimit(b.length); builder.mergeFrom(codedInput); codedInput.checkLastTagWas(0); }
@Override protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = PROTOBUF; } Charset charset = contentType.getCharset(); if (charset == null) { charset = DEFAULT_CHARSET; } Message.Builder builder = getMessageBuilder(clazz); if (PROTOBUF.isCompatibleWith(contentType)) { builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry); } else if (TEXT_PLAIN.isCompatibleWith(contentType)) { InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset); TextFormat.merge(reader, this.extensionRegistry, builder); } else if (this.protobufFormatSupport != null) { this.protobufFormatSupport.merge( inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder); } return builder.build(); }
@Override public Mono<Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { return DataBufferUtils.join(inputStream).map(dataBuffer -> { try { Message.Builder builder = getMessageBuilder(elementType.toClass()); ByteBuffer buffer = dataBuffer.asByteBuffer(); builder.mergeFrom(CodedInputStream.newInstance(buffer), this.extensionRegistry); return builder.build(); } catch (IOException ex) { throw new DecodingException("I/O error while parsing input stream", ex); } catch (Exception ex) { throw new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex); } finally { DataBufferUtils.release(dataBuffer); } } ); }