Search in sources :

Example 86 with EOFException

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);
}
Also used : EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 87 with EOFException

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 88 with EOFException

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();
}
Also used : Field(java.lang.reflect.Field) Deflater(java.util.zip.Deflater) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) EOFException(java.io.EOFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 89 with EOFException

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.
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) ObjectInputStream(java.io.ObjectInputStream)

Example 90 with EOFException

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.
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

EOFException (java.io.EOFException)1149 IOException (java.io.IOException)508 DataInputStream (java.io.DataInputStream)139 FileInputStream (java.io.FileInputStream)124 InputStream (java.io.InputStream)100 ByteArrayInputStream (java.io.ByteArrayInputStream)95 ArrayList (java.util.ArrayList)84 Test (org.junit.Test)84 ByteBuffer (java.nio.ByteBuffer)75 File (java.io.File)74 FileNotFoundException (java.io.FileNotFoundException)61 BufferedInputStream (java.io.BufferedInputStream)56 RandomAccessFile (java.io.RandomAccessFile)46 SocketTimeoutException (java.net.SocketTimeoutException)43 HashMap (java.util.HashMap)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 SocketException (java.net.SocketException)31 Map (java.util.Map)30 InterruptedIOException (java.io.InterruptedIOException)29 Path (org.apache.hadoop.fs.Path)29