Search in sources :

Example 26 with EOFException

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

the class LittleEndianDataInputStreamTest method testReadUnsignedShort_eof.

public void testReadUnsignedShort_eof() throws IOException {
    byte[] buf = { 23 };
    DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(buf));
    try {
        in.readUnsignedShort();
        fail();
    } catch (EOFException expected) {
    }
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException)

Example 27 with EOFException

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

the class CharStreamsTest method testSkipFully_EOF.

public void testSkipFully_EOF() throws IOException {
    Reader reader = new StringReader("abcde");
    try {
        CharStreams.skipFully(reader, 6);
        fail("expected EOFException");
    } catch (EOFException e) {
    // expected
    }
}
Also used : StringReader(java.io.StringReader) EOFException(java.io.EOFException) StringReader(java.io.StringReader) FilterReader(java.io.FilterReader) Reader(java.io.Reader)

Example 28 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)

Example 29 with EOFException

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

the class WebSocketReader method readControlFrame.

private void readControlFrame() throws IOException {
    Buffer buffer = new Buffer();
    if (frameBytesRead < frameLength) {
        if (isClient) {
            source.readFully(buffer, frameLength);
        } else {
            while (frameBytesRead < frameLength) {
                int toRead = (int) Math.min(frameLength - frameBytesRead, maskBuffer.length);
                int read = source.read(maskBuffer, 0, toRead);
                if (read == -1)
                    throw new EOFException();
                toggleMask(maskBuffer, read, maskKey, frameBytesRead);
                buffer.write(maskBuffer, 0, read);
                frameBytesRead += read;
            }
        }
    }
    switch(opcode) {
        case OPCODE_CONTROL_PING:
            frameCallback.onReadPing(buffer.readByteString());
            break;
        case OPCODE_CONTROL_PONG:
            frameCallback.onReadPong(buffer.readByteString());
            break;
        case OPCODE_CONTROL_CLOSE:
            int code = CLOSE_NO_STATUS_CODE;
            String reason = "";
            long bufferSize = buffer.size();
            if (bufferSize == 1) {
                throw new ProtocolException("Malformed close payload length of 1.");
            } else if (bufferSize != 0) {
                code = buffer.readShort();
                reason = buffer.readUtf8();
                String codeExceptionMessage = WebSocketProtocol.closeCodeExceptionMessage(code);
                if (codeExceptionMessage != null)
                    throw new ProtocolException(codeExceptionMessage);
            }
            frameCallback.onReadClose(code, reason);
            closed = true;
            break;
        default:
            throw new ProtocolException("Unknown control opcode: " + toHexString(opcode));
    }
}
Also used : Buffer(okio.Buffer) ProtocolException(java.net.ProtocolException) EOFException(java.io.EOFException) Integer.toHexString(java.lang.Integer.toHexString) ByteString(okio.ByteString)

Example 30 with EOFException

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

the class WebSocketReader method readMessage.

/**
   * Reads a message body into across one or more frames. Control frames that occur between
   * fragments will be processed. If the message payload is masked this will unmask as it's being
   * processed.
   */
private void readMessage(Buffer sink) throws IOException {
    while (true) {
        if (closed)
            throw new IOException("closed");
        if (frameBytesRead == frameLength) {
            // We are exhausted and have no continuations.
            if (isFinalFrame)
                return;
            readUntilNonControlFrame();
            if (opcode != OPCODE_CONTINUATION) {
                throw new ProtocolException("Expected continuation opcode. Got: " + toHexString(opcode));
            }
            if (isFinalFrame && frameLength == 0) {
                // Fast-path for empty final frame.
                return;
            }
        }
        long toRead = frameLength - frameBytesRead;
        long read;
        if (isMasked) {
            toRead = Math.min(toRead, maskBuffer.length);
            read = source.read(maskBuffer, 0, (int) toRead);
            if (read == -1)
                throw new EOFException();
            toggleMask(maskBuffer, read, maskKey, frameBytesRead);
            sink.write(maskBuffer, 0, (int) read);
        } else {
            read = source.read(sink, toRead);
            if (read == -1)
                throw new EOFException();
        }
        frameBytesRead += read;
    }
}
Also used : ProtocolException(java.net.ProtocolException) EOFException(java.io.EOFException) IOException(java.io.IOException)

Aggregations

EOFException (java.io.EOFException)624 IOException (java.io.IOException)275 FileInputStream (java.io.FileInputStream)81 DataInputStream (java.io.DataInputStream)79 Test (org.junit.Test)59 ByteArrayInputStream (java.io.ByteArrayInputStream)53 InputStream (java.io.InputStream)43 RandomAccessFile (java.io.RandomAccessFile)42 ArrayList (java.util.ArrayList)42 File (java.io.File)41 FileNotFoundException (java.io.FileNotFoundException)41 ByteBuffer (java.nio.ByteBuffer)40 BufferedInputStream (java.io.BufferedInputStream)31 Path (org.apache.hadoop.fs.Path)21 InterruptedIOException (java.io.InterruptedIOException)20 ObjectInputStream (java.io.ObjectInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 SocketTimeoutException (java.net.SocketTimeoutException)19 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)17 SocketException (java.net.SocketException)16