use of java.io.EOFException in project robovm by robovm.
the class InflaterInputStream method read.
/**
* Reads up to {@code byteCount} bytes of decompressed data and stores it in
* {@code buffer} starting at {@code byteOffset}. Returns the number of uncompressed bytes read,
* or -1.
*/
@Override
public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
checkClosed();
Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount);
if (byteCount == 0) {
return 0;
}
if (eof) {
return -1;
}
do {
if (inf.needsInput()) {
fill();
}
// It may also be true if the next read() should return -1.
try {
int result = inf.inflate(buffer, byteOffset, byteCount);
eof = inf.finished();
if (result > 0) {
return result;
} else if (eof) {
return -1;
} else if (inf.needsDictionary()) {
eof = true;
return -1;
} else if (len == -1) {
eof = true;
throw new EOFException();
// If result == 0, fill() and try again
}
} catch (DataFormatException e) {
eof = true;
if (len == -1) {
throw new EOFException();
}
throw (IOException) (new IOException().initCause(e));
}
} while (true);
}
use of java.io.EOFException in project robovm by robovm.
the class StrictLineReaderTest method testLineReaderConsistencyWithReadAsciiLine.
public void testLineReaderConsistencyWithReadAsciiLine() {
try {
// Testing with LineReader buffer capacity 32 to check some corner cases.
StrictLineReader lineReader = new StrictLineReader(createTestInputStream(), 32, StandardCharsets.US_ASCII);
InputStream refStream = createTestInputStream();
while (true) {
try {
String refLine = Streams.readAsciiLine(refStream);
try {
String line = lineReader.readLine();
if (!refLine.equals(line)) {
fail("line (\"" + line + "\") differs from expected (\"" + refLine + "\").");
}
} catch (EOFException eof) {
fail("line reader threw EOFException too early.");
}
} catch (EOFException refEof) {
try {
lineReader.readLine();
fail("line reader didn't throw the expected EOFException.");
} catch (EOFException eof) {
// OK
break;
}
}
}
refStream.close();
lineReader.close();
} catch (IOException ioe) {
fail("Unexpected IOException " + ioe.toString());
}
}
use of java.io.EOFException in project robovm by robovm.
the class DeflaterOutputStreamTest method testSyncFlushDeflater.
/**
* Confirm that a DeflaterOutputStream constructed with Deflater
* with flushParm == SYNC_FLUSH does not need to to be flushed.
*
* http://b/4005091
*/
public void testSyncFlushDeflater() throws Exception {
Deflater def = new Deflater();
Field f = def.getClass().getDeclaredField("flushParm");
f.setAccessible(true);
f.setInt(def, Deflater.SYNC_FLUSH);
final int deflaterBufferSize = 512;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos, def, deflaterBufferSize);
// make output buffer large enough that even if compressed it
// won't all fit within the deflaterBufferSize.
final int outputBufferSize = 128 * deflaterBufferSize;
byte[] output = new byte[outputBufferSize];
for (int i = 0; i < output.length; i++) {
output[i] = (byte) i;
}
dos.write(output);
byte[] compressed = baos.toByteArray();
// this main reason for this assert is to make sure that the
// compressed byte count is larger than the
// deflaterBufferSize. However, when the original bug exists,
// it will also fail because the compressed length will be
// exactly the length of the deflaterBufferSize.
assertTrue("compressed=" + compressed.length + " but deflaterBufferSize=" + deflaterBufferSize, compressed.length > deflaterBufferSize);
// assert that we returned data matches the input exactly.
ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
InflaterInputStream iis = new InflaterInputStream(bais);
byte[] input = new byte[output.length];
int total = 0;
while (true) {
int n = iis.read(input, total, input.length - total);
if (n == -1) {
break;
}
total += n;
if (total == input.length) {
try {
iis.read();
fail();
} catch (EOFException expected) {
break;
}
}
}
assertEquals(output.length, total);
assertTrue(Arrays.equals(input, output));
// ensure Deflater.finish has not been called at any point
// during the test, since that would lead to the results being
// flushed even without SYNC_FLUSH being used
assertFalse(def.finished());
// Quieten CloseGuard.
def.end();
iis.close();
}
use of java.io.EOFException in project robovm by robovm.
the class Test method test_readFully$B.
public void test_readFully$B() throws IOException {
byte[] buf = new byte[testLength];
oos.writeBytes(testString);
oos.close();
ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
ois.readFully(buf);
assertEquals("Test 1: Incorrect bytes read;", testString, new String(buf));
ois.close();
ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
ois.read();
try {
ois.readFully(buf);
fail("Test 2: EOFException expected.");
} catch (EOFException e) {
// Expected.
}
}
use of java.io.EOFException in project robovm by robovm.
the class Test method test_readUnsignedByte.
public void test_readUnsignedByte() throws IOException {
oos.writeByte(-1);
oos.close();
ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
assertEquals("Test 1: Incorrect unsigned byte written or read.", 255, ois.readUnsignedByte());
try {
ois.readUnsignedByte();
fail("Test 2: EOFException expected.");
} catch (EOFException e) {
// Expected.
}
ois.close();
try {
ois.readUnsignedByte();
fail("Test 3: IOException expected.");
} catch (IOException e) {
// Expected.
}
}
Aggregations