Example usage for java.io EOFException EOFException

List of usage examples for java.io EOFException EOFException

Introduction

In this page you can find the example usage for java.io EOFException EOFException.

Prototype

public EOFException() 

Source Link

Document

Constructs an EOFException with null as its error detail message.

Usage

From source file:org.apache.pdfbox.io.ScratchFileBuffer.java

/**
 * {@inheritDoc}//  ww  w  . j  av  a 2 s  . c o m
 */
@Override
public void seek(long seekToPosition) throws IOException {
    checkClosed();

    /*
     * for now we won't allow to seek past end of buffer; this can be changed by adding new pages as needed
     */
    if (seekToPosition > size) {
        throw new EOFException();
    }

    if (seekToPosition < 0) {
        throw new IOException("Negative seek offset: " + seekToPosition);
    }

    if ((seekToPosition >= currentPageOffset) && (seekToPosition <= currentPageOffset + pageSize)) {
        // within same page
        positionInPage = (int) (seekToPosition - currentPageOffset);
    } else {
        // have to go to another page

        // check if current page needs to be written to file
        if (currentPageContentChanged) {
            pageHandler.writePage(pageIndexes[currentPagePositionInPageIndexes], currentPage);
            currentPageContentChanged = false;
        }

        int newPagePosition = (int) (seekToPosition / pageSize);

        currentPage = pageHandler.readPage(pageIndexes[newPagePosition]);
        currentPagePositionInPageIndexes = newPagePosition;
        currentPageOffset = ((long) currentPagePositionInPageIndexes) * pageSize;
        positionInPage = (int) (seekToPosition - currentPageOffset);
    }
}

From source file:com.bittorrent.mpetazzoni.bencode.BDecoder.java

/**
 * Returns a byte[] containing length valid bytes starting at offset zero.
 *
 * @throws EOFException If InputStream.read() returned -1 before all
 * requested bytes could be read.  Note that the byte[] returned might be
 * bigger then requested but will only contain length valid bytes.  The
 * returned byte[] will be reused when this method is called again.
 *//*from   w w w . java  2s.  co m*/
private byte[] read(int length) throws IOException {
    byte[] result = new byte[length];

    int read = 0;
    while (read < length) {
        int i = this.in.read(result, read, length - read);
        if (i == -1)
            throw new EOFException();
        read += i;
    }

    return result;
}

From source file:TarList.java

public TarEntry(RandomAccessFile is) throws IOException, TarException {

    fileOffset = is.getFilePointer();/*from   w w  w  .j  av a 2  s. c  om*/

    // read() returns -1 at EOF
    if (is.read(name) < 0)
        throw new EOFException();
    // Tar pads to block boundary with nulls.
    if (name[0] == '\0')
        throw new EOFException();
    // OK, read remaining fields.
    is.read(mode);
    is.read(uid);
    is.read(gid);
    is.read(size);
    is.read(mtime);
    is.read(chksum);
    type = is.readByte();
    is.read(linkName);
    is.read(magic);
    is.read(uname);
    is.read(gname);
    is.read(devmajor);
    is.read(devminor);

    // Since the tar header is < 512, we need to skip it.
    is.skipBytes((int) (TarFile.RECORDSIZE - (is.getFilePointer() % TarFile.RECORDSIZE)));

    // TODO if checksum() fails,
    //   throw new TarException("Failed to find next header");

}

From source file:UTF8Util.java

/**
  Read a number of bytes into an array./*from  w  ww.j  a v a 2  s .com*/
        
  @exception IOException if an I/O error occurs.
  @exception EOFException if the end of the stream is reached
        
  @see DataInput#readFully
        
*/
public static void readFully(InputStream in, byte b[], int offset, int len) throws IOException {
    do {
        int bytesRead = in.read(b, offset, len);
        if (bytesRead < 0)
            throw new EOFException();
        len -= bytesRead;
        offset += bytesRead;
    } while (len != 0);
}

From source file:ga.rugal.jpt.common.tracker.bcodec.BDecoder.java

/**
 * Returns a byte[] containing length valid bytes starting at offset zero.
 *
 * @throws EOFException If InputStream.read() returned -1 before all requested bytes could be
 *                      read. Note that the byte[] returned might be bigger then requested but
 *                      will only contain length valid bytes. The returned byte[] will be reused
 *                      when this method is called again.
 *///  w  w  w  .j  a  v a  2 s.  c  o m
private byte[] read(int length) throws IOException {
    byte[] result = new byte[length];

    int read = 0;
    while (read < length) {
        int i = this.in.read(result, read, length - read);
        if (i == -1) {
            throw new EOFException();
        }
        read += i;
    }

    return result;
}

From source file:sample.multithreading.ImageDownloaderThread.java

public void run() {
    mThread = Thread.currentThread();
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
    Bitmap bmReturn = null;/* w w  w . j av a2 s  .  c o m*/
    try {
        if (Thread.interrupted())
            return;
        byte[] imageBuf = sCache.get(mLocation);
        if (null == imageBuf) {
            mHandler.sendEmptyMessage(DOWNLOAD_STARTED);
            InputStream is = null;
            try {
                HttpURLConnection httpConn = (HttpURLConnection) mLocation.openConnection();
                httpConn.setRequestProperty("User-Agent", NetworkDownloadService.USER_AGENT);
                if (Thread.interrupted())
                    return;
                is = httpConn.getInputStream();
                if (Thread.interrupted())
                    return;
                int contentLength = httpConn.getContentLength();

                /*
                 * We take advantage of the content length if it is returned
                 * to preallocate our buffer. If it is not returned, we end
                 * up thrashing memory a bit more.
                 */
                if (-1 == contentLength) {
                    byte[] buf = new byte[READ_SIZE];
                    int bufferLeft = buf.length;
                    int offset = 0;
                    int result = 0;
                    outer: do {
                        while (bufferLeft > 0) {
                            result = is.read(buf, offset, bufferLeft);
                            if (result < 0) {
                                // we're done
                                break outer;
                            }
                            offset += result;
                            bufferLeft -= result;
                            if (Thread.interrupted())
                                return;
                        }
                        // resize
                        bufferLeft = READ_SIZE;
                        int newSize = buf.length + READ_SIZE;
                        byte[] newBuf = new byte[newSize];
                        System.arraycopy(buf, 0, newBuf, 0, buf.length);
                        buf = newBuf;
                    } while (true);
                    imageBuf = new byte[offset];
                    System.arraycopy(buf, 0, imageBuf, 0, offset);
                } else {
                    imageBuf = new byte[contentLength];
                    int length = contentLength;
                    int offset = 0;
                    while (length > 0) {
                        int result = is.read(imageBuf, offset, length);
                        if (result < 0) {
                            throw new EOFException();
                        }
                        offset += result;
                        length -= result;
                        if (Thread.interrupted())
                            return;
                    }
                }
                if (Thread.interrupted())
                    return;
            } catch (IOException e) {
                return;
            } finally {
                if (null != is) {
                    try {
                        is.close();
                    } catch (Exception e) {

                    }
                }
            }
        }
        mHandler.sendEmptyMessage(DECODE_QUEUED);
        try {
            sCoreAvailable.acquire();
            mHandler.sendEmptyMessage(DECODE_STARTED);
            BitmapFactory.Options bfo = new BitmapFactory.Options();
            int targetWidth = mTargetWidth;
            int targetHeight = mTargetHeight;
            if (Thread.interrupted())
                return;
            bfo.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(imageBuf, 0, imageBuf.length, bfo);
            int hScale = bfo.outHeight / targetHeight;
            int wScale = bfo.outWidth / targetWidth;
            int sampleSize = Math.max(hScale, wScale);
            if (sampleSize > 1) {
                bfo.inSampleSize = sampleSize;
            }
            if (Thread.interrupted())
                return;
            bfo.inJustDecodeBounds = false;
            // oom handling in decode stage
            for (int i = 0; i < NUMBER_OF_DECODE_TRIES; i++) {
                try {
                    bmReturn = BitmapFactory.decodeByteArray(imageBuf, 0, imageBuf.length, bfo);
                    // break out of OOM loop
                    break;
                } catch (Throwable e) {
                    Log.e(LOG_TAG, "Out of memory in decode stage. Throttling.");
                    java.lang.System.gc();
                    if (Thread.interrupted())
                        return;
                    try {
                        Thread.sleep(0xfa);
                    } catch (java.lang.InterruptedException ix) {

                    }
                }
            }
            if (mCache) {
                sCache.put(mLocation, imageBuf);
            }
        } catch (java.lang.InterruptedException x) {
            x.printStackTrace();
        } finally {
            sCoreAvailable.release();
        }
    } finally {
        mThread = null;
        if (null == bmReturn) {
            mHandler.sendEmptyMessage(DOWNLOAD_FAILED);
        } else {
            Message completeMessage = mHandler.obtainMessage(TASK_COMPLETE, bmReturn);
            completeMessage.sendToTarget();
        }
        // clear interrupt flag
        Thread.interrupted();
    }
}

From source file:org.apache.pdfbox.io.ScratchFileBuffer.java

/**
 * {@inheritDoc}/*  w  w w  .  ja va2  s. com*/
 */
@Override
public byte[] readFully(int len) throws IOException {
    byte[] b = new byte[len];

    int n = 0;
    do {
        int count = read(b, n, len - n);
        if (count < 0) {
            throw new EOFException();
        }
        n += count;
    } while (n < len);

    return b;
}

From source file:org.apache.hadoop.hive.ql.io.orc.RecordReaderUtils.java

public static void readDirect(FSDataInputStream file, int len, ByteBuffer directBuf) throws IOException {
    // TODO: HDFS API is a mess, so handle all kinds of cases.
    // Before 2.7, read() also doesn't adjust position correctly, so track it separately.
    int pos = directBuf.position(), startPos = pos, endPos = pos + len;
    try {/*from ww  w.ja v  a2  s. com*/
        while (pos < endPos) {
            int count = SHIMS.readByteBuffer(file, directBuf);
            if (count < 0)
                throw new EOFException();
            assert count != 0 : "0-length read: " + (endPos - pos) + "@" + (pos - startPos);
            pos += count;
            assert pos <= endPos : "Position " + pos + " > " + endPos + " after reading " + count;
            directBuf.position(pos);
        }
    } catch (UnsupportedOperationException ex) {
        assert pos == startPos;
        // Happens in q files and such.
        RecordReaderImpl.LOG.error("Stream does not support direct read; we will copy.");
        byte[] buffer = new byte[len];
        file.readFully(buffer, 0, buffer.length);
        directBuf.put(buffer);
    }
    directBuf.position(startPos);
    directBuf.limit(startPos + len);
}

From source file:UTF8Util.java

/**
 * Skips requested number of bytes,/*from   w  ww.jav  a 2s .c o m*/
 * throws EOFException if there is too few bytes in the stream.
 * @param is
 *      InputStream to be skipped.
 * @param skippedBytes
 *      number of bytes to skip. if skippedBytes <= zero, do nothing.
 * @throws EOFException
 *      if EOF meets before requested number of bytes are skipped.
 * @throws IOException
 *      if IOException occurs. It doesn't contain EOFException.
 * @throws NullPointerException
 *      if the param 'is' equals null.
 */
public static void skipFully(InputStream is, long skippedBytes) throws IOException {
    if (is == null)
        throw new NullPointerException();

    if (skippedBytes <= 0)
        return;

    long bytes = skipPersistent(is, skippedBytes);

    if (bytes < skippedBytes)
        throw new EOFException();
}

From source file:org.apache.tajo.storage.v2.ScheduledInputStream.java

@Override
public void readFully(byte[] b, int off, int len) throws IOException {
    if (len < 0) {
        throw new IndexOutOfBoundsException();
    }//from w  w  w  .j  a va2s .  c om
    int n = 0;
    while (n < len) {
        int count = read(b, off + n, len - n);
        if (count < 0) {
            throw new EOFException();
        }
        n += count;
    }
}