/** * Serializes the given protobuf object into a Netty {@link ChannelBuffer}. * @param method The name of the method of the RPC we're going to send. * @param pb The protobuf to serialize. * @return A new channel buffer containing the serialized protobuf, with * enough free space at the beginning to tack on the RPC header. */ static final ChannelBuffer toChannelBuffer(final byte[] method, final AbstractMessageLite pb) { final int pblen = pb.getSerializedSize(); final int vlen = CodedOutputStream.computeRawVarint32Size(pblen); final byte[] buf = new byte[4 + 19 + method.length + vlen + pblen]; try { final CodedOutputStream out = CodedOutputStream.newInstance(buf, 4 + 19 + method.length, vlen + pblen); out.writeRawVarint32(pblen); pb.writeTo(out); out.checkNoSpaceLeft(); } catch (IOException e) { throw new RuntimeException("Should never happen", e); } return ChannelBuffers.wrappedBuffer(buf); }
@Test public void readEvents_multipleEventsInOneChunk() throws Exception { final List<Event> subHbOffer = newArrayList( TestingProtos.SUBSCRIBED, TestingProtos.HEARTBEAT, TestingProtos.OFFER ); final List<byte[]> eventChunks = subHbOffer.stream() .map(AbstractMessageLite::toByteArray) .map(RecordIOUtils::createChunk) .collect(Collectors.toList()); final List<ByteBuf> singleChunk = newArrayList(Unpooled.copiedBuffer(concatAllChunks(eventChunks))); final List<Event> events = runTestOnChunks(singleChunk); assertThat(events).isEqualTo(subHbOffer); }
@Override public TypedOutput toBody(Object object) { if (!(object instanceof AbstractMessageLite)) { throw new IllegalArgumentException( "Expected a protobuf message but was " + (object != null ? object.getClass().getName() : "null")); } byte[] bytes = ((AbstractMessageLite) object).toByteArray(); return new TypedByteArray(MIME_TYPE, bytes); }
public static String encode(AbstractMessageLite message) { return new String(Base64.encodeBase64(message.toByteArray()), CHAR_SET); }
public ProtoDeterministicWriter(AbstractMessageLite message) { this.message = message; }
/** Returns a ProtoWrapper that wraps the provided object */ public static <M extends AbstractMessageLite> ProtoWrapper<M> of(M proto) { return new ProtoWrapper<M>(proto); }