corda/sgx-jvm/avian/test/BufferedInputStreamTest.java
Andras Slemmer 9bb3d6b972 Add 'sgx-jvm/avian/' from commit '09e4fe60d01f4f4bfb6b2976973bb4913ef61edc'
git-subtree-dir: sgx-jvm/avian
git-subtree-mainline: f978eab8d1
git-subtree-split: 09e4fe60d0
2017-03-13 12:18:24 +00:00

81 lines
2.2 KiB
Java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
/**
* Checks that BufferedInputStream does not block if data is available in it's internal buffer.
*/
public class BufferedInputStreamTest
{
public static void main(String[] args) throws IOException
{
MyByteArrayStream in = new MyByteArrayStream(new byte[100]);
BufferedInputStream bin = new BufferedInputStream(in);
//read a single byte to fill the buffer
int b = bin.read();
byte[] buf = new byte[10];
//now try to read 10 bytes. this should a least return the content of the buffer. On OpenJDK this are
//4 bytes (the rest of the buffer returned by MyByteArrayStream in the first call).
//It should definately NOT block.
int count = bin.read(buf);
System.out.println("Read bytes: " + count);
}
/**
* Internal Stream used to show the BufferedInputStream behaviour.
*/
static class MyByteArrayStream extends ByteArrayInputStream
{
boolean stopReading = false;
/**
* @param buf
*/
public MyByteArrayStream(byte[] buf)
{
super(buf);
}
/* (non-Javadoc)
* @see java.io.ByteArrayInputStream#read(byte[], int, int)
*/
@Override
public synchronized int read(byte[] b, int off, int len)
{
if(stopReading == false)
{ //On the first call 5 bytes are returned.
stopReading = true;
return super.read(b, off, 5);
}
//on all following calls block. The spec says, that a least one byte is returned, if the
//stream is not at EOF.
while(available() == 0)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
}
return 0;
}
/* (non-Javadoc)
* @see java.io.ByteArrayInputStream#available()
*/
@Override
public synchronized int available()
{
if(stopReading)
{
return 0;
}
return super.available();
}
}
}