testdata for IDEA-137668

This commit is contained in:
Anna Kozlova
2015-03-16 18:57:14 +01:00
parent dfc1f6b698
commit 4603022670
2 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
class Test {
private InputStream stream;
public int read(byte[] data, int offset, int len) throws IOException {
return apply(() -> {
if (stream == null || stream.available() < 0) {
throw new FileNotFoundException();
}
for (long t = System.currentTimeMillis(); stream != null && stream.available() >= 0; ) {
if (System.currentTimeMillis() - t >= 10_000) {
throw new InterruptedIOException("Timeout exceeded");
}
final int count = stream.available();
if (count > 0) {
final int n = Math.min(len, count);
final byte[] buf = new byte[n];
final int c = stream.read(buf);
System.arraycopy(buf, 0, data, offset, c);
return c;
} else {
Thread.sleep(1L);
}
}
throw new FileNotFoundException();
});
}
synchronized <T> T apply(SerialPortAction<T> action) throws IOException {
try {
return action.apply();
} catch (InterruptedException x) {
final InterruptedIOException y = new InterruptedIOException(x.getMessage());
y.initCause(x);
throw y;
}
}
@FunctionalInterface
interface SerialPortAction<T> {
T apply() throws IOException, InterruptedException;
}
}