@Setup(Level.Trial) public void benchSetup(BenchmarkParams params) throws TException { testObject = new ThriftTestObject(); testObject.addToField1(createElement("1")); testObject.addToField1(createElement("2")); testObject.addToField1(createElement("3")); testObject.addToField1(createElement("4")); testObject.addToField1(createElement("1")); testObject.addToField1(createElement("2")); testObject.addToField1(createElement("3")); testObject.addToField1(createElement("4")); testObject.addToField1(createElement("1")); testObject.addToField1(createElement("2")); testObject.addToField1(createElement("3")); testObject.addToField1(createElement("4")); testObject.addToField1(createElement("1")); testObject.addToField1(createElement("2")); testObject.addToField1(createElement("3")); testObject.addToField1(createElement("4")); byteArrayOutputStream = new ByteArrayOutputStream(); TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory(); protocol = protocolFactory.getProtocol(new TIOStreamTransport(byteArrayOutputStream)); }
@Setup(Level.Trial) public void benchSetup(BenchmarkParams params) throws TException { testObject = new ThriftTestObject(); testObject.addToField1(31.415926); testObject.addToField1(61.415923); testObject.addToField1(31.415269); testObject.addToField1(53.141926); testObject.addToField1(13.145926); testObject.addToField1(43.115926); testObject.addToField1(31.419265); testObject.addToField1(23.141596); testObject.addToField1(43.161592); testObject.addToField1(0.112); byteArrayOutputStream = new ByteArrayOutputStream(); TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory(); protocol = protocolFactory.getProtocol(new TIOStreamTransport(byteArrayOutputStream)); }
@Setup(Level.Trial) public void benchSetup(BenchmarkParams params) throws TException { testObject = new ThriftTestObject(); testObject.setField1("1"); testObject.setField2("2"); testObject.setField3("3"); testObject.setField4("4"); testObject.setField5("5"); testObject.setField6("6"); testObject.setField7("7"); testObject.setField8("8"); testObject.setField9("9"); testObject.setField10("10"); byteArrayOutputStream = new ByteArrayOutputStream(); TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory(); protocol = protocolFactory.getProtocol(new TIOStreamTransport(byteArrayOutputStream)); }
@Setup(Level.Trial) public void benchSetup(BenchmarkParams params) throws TException { testObject = new ThriftTestObject(); testObject.setField1(31.415926); testObject.setField2(61.415923); testObject.setField3(31.415269); testObject.setField4(53.141926); testObject.setField5(13.145926); testObject.setField6(43.115926); testObject.setField7(31.419265); testObject.setField8(23.141596); testObject.setField9(43.161592); testObject.setField10(0.112); byteArrayOutputStream = new ByteArrayOutputStream(); TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory(); protocol = protocolFactory.getProtocol(new TIOStreamTransport(byteArrayOutputStream)); }
@Setup(Level.Trial) public void benchSetup(BenchmarkParams params) throws TException { testObject = new ThriftTestObject(); testObject.addToField1(321); testObject.addToField1(123); testObject.addToField1(132); testObject.addToField1(312); testObject.addToField1(321); testObject.addToField1(123); testObject.addToField1(132); testObject.addToField1(312); testObject.addToField1(321); testObject.addToField1(123); testObject.addToField1(132); testObject.addToField1(312); testObject.addToField1(321); testObject.addToField1(123); testObject.addToField1(132); testObject.addToField1(312); byteArrayOutputStream = new ByteArrayOutputStream(); TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory(); protocol = protocolFactory.getProtocol(new TIOStreamTransport(byteArrayOutputStream)); }
@Setup(Level.Trial) public void benchSetup(BenchmarkParams params) throws TException { testObject = new ThriftTestObject(); testObject.setField1(31415926); testObject.setField2(61415923); testObject.setField3(31415269); testObject.setField4(53141926); testObject.setField5(13145926); testObject.setField6(43115926); testObject.setField7(31419265); testObject.setField8(23141596); testObject.setField9(43161592); testObject.setField10(112); byteArrayOutputStream = new ByteArrayOutputStream(); TCompactProtocol.Factory protocolFactory = new TCompactProtocol.Factory(); protocol = protocolFactory.getProtocol(new TIOStreamTransport(byteArrayOutputStream)); }
@Override protected final Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { /* This is initialized to null because the decode function must return * null if the buffer does not contain a complete frame and cannot be * decoded. */ List<T> ms = null; ByteBuf frame = null; while (null != (frame = (ByteBuf) super.decode(ctx, buffer))) { if (ms == null) ms = new ArrayList<T>(); ByteBufInputStream is = new ByteBufInputStream(frame); TCompactProtocol thriftProtocol = new TCompactProtocol(new TIOStreamTransport(is)); T message = allocateMessage(); message.read(thriftProtocol); ms.add(message); } return ms; }
@Override protected void encode(ChannelHandlerContext ctx, T msg, ByteBuf out) throws Exception { int lengthIndex = out.writerIndex(); // length field, will be filled in later. out.writeInt(0); int startIndex = out.writerIndex(); ByteBufOutputStream os = new ByteBufOutputStream(out); TCompactProtocol thriftProtocol = new TCompactProtocol(new TIOStreamTransport(os)); msg.write(thriftProtocol); os.close(); int endIndex = out.writerIndex(); // update the length field int length = endIndex - startIndex; out.setInt(lengthIndex, length); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { List<SyncMessage> ms = null; ChannelBuffer frame = null; while (null != (frame = (ChannelBuffer) super.decode(ctx, channel, buffer))) { if (ms == null) ms = new ArrayList<SyncMessage>(); ChannelBufferInputStream is = new ChannelBufferInputStream(frame); TCompactProtocol thriftProtocol = new TCompactProtocol(new TIOStreamTransport(is)); SyncMessage bsm = new SyncMessage(); bsm.read(thriftProtocol); ms.add(bsm); } return ms; }
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object message) throws Exception { if (message instanceof SyncMessage) { ChannelBuffer buf = new DynamicChannelBuffer(512); ChannelBufferOutputStream os = new ChannelBufferOutputStream(buf); TCompactProtocol thriftProtocol = new TCompactProtocol(new TIOStreamTransport(os)); ((SyncMessage) message).write(thriftProtocol); ChannelBuffer len = ChannelBuffers.buffer(4); len.writeInt(buf.readableBytes()); return ChannelBuffers.wrappedBuffer(len, buf); } return message; }
/** * Encodes a thrift object into a DEFLATE-compressed binary array. * * @param tBase Object to encode. * @return Deflated, encoded object. * @throws CodingException If the object could not be encoded. */ public static byte[] deflateNonNull(TBase<?, ?> tBase) throws CodingException { requireNonNull(tBase); // NOTE: Buffering is needed here for performance. // There are actually 2 buffers in play here - the BufferedOutputStream prevents thrift from // causing a call to deflate() on every encoded primitive. The DeflaterOutputStream buffer // allows the underlying Deflater to operate on a larger chunk at a time without stopping to // copy the intermediate compressed output to outBytes. // See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4986239 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); TTransport transport = new TIOStreamTransport( new BufferedOutputStream( new DeflaterOutputStream(outBytes, new Deflater(DEFLATE_LEVEL), DEFLATER_BUFFER_SIZE), DEFLATER_BUFFER_SIZE)); try { TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport); tBase.write(protocol); transport.close(); // calls finish() on the underlying stream, completing the compression return outBytes.toByteArray(); } catch (TException e) { throw new CodingException("Failed to serialize: " + tBase, e); } finally { transport.close(); } }
/** * Decodes a thrift object from a DEFLATE-compressed byte array into a target type. * * @param clazz Class to instantiate and deserialize to. * @param buffer Compressed buffer to decode. * @return A populated message. * @throws CodingException If the message could not be decoded. */ public static <T extends TBase<T, ?>> T inflateNonNull(Class<T> clazz, byte[] buffer) throws CodingException { requireNonNull(clazz); requireNonNull(buffer); T tBase = newInstance(clazz); TTransport transport = new TIOStreamTransport( new InflaterInputStream(new ByteArrayInputStream(buffer))); try { TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport); tBase.read(protocol); return tBase; } catch (TException e) { throw new CodingException("Failed to deserialize: " + e, e); } finally { transport.close(); } }
public static Result doPost() throws Exception { RequestBody requestBody = request().body(); byte[] requestContent = null; RawBuffer rawBuffer = requestBody.asRaw(); if (rawBuffer != null) { requestContent = rawBuffer.asBytes(); } else { requestContent = requestBody.asText().getBytes(Constants.UTF8); } InputStream in = new ByteArrayInputStream(requestContent); ByteArrayOutputStream out = new ByteArrayOutputStream(); TTransport transport = new TIOStreamTransport(in, out); TProtocol inProtocol = protocolFactory.getProtocol(transport); TProtocol outProtocol = protocolFactory.getProtocol(transport); processor.process(inProtocol, outProtocol); response().setHeader(CONTENT_TYPE, "application/x-thrift"); return ok(out.toByteArray()); }
@Override public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException { CountingOutputStream wrapper = new CountingOutputStream(output); TTransport transport = new TIOStreamTransport(wrapper); try { TProtocol protocol = protocolFactory.getProtocol(transport); TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence()); protocol.writeMessageBegin(tm); writeMessage(call.getMessage(), protocol); protocol.writeMessageEnd(); transport.flush(); wrapper.flush(); return wrapper.getByteCount(); } catch (TException e) { throw new SerializerException(e, e.getMessage()); } }
@Override public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException { CountingOutputStream wrapper = new CountingOutputStream(output); TTransport transport = new TIOStreamTransport(wrapper); try { TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport); TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence()); protocol.writeMessageBegin(tm); writeMessage(call.getMessage(), protocol); protocol.writeMessageEnd(); transport.flush(); wrapper.flush(); return wrapper.getByteCount(); } catch (TException e) { throw new SerializerException(e, e.getMessage()); } }
public FrameBuffer(final TNonblockingTransport trans, final SelectionKey selectionKey, final AbstractSelectThread selectThread) { trans_ = trans; selectionKey_ = selectionKey; selectThread_ = selectThread; buffer_ = ByteBuffer.allocate(4); frameTrans_ = new TMemoryInputTransport(); response_ = new TByteArrayOutputStream(); inTrans_ = inputTransportFactory_.getTransport(frameTrans_); outTrans_ = outputTransportFactory_.getTransport(new TIOStreamTransport(response_)); inProt_ = inputProtocolFactory_.getProtocol(inTrans_); outProt_ = outputProtocolFactory_.getProtocol(outTrans_); if (eventHandler_ != null) { context_ = eventHandler_.createContext(inProt_, outProt_); } else { context_ = null; } }
/** * Read in (deserialize) a thrift message in TTextProtocol format * from a file on disk, then serialize it back out to a string. * Finally, deserialize that string and compare to the original * message. */ @Test public void tTextProtocolReadWriteTest() throws Exception { // Deserialize the file contents into a thrift message. ByteArrayInputStream bais1 = new ByteArrayInputStream( fileContents.getBytes()); TTextProtocolTestMsg msg1 = new TTextProtocolTestMsg(); msg1.read(new TTextProtocol(new TIOStreamTransport(bais1))); assertEquals(testMsg(), msg1); // Serialize that thrift message out to a byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); msg1.write(new TTextProtocol(new TIOStreamTransport(baos))); byte[] bytes = baos.toByteArray(); // Deserialize that string back to a thrift message. ByteArrayInputStream bais2 = new ByteArrayInputStream(bytes); TTextProtocolTestMsg msg2 = new TTextProtocolTestMsg(); msg2.read(new TTextProtocol(new TIOStreamTransport(bais2))); assertEquals(msg1, msg2); }
@Test(expected = TException.class) public void rpcNoMethod() throws Exception { String request = "{\n" + " \"type\" : \"CALL\",\n" + " \"args\" : {\n" + " \"methodArg1\" : \"foo1\",\n" + " \"methodArg2\" : 200,\n" + " \"details\" : {\n" + " \"detailsArg1\" : \"foo2\",\n" + " \"detailsArg2\" : 100\n" + " }\n" + " }\n" + '}'; TTextProtocol prot = new TTextProtocol( new TIOStreamTransport(new ByteArrayInputStream(request.getBytes()))); prot.readMessageBegin(); }