Search in sources :

Example 96 with EOFException

use of java.io.EOFException in project iosched by google.

the class StrictLineReaderTest method lineReaderConsistencyWithReadAsciiLine.

@Test
public void lineReaderConsistencyWithReadAsciiLine() {
    try {
        // Testing with LineReader buffer capacity 32 to check some corner cases.
        StrictLineReader lineReader = new StrictLineReader(createTestInputStream(), 32, Util.US_ASCII);
        InputStream refStream = createTestInputStream();
        while (true) {
            try {
                String refLine = 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 expected) {
                    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) Test(org.junit.Test)

Example 97 with EOFException

use of java.io.EOFException in project j2objc by google.

the class GZIPInputStream method maybeReadNextMember.

private boolean maybeReadNextMember() throws IOException {
    // If we have any unconsumed data in the inflater buffer, we have to
    // scan that first. The fact that we've reached here implies we've
    // successfully consumed the GZIP trailer.
    final int remaining = inf.getRemaining() - GZIP_TRAILER_SIZE;
    if (remaining > 0) {
        // remaining when it is first created.
        if (!(in instanceof PushbackInputStream)) {
            in = new PushbackInputStream(in, buf.length);
        }
        ((PushbackInputStream) in).unread(buf, inf.getCurrentOffset() + GZIP_TRAILER_SIZE, remaining);
    }
    final byte[] buffer;
    try {
        buffer = readHeader(in);
    } catch (EOFException eof) {
        // gzip record.
        return true;
    }
    final short magic = Memory.peekShort(buffer, 0, ByteOrder.LITTLE_ENDIAN);
    if (magic != (short) GZIP_MAGIC) {
        // from this stream.
        return true;
    }
    // We've encountered the gzip magic number, so we assume there's another
    // member in the stream.
    parseGzipHeader(in, buffer, crc, buf);
    return false;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) EOFException(java.io.EOFException)

Example 98 with EOFException

use of java.io.EOFException in project j2objc by google.

the class InflaterInputStream method fill.

/**
     * Fills the input buffer with data to be decompressed.
     *
     * @throws IOException
     *             if an {@code IOException} occurs.
     */
protected void fill() throws IOException {
    checkClosed();
    if (nativeEndBufSize > 0) {
        ZipFile.RAFStream is = (ZipFile.RAFStream) in;
        len = is.fill(inf, nativeEndBufSize);
    } else {
        if ((len = in.read(buf)) > 0) {
            inf.setInput(buf, 0, len);
        } else if (len == -1) {
            throw new EOFException("Unexpected end of stream.");
        }
    }
}
Also used : EOFException(java.io.EOFException)

Example 99 with EOFException

use of java.io.EOFException in project j2objc by google.

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 100 with EOFException

use of java.io.EOFException in project okhttp by square.

the class Http1Codec method readResponseHeaders.

@Override
public Response.Builder readResponseHeaders(boolean expectContinue) throws IOException {
    if (state != STATE_OPEN_REQUEST_BODY && state != STATE_READ_RESPONSE_HEADERS) {
        throw new IllegalStateException("state: " + state);
    }
    try {
        StatusLine statusLine = StatusLine.parse(source.readUtf8LineStrict());
        Response.Builder responseBuilder = new Response.Builder().protocol(statusLine.protocol).code(statusLine.code).message(statusLine.message).headers(readHeaders());
        if (expectContinue && statusLine.code == HTTP_CONTINUE) {
            return null;
        }
        state = STATE_OPEN_RESPONSE_BODY;
        return responseBuilder;
    } catch (EOFException e) {
        // Provide more context if the server ends the stream before sending a response.
        IOException exception = new IOException("unexpected end of stream on " + streamAllocation);
        exception.initCause(e);
        throw exception;
    }
}
Also used : StatusLine(okhttp3.internal.http.StatusLine) Response(okhttp3.Response) EOFException(java.io.EOFException) IOException(java.io.IOException)

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