Search in sources :

Example 31 with EOFException

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

the class DiskLruCache method readJournal.

private void readJournal() throws IOException {
    BufferedSource source = Okio.buffer(fileSystem.source(journalFile));
    try {
        String magic = source.readUtf8LineStrict();
        String version = source.readUtf8LineStrict();
        String appVersionString = source.readUtf8LineStrict();
        String valueCountString = source.readUtf8LineStrict();
        String blank = source.readUtf8LineStrict();
        if (!MAGIC.equals(magic) || !VERSION_1.equals(version) || !Integer.toString(appVersion).equals(appVersionString) || !Integer.toString(valueCount).equals(valueCountString) || !"".equals(blank)) {
            throw new IOException("unexpected journal header: [" + magic + ", " + version + ", " + valueCountString + ", " + blank + "]");
        }
        int lineCount = 0;
        while (true) {
            try {
                readJournalLine(source.readUtf8LineStrict());
                lineCount++;
            } catch (EOFException endOfJournal) {
                break;
            }
        }
        redundantOpCount = lineCount - lruEntries.size();
        // If we ended on a truncated line, rebuild the journal before appending to it.
        if (!source.exhausted()) {
            rebuildJournal();
        } else {
            journalWriter = newJournalWriter();
        }
    } finally {
        Util.closeQuietly(source);
    }
}
Also used : EOFException(java.io.EOFException) IOException(java.io.IOException) BufferedSource(okio.BufferedSource)

Example 32 with EOFException

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

the class FileOperator method read.

/**
   * Copy {@code byteCount} bytes from the file at {@code pos} into to {@code source}. It is the
   * caller's responsibility to make sure there are sufficient bytes to read: if there aren't this
   * method throws an {@link EOFException}.
   */
public void read(long pos, Buffer sink, long byteCount) throws IOException {
    if (byteCount < 0)
        throw new IndexOutOfBoundsException();
    while (byteCount > 0L) {
        try {
            // Read up to byteCount bytes.
            byteBuffer.limit((int) Math.min(BUFFER_SIZE, byteCount));
            if (fileChannel.read(byteBuffer, pos) == -1)
                throw new EOFException();
            int bytesRead = byteBuffer.position();
            // Write those bytes to sink.
            sink.write(byteArray, 0, bytesRead);
            pos += bytesRead;
            byteCount -= bytesRead;
        } finally {
            byteBuffer.clear();
        }
    }
}
Also used : EOFException(java.io.EOFException)

Example 33 with EOFException

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

the class WireConverterFactoryTest method deserializeWrongValue.

@Test
public void deserializeWrongValue() throws IOException {
    ByteString encoded = ByteString.decodeBase64("////");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<?> call = service.get();
    try {
        call.execute();
        fail();
    } catch (EOFException ignored) {
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 34 with EOFException

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

the class InflaterSource method read.

@Override
public long read(Buffer sink, long byteCount) throws IOException {
    if (byteCount < 0)
        throw new IllegalArgumentException("byteCount < 0: " + byteCount);
    if (closed)
        throw new IllegalStateException("closed");
    if (byteCount == 0)
        return 0;
    while (true) {
        boolean sourceExhausted = refill();
        // Decompress the inflater's compressed data into the sink.
        try {
            Segment tail = sink.writableSegment(1);
            int bytesInflated = inflater.inflate(tail.data, tail.limit, Segment.SIZE - tail.limit);
            if (bytesInflated > 0) {
                tail.limit += bytesInflated;
                sink.size += bytesInflated;
                return bytesInflated;
            }
            if (inflater.finished() || inflater.needsDictionary()) {
                releaseInflatedBytes();
                if (tail.pos == tail.limit) {
                    // We allocated a tail segment, but didn't end up needing it. Recycle!
                    sink.head = tail.pop();
                    SegmentPool.recycle(tail);
                }
                return -1;
            }
            if (sourceExhausted)
                throw new EOFException("source exhausted prematurely");
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 35 with EOFException

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

the class RealBufferedSink method write.

@Override
public BufferedSink write(Source source, long byteCount) throws IOException {
    while (byteCount > 0) {
        long read = source.read(buffer, byteCount);
        if (read == -1)
            throw new EOFException();
        byteCount -= read;
        emitCompleteSegments();
    }
    return this;
}
Also used : EOFException(java.io.EOFException)

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