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(String s) 

Source Link

Document

Constructs an EOFException with the specified detail message.

Usage

From source file:org.apache.fontbox.ttf.TTFSubFont.java

private byte[] buildHmtxTable() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    LOG.debug("Building table [hmtx]...");
    HorizontalHeaderTable h = this.baseTTF.getHorizontalHeader();
    HorizontalMetricsTable hm = this.baseTTF.getHorizontalMetrics();
    byte[] buf = new byte[4];
    InputStream is = this.baseTTF.getOriginalData();
    try {//www .  j  a  va  2  s  .co  m
        is.skip(hm.getOffset());
        long lastOff = 0;
        for (Integer glyphId : this.glyphIds) {
            // offset in original file.
            long off;
            if (glyphId < h.getNumberOfHMetrics()) {
                off = glyphId * 4;
            } else {
                off = h.getNumberOfHMetrics() * 4 + (glyphId - h.getNumberOfHMetrics()) * 2;
            }
            // skip over from last original offset.
            if (off != lastOff) {
                long nskip = off - lastOff;
                if (nskip != is.skip(nskip)) {
                    throw new EOFException("Unexpected EOF exception parsing glyphId of hmtx table.");
                }
            }
            // read left side bearings only, if we are beyond numOfHMetrics.
            int n = glyphId < h.getNumberOfHMetrics() ? 4 : 2;
            if (n != is.read(buf, 0, n)) {
                throw new EOFException("Unexpected EOF exception parsing glyphId of hmtx table.");
            }
            bos.write(buf, 0, n);
            lastOff = off + n;
        }
        LOG.debug("Finished table [hmtx].");
        return bos.toByteArray();
    } finally {
        is.close();
    }
}

From source file:org.apache.hadoop.fs.swift.http.SwiftRestClient.java

/**
 * Build an exception from a failed operation. This can include generating
 * specific exceptions (e.g. FileNotFound), as well as the default
 * {@link SwiftInvalidResponseException}
 * {@link SwiftInvalidResponseException}.
 * @param uri URI for operation//from   w  w w .  j a v  a  2  s. c o m
 * @param method operation that failed
 * @param statusCode status code
 * @param <M> method type
 * @return an exception to throw.
 */
private <M extends HttpMethod> IOException buildException(URI uri, M method, int statusCode) {
    IOException fault;

    //log the failure @debug level
    String errorMessage = String.format("Method %s on %s failed, status code: %d," + " status line: %s",
            method.getName(), uri, statusCode, method.getStatusLine());
    if (LOG.isDebugEnabled()) {
        LOG.debug(errorMessage);
    }
    //send the command
    switch (statusCode) {
    case SC_NOT_FOUND:
        fault = new FileNotFoundException("Operation " + method.getName() + " on " + uri);
        break;
    case SC_BAD_REQUEST:
        //bad HTTP request
        fault = new SwiftBadRequestException("Bad request against " + uri);
        break;

    case SC_REQUESTED_RANGE_NOT_SATISFIABLE:
        //out of range: end of the message
        fault = new EOFException(method.getStatusText());
        break;

    default:
        fault = new SwiftInvalidResponseException(errorMessage, statusCode, method.getName(), uri);
    }

    return fault;
}

From source file:com.emc.esu.api.rest.AbstractEsuRestApi.java

protected byte[] readStream(InputStream in, int contentLength) throws IOException {
    try {//from  w  w  w.  j  a  va 2s.  c  om
        byte[] output;
        // If we know the content length, read it directly into a buffer.
        if (contentLength != -1) {
            output = new byte[contentLength];

            int c = 0;
            while (c < contentLength) {
                int read = in.read(output, c, contentLength - c);
                if (read == -1) {
                    // EOF!
                    throw new EOFException(
                            "EOF reading response at position " + c + " size " + (contentLength - c));
                }
                c += read;
            }

            return output;
        } else {
            l4j.debug("Content length is unknown.  Buffering output.");
            // Else, use a ByteArrayOutputStream to collect the response.
            byte[] buffer = new byte[4096];

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int c = 0;
            while ((c = in.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
            }
            baos.close();

            l4j.debug("Buffered " + baos.size() + " response bytes");

            return baos.toByteArray();
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }

}

From source file:com.mellanox.r4h.DFSInputStream.java

/**
 * Seek to a new arbitrary location//from w ww .  ja va  2 s . c o  m
 */
@Override
public synchronized void seek(long targetPos) throws IOException {
    if (targetPos > getFileLength()) {
        throw new EOFException("Cannot seek after EOF");
    }
    if (targetPos < 0) {
        throw new EOFException("Cannot seek to negative offset");
    }
    if (closed.get()) {
        throw new IOException("Stream is closed!");
    }
    boolean done = false;
    if (pos <= targetPos && targetPos <= blockEnd) {
        //
        // If this seek is to a positive position in the current
        // block, and this piece of data might already be lying in
        // the TCP buffer, then just eat up the intervening data.
        //
        int diff = (int) (targetPos - pos);
        if (diff <= blockReader.available()) {
            try {
                pos += blockReader.skip(diff);
                if (pos == targetPos) {
                    done = true;
                } else {
                    // The range was already checked. If the block reader returns
                    // something unexpected instead of throwing an exception, it is
                    // most likely a bug.
                    String errMsg = "BlockReader failed to seek to " + targetPos + ". Instead, it seeked to "
                            + pos + ".";
                    DFSClient.LOG.warn(errMsg);
                    throw new IOException(errMsg);
                }
            } catch (IOException e) {// make following read to retry
                if (DFSClient.LOG.isDebugEnabled()) {
                    DFSClient.LOG.debug("Exception while seek to " + targetPos + " from " + getCurrentBlock()
                            + " of " + src + " from " + currentNode, e);
                }
            }
        }
    }
    if (!done) {
        pos = targetPos;
        blockEnd = -1;
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetImpl.java

/**
 * Check if a block is valid./*from ww  w .  java 2  s . co  m*/
 *
 * @param b           The block to check.
 * @param minLength   The minimum length that the block must have.  May be 0.
 * @param state       If this is null, it is ignored.  If it is non-null, we
 *                        will check that the replica has this state.
 *
 * @throws ReplicaNotFoundException          If the replica is not found 
 *
 * @throws UnexpectedReplicaStateException   If the replica is not in the 
 *                                             expected state.
 * @throws FileNotFoundException             If the block file is not found or there
 *                                              was an error locating it.
 * @throws EOFException                      If the replica length is too short.
 * 
 * @throws IOException                       May be thrown from the methods called. 
 */
public void checkBlock(ExtendedBlock b, long minLength, ReplicaState state) throws ReplicaNotFoundException,
        UnexpectedReplicaStateException, FileNotFoundException, EOFException, IOException {
    final ReplicaInfo replicaInfo = volumeMap.get(b.getBlockPoolId(), b.getLocalBlock());
    if (replicaInfo == null) {
        throw new ReplicaNotFoundException(b);
    }
    if (replicaInfo.getState() != state) {
        throw new UnexpectedReplicaStateException(b, state);
    }
    if (!replicaInfo.getBlockFile().exists()) {
        throw new FileNotFoundException(replicaInfo.getBlockFile().getPath());
    }
    long onDiskLength = getLength(b);
    if (onDiskLength < minLength) {
        throw new EOFException(
                b + "'s on-disk length " + onDiskLength + " is shorter than minLength " + minLength);
    }
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Skips the requested number of bytes or fail if there are not enough left.
 * <p>/*from   w  w w  . j a va2 s . com*/
 * This allows for the possibility that {@link InputStream#skip(long)} may
 * not skip as many bytes as requested (most likely because of reaching EOF).
 * <p>
 * Note that the implementation uses {@link #skip(InputStream, long)}.
 * This means that the method may be considerably less efficient than using the actual skip implementation,
 * this is done to guarantee that the correct number of characters are skipped.
 * </p>
 *
 * @param input stream to skip
 * @param toSkip the number of bytes to skip
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if toSkip is negative
 * @throws EOFException             if the number of bytes skipped was incorrect
 * @see InputStream#skip(long)
 * @since 2.0
 */
public static void skipFully(final InputStream input, final long toSkip) throws IOException {
    if (toSkip < 0) {
        throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);
    }
    final long skipped = skip(input, toSkip);
    if (skipped != toSkip) {
        throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);
    }
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Skips the requested number of bytes or fail if there are not enough left.
 *
 * @param input ReadableByteChannel to skip
 * @param toSkip the number of bytes to skip
 * @throws IOException              if there is a problem reading the ReadableByteChannel
 * @throws IllegalArgumentException if toSkip is negative
 * @throws EOFException             if the number of bytes skipped was incorrect
 * @since 2.2// w w w. java 2  s .  c  om
 */
public static void skipFully(final ReadableByteChannel input, final long toSkip) throws IOException {
    if (toSkip < 0) {
        throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);
    }
    final long skipped = skip(input, toSkip);
    if (skipped != toSkip) {
        throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);
    }
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Skips the requested number of characters or fail if there are not enough left.
 * <p>/*  w w w .j  a v a2 s  . c  om*/
 * This allows for the possibility that {@link Reader#skip(long)} may
 * not skip as many characters as requested (most likely because of reaching EOF).
 * <p>
 * Note that the implementation uses {@link #skip(Reader, long)}.
 * This means that the method may be considerably less efficient than using the actual skip implementation,
 * this is done to guarantee that the correct number of characters are skipped.
 * </p>
 *
 * @param input stream to skip
 * @param toSkip the number of characters to skip
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if toSkip is negative
 * @throws EOFException             if the number of characters skipped was incorrect
 * @see Reader#skip(long)
 * @since 2.0
 */
public static void skipFully(final Reader input, final long toSkip) throws IOException {
    final long skipped = skip(input, toSkip);
    if (skipped != toSkip) {
        throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped);
    }
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Reads the requested number of characters or fail if there are not enough left.
 * <p/>/*  w w  w  .j a v  a 2  s . c  o m*/
 * This allows for the possibility that {@link Reader#read(char[], int, int)} may
 * not read as many characters as requested (most likely because of reaching EOF).
 *
 * @param input where to read input from
 * @param buffer destination
 * @param offset initial offset into buffer
 * @param length length to read, must be &gt;= 0
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if length is negative
 * @throws EOFException             if the number of characters read was incorrect
 * @since 2.2
 */
public static void readFully(final Reader input, final char[] buffer, final int offset, final int length)
        throws IOException {
    final int actual = read(input, buffer, offset, length);
    if (actual != length) {
        throw new EOFException("Length to read: " + length + " actual: " + actual);
    }
}