Example usage for org.apache.thrift.transport TFramedTransport decodeFrameSize

List of usage examples for org.apache.thrift.transport TFramedTransport decodeFrameSize

Introduction

In this page you can find the example usage for org.apache.thrift.transport TFramedTransport decodeFrameSize.

Prototype

public static final int decodeFrameSize(final byte[] buf) 

Source Link

Usage

From source file:net.morimekta.providence.thrift.io.FramedBufferInputSteram.java

License:Apache License

private int readFrame() throws IOException {
    frameSizeBuffer.rewind();//www .j a  va2  s.  c  o  m
    frameSizeBuffer.limit(Integer.BYTES);

    in.read(frameSizeBuffer);
    if (frameSizeBuffer.position() == 0) {
        return -1;
    }
    if (frameSizeBuffer.position() < Integer.BYTES) {
        throw new IOException();
    }

    int frameSize = TFramedTransport.decodeFrameSize(frameSizeBuffer.array());
    if (frameSize < 1 || frameSize > MAX_BUFFER_SIZE) {
        throw new IOException();
    }

    buffer.rewind();
    buffer.limit(frameSize);

    while (in.read(buffer) > 0) {
        if (buffer.position() == frameSize) {
            break;
        }
        LOGGER.debug("still not enough:  " + buffer.position() + " of " + frameSize);
    }
    if (buffer.position() < frameSize) {
        throw new IOException();
    }

    buffer.flip();
    return frameSize;
}