int streamId = 1;
ByteBuffer writeBuffer = generator.control(new SynStreamFrame(version, (byte)0, streamId, 0, (byte)0, (short)0, new Fields()));
channel.write(writeBuffer);
assertThat("writeBuffer is fully written", writeBuffer.hasRemaining(), is(false));
final Parser parser = new Parser(new StandardCompressionFactory.StandardDecompressor());
parser.addListener(new Listener.Adapter()
{
int bytesRead = 0;
@Override
public void onControlFrame(ControlFrame frame)
{
if (frame instanceof SynStreamFrame)
{
int pushStreamId = ((SynStreamFrame)frame).getStreamId();
ByteBuffer writeBuffer = generator.control(new RstStreamFrame(version, pushStreamId, StreamStatus.CANCEL_STREAM.getCode(version)));
try
{
channel.write(writeBuffer);
}
catch (IOException e)
{
e.printStackTrace();
unexpectedExceptionOccurred.set(true);
}
}
}
@Override
public void onDataFrame(DataFrame frame, ByteBuffer data)
{
if (frame.getStreamId() == 2)
bytesRead = bytesRead + frame.getLength();
if (bytesRead == dataSizeInBytes)
{
allDataFramesReceivedLatch.countDown();
return;
}
if (flowControl)
{
ByteBuffer writeBuffer = generator.control(new WindowUpdateFrame(version, frame.getStreamId(), frame.getLength()));
try
{
channel.write(writeBuffer);
}
catch (IOException e)
{
e.printStackTrace();
unexpectedExceptionOccurred.set(true);
}
}
}
});
Thread reader = new Thread(new Runnable()
{
@Override
public void run()
{
ByteBuffer readBuffer = ByteBuffer.allocate(dataSizeInBytes * 2);
while (read)
{
try
{
channel.read(readBuffer);
}
catch (IOException e)
{
e.printStackTrace();
unexpectedExceptionOccurred.set(true);
}
readBuffer.flip();
parser.parse(readBuffer);
readBuffer.clear();
}
}
});