Java Code Examples for java.io.ByteArrayInputStream#markSupported()

The following examples show how to use java.io.ByteArrayInputStream#markSupported() . 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: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeReplyResponse() throws Exception {

    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName());

    Channel channel = new MockedChannel(url);

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    rpcResult.setResult("Hello, World!");

    Response response = new Response();
    response.setResult(rpcResult);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.cachedRequest.putIfAbsent(request.getId(), rd);
    codec.encode(channel, bos, response);

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);

    ByteArrayInputStream bis = new ByteArrayInputStream(buf);

    if (bis.markSupported()) {
        bis.mark(0);
    }

    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();

    Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assert.assertEquals(request.getId(), protocol.readI64());

    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals("echoString", message.name);
    Assert.assertEquals(TMessageType.REPLY, message.type);
    Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    Demo.echoString_result result = new Demo.echoString_result();
    result.read(protocol);
    protocol.readMessageEnd();

    Assert.assertEquals(rpcResult.getValue(), result.getSuccess());
}
 
Example 2
Source File: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeExceptionResponse() throws Exception {

    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName());

    Channel channel = new MockedChannel(url);

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    String exceptionMessage = "failed";
    rpcResult.setException(new RuntimeException(exceptionMessage));

    Response response = new Response();
    response.setResult(rpcResult);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.cachedRequest.put(request.getId(), rd);
    codec.encode(channel, bos, response);

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);

    if (bis.markSupported()) {
        bis.mark(0);
    }

    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();

    Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assert.assertEquals(request.getId(), protocol.readI64());

    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals("echoString", message.name);
    Assert.assertEquals(TMessageType.EXCEPTION, message.type);
    Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    TApplicationException exception = TApplicationException.read(protocol);
    protocol.readMessageEnd();

    Assert.assertEquals(exceptionMessage, exception.getMessage());

}
 
Example 3
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    rpcResult.setResult( "Hello, World!" );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.putIfAbsent( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );

    ByteArrayInputStream bis = new ByteArrayInputStream( buf );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.REPLY, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    Demo.echoString_result result = new Demo.echoString_result();
    result.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( rpcResult.getValue(), result.getSuccess() );
}
 
Example 4
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeExceptionResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    String exceptionMessage = "failed";
    rpcResult.setException( new RuntimeException( exceptionMessage ) );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.put( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );
    ByteArrayInputStream bis = new ByteArrayInputStream( buf);

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.EXCEPTION, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    TApplicationException exception = TApplicationException.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( exceptionMessage, exception.getMessage() );

}
 
Example 5
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    rpcResult.setResult( "Hello, World!" );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.putIfAbsent( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );

    ByteArrayInputStream bis = new ByteArrayInputStream( buf );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.REPLY, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    Demo.echoString_result result = new Demo.echoString_result();
    result.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( rpcResult.getValue(), result.getSuccess() );
}
 
Example 6
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeExceptionResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    String exceptionMessage = "failed";
    rpcResult.setException( new RuntimeException( exceptionMessage ) );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.put( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );
    ByteArrayInputStream bis = new ByteArrayInputStream( buf);

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.EXCEPTION, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    TApplicationException exception = TApplicationException.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( exceptionMessage, exception.getMessage() );

}
 
Example 7
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    rpcResult.setResult( "Hello, World!" );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.putIfAbsent( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );

    ByteArrayInputStream bis = new ByteArrayInputStream( buf );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.REPLY, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    Demo.echoString_result result = new Demo.echoString_result();
    result.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( rpcResult.getValue(), result.getSuccess() );
}
 
Example 8
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeExceptionResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    String exceptionMessage = "failed";
    rpcResult.setException( new RuntimeException( exceptionMessage ) );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.put( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );
    ByteArrayInputStream bis = new ByteArrayInputStream( buf);

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.EXCEPTION, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    TApplicationException exception = TApplicationException.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( exceptionMessage, exception.getMessage() );

}
 
Example 9
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeReplyResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    rpcResult.setResult( "Hello, World!" );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.putIfAbsent( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );

    ByteArrayInputStream bis = new ByteArrayInputStream( buf );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.REPLY, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    Demo.echoString_result result = new Demo.echoString_result();
    result.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( rpcResult.getValue(), result.getSuccess() );
}
 
Example 10
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeExceptionResponse() throws Exception {

    URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );

    Channel channel = new MockedChannel( url );

    Request request = createRequest();

    RpcResult rpcResult = new RpcResult();
    String exceptionMessage = "failed";
    rpcResult.setException( new RuntimeException( exceptionMessage ) );

    Response response = new Response();
    response.setResult( rpcResult );
    response.setId( request.getId() );
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);

    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
            ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
    ThriftCodec.cachedRequest.put( request.getId(), rd );
    codec.encode( channel, bos, response );

    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );
    ByteArrayInputStream bis = new ByteArrayInputStream( buf);

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    TIOStreamTransport transport = new TIOStreamTransport( bis );
    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
    Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
    int headerLength = protocol.readI16();

    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    Assert.assertEquals( request.getId(), protocol.readI64() );

    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals( "echoString", message.name );
    Assert.assertEquals( TMessageType.EXCEPTION, message.type );
    Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
    TApplicationException exception = TApplicationException.read( protocol );
    protocol.readMessageEnd();

    Assert.assertEquals( exceptionMessage, exception.getMessage() );

}
 
Example 11
Source File: HeaderedArchiveRecord.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Read header if present. Technique borrowed from HttpClient HttpParse
 * class. Using http parser code for now. Later move to more generic header
 * parsing code if there proves a need.
 * 
 * @return ByteArrayInputStream with the http header in it or null if no
 *         http header.
 * @throws IOException
 */
private InputStream readContentHeaders() throws IOException {
    // If judged a record that doesn't have an http header, return
    // immediately.
    if (!hasContentHeaders()) {
        return null;
    }
    byte [] statusBytes = LaxHttpParser.readRawLine(getIn());
    int eolCharCount = getEolCharsCount(statusBytes);
    if (eolCharCount <= 0) {
        throw new IOException("Failed to read raw lie where one " +
            " was expected: " + new String(statusBytes));
    }
    String statusLine = EncodingUtil.getString(statusBytes, 0,
        statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING);
    if (statusLine == null) {
        throw new NullPointerException("Expected status line is null");
    }
    // TODO: Tighten up this test.
    boolean isHttpResponse = StatusLine.startsWithHTTP(statusLine);
    boolean isHttpRequest = false;
    if (!isHttpResponse) {
        isHttpRequest = statusLine.toUpperCase().startsWith("GET") ||
            !statusLine.toUpperCase().startsWith("POST");
    }
    if (!isHttpResponse && !isHttpRequest) {
        throw new UnexpectedStartLineIOException("Failed parse of " +
            "status line: " + statusLine);
    }
    this.statusCode = isHttpResponse?
        (new StatusLine(statusLine)).getStatusCode(): -1;
    
    // Save off all bytes read.  Keep them as bytes rather than
    // convert to strings so we don't have to worry about encodings
    // though this should never be a problem doing http headers since
    // its all supposed to be ascii.
    ByteArrayOutputStream baos =
        new ByteArrayOutputStream(statusBytes.length + 4 * 1024);
    baos.write(statusBytes);
    
    // Now read rest of the header lines looking for the separation
    // between header and body.
    for (byte [] lineBytes = null; true;) {
        lineBytes = LaxHttpParser.readRawLine(getIn());
        eolCharCount = getEolCharsCount(lineBytes);
        if (eolCharCount <= 0) {
            throw new IOException("Failed reading headers: " +
                ((lineBytes != null)? new String(lineBytes): null));
        }
        // Save the bytes read.
        baos.write(lineBytes);
        if ((lineBytes.length - eolCharCount) <= 0) {
            // We've finished reading the http header.
            break;
        }
    }
    
    byte [] headerBytes = baos.toByteArray();
    // Save off where content body, post content headers, starts.
    this.contentHeadersLength = headerBytes.length;
    ByteArrayInputStream bais =
        new ByteArrayInputStream(headerBytes);
    if (!bais.markSupported()) {
        throw new IOException("ByteArrayInputStream does not support mark");
    }
    bais.mark(headerBytes.length);
    // Read the status line.  Don't let it into the parseHeaders function.
    // It doesn't know what to do with it.
    bais.read(statusBytes, 0, statusBytes.length);
    this.contentHeaders = LaxHttpParser.parseHeaders(bais,
        ARCConstants.DEFAULT_ENCODING);
    bais.reset();
    return bais;
}
 
Example 12
Source File: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode(channel, output, request);

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

    TTransport transport = new TIOStreamTransport(bis);

    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    // frame
    byte[] length = new byte[4];
    transport.read(length, 0, 4);

    if (bis.markSupported()) {
        bis.mark(0);
    }

    // magic
    Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals(messageLength + 4, bytes.length);

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    // service name
    Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    // dubbo request id
    Assert.assertEquals(request.getId(), protocol.readI64());

    // test message header length
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read(protocol);

    protocol.readMessageEnd();

    Assert.assertEquals("echoString", message.name);

    Assert.assertEquals(TMessageType.CALL, message.type);

    Assert.assertEquals("Hello, World!", args.getArg());

}
 
Example 13
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}
 
Example 14
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}
 
Example 15
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}
 
Example 16
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}