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:Main.java

/**
 * Copies <code>length</code> bytes from the source input stream to the
 * destination output stream. If <code>length</code> is <code>-1</code>
 * as much bytes as possible will be copied (i.e. until
 * {@link InputStream#read()} returns <code>-1</code> to signal the end of
 * the stream).// w w  w .  j  a va2 s .  co m
 * 
 * @param source
 *            The input stream to read from
 * @param destination
 *            The output stream to write to
 * @param length
 *            The number of bytes to copy
 * @param bufferSize
 *            The buffer size
 * @throws IOException
 *             if an I/O error occurs
 */
public static void copy(InputStream source, OutputStream destination, long length, int bufferSize)
        throws IOException {
    long remaining = length;
    byte[] buffer = new byte[bufferSize];
    int read = 0;
    while ((remaining == -1) || (remaining > 0)) {
        read = source.read(buffer, 0,
                ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining);
        if (read == -1) {
            if (length == -1) {
                return;
            }
            throw new EOFException("stream reached eof");
        }
        destination.write(buffer, 0, read);
        remaining -= read;
    }
}

From source file:org.openhab.binding.homematic.internal.communicator.message.BinRpcMessage.java

/**
 * Decodes a BIN-RPC message from the given InputStream.
 *//*from ww w  . j av a 2 s.c o m*/
public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) throws IOException {
    this.encoding = encoding;
    byte sig[] = new byte[8];
    int length = is.read(sig, 0, 4);
    if (length != 4) {
        throw new EOFException("Only " + length + " bytes received reading signature");
    }
    validateBinXSignature(sig);
    length = is.read(sig, 4, 4);
    if (length != 4) {
        throw new EOFException("Only " + length + " bytes received reading message length");
    }
    int datasize = (new BigInteger(ArrayUtils.subarray(sig, 4, 8))).intValue();
    byte[] message = ArrayUtils.addAll(sig, IOUtils.toByteArray(is, datasize));
    decodeMessage(message, methodHeader);
}

From source file:org.eclipse.smarthome.binding.homematic.internal.communicator.message.BinRpcMessage.java

/**
 * Decodes a BIN-RPC message from the given InputStream.
 *///from  w w w  .  j a va 2  s  .  co  m
public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) throws IOException {
    this.encoding = encoding;
    byte sig[] = new byte[8];
    int length = is.read(sig, 0, 4);
    if (length != 4) {
        throw new EOFException("Only " + length + " bytes received reading signature");
    }
    validateBinXSignature(sig);
    length = is.read(sig, 4, 4);
    if (length != 4) {
        throw new EOFException("Only " + length + " bytes received reading message length");
    }
    int datasize = (new BigInteger(ArrayUtils.subarray(sig, 4, 8))).intValue();
    byte payload[] = new byte[datasize];
    int offset = 0;
    int currentLength;

    while (offset < datasize && (currentLength = is.read(payload, offset, datasize - offset)) != -1) {
        offset += currentLength;
    }
    if (offset != datasize) {
        throw new EOFException("Only " + offset + " bytes received while reading message payload, expected "
                + datasize + " bytes");
    }
    byte[] message = ArrayUtils.addAll(sig, payload);
    decodeMessage(message, methodHeader);
}

From source file:Main.java

/**
 * Read the requested number of bytes or fail if there are not enough left.
 * <p>//  w w w.  java 2s  .  com
 * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
 * not skip as many bytes as requested (most likely because of reaching EOF).
 * 
 * @param input where to read input from
 * @param buffer destination
 * @param offset inital offset into buffer
 * @param length length to read, must be >= 0
 * 
 * @throws IOException if there is a problem reading the file
 * @throws IllegalArgumentException if length is negative
 * @throws EOFException if the number of bytes read was incorrect
 */
public static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException {
    int actual = read(input, buffer, offset, length);
    if (actual != length) {
        throw new EOFException("Length to read: " + length + " actual: " + actual);
    }
}

From source file:org.apache.hadoop.fs.s3.S3InputStream.java

@Override
public synchronized void seek(long targetPos) throws IOException {
    String message = String.format("Cannot seek to %d", targetPos);
    if (targetPos > fileLength) {
        throw new EOFException(message + ": after EOF");
    }/*from w  ww  . j  a  v a 2  s. c  o m*/
    if (targetPos < 0) {
        throw new EOFException(message + ": negative");
    }
    pos = targetPos;
    blockEnd = -1;
}

From source file:org.apache.hadoop.hive.serde2.teradata.TeradataBinaryDataInputStream.java

/**
 * Read VARCHAR(N).//from w w  w .jav  a 2s  . c  om
 * The representation of Varchar in Teradata binary format is:
 * the first two bytes represent the length N of this varchar field,
 * the next N bytes represent the content of this varchar field.
 * To pad the null varchar, the length will be 0 and the content will be none.
 *
 * @return the string
 * @throws IOException the io exception
 */
public String readVarchar() throws IOException {
    int varcharLength = readUnsignedShort();
    byte[] varcharContent = new byte[varcharLength];
    int numOfBytesRead = in.read(varcharContent);
    if (varcharContent.length != 0 && numOfBytesRead != varcharLength) {
        throw new EOFException(format("Fail to read the varchar. Expect %d bytes, get %d bytes", varcharLength,
                numOfBytesRead));
    }
    //force it to be UTF8 string
    return new String(varcharContent, "UTF8");
}

From source file:org.apache.hadoop.fs.redfish.RedfishDataInputStream.java

@Override
public void readFully(long pos, byte[] buf, int off, int len) throws IOException {
    int nread = redfishPread(pos, buf, off, len);
    if (nread < len) {
        throw new EOFException("End of file reached before reading fully.");
    }/*from  w w w  .  j  ava2 s. c o m*/
}

From source file:com.meidusa.venus.benchmark.FileLineData.java

@Override
public synchronized Object nextData() {
    if (closed)//from  w  w w . j  a v  a2s . co  m
        throw new IllegalStateException("file closed..");
    String line = null;
    try {
        line = raf.readLine();

        while (StringUtil.isEmpty(line) && line != null) {
            line = raf.readLine();
        }

        if (line == null) {
            throw new EOFException("end of File=" + this.getFile().getAbsolutePath());
        }

    } catch (IOException e) {
        closed = true;
        throw new IllegalStateException(e);
    }
    String[] obj = null;
    if (needSplit) {
        if (lineSplit == null) {
            obj = StringUtils.split(line);
        } else {
            obj = StringUtils.split(line, lineSplit);
        }
        return obj;
    } else {
        return line;
    }
}

From source file:org.apache.hadoop.hbase.regionserver.wal.BinaryCompatibleBaseDecoder.java

private void rethrowEofException(IOException ioEx) throws IOException {
    boolean isEof = false;
    try {//from w w  w. j a  v  a  2s.  c  om
        isEof = this.in.available() == 0;
    } catch (Throwable t) {
        LOG.trace("Error getting available for error message - ignoring", t);
    }
    if (!isEof)
        throw ioEx;
    if (LOG.isTraceEnabled()) {
        LOG.trace("Partial cell read caused by EOF", ioEx);
    }
    EOFException eofEx = new EOFException("Partial cell read");
    eofEx.initCause(ioEx);
    throw eofEx;
}

From source file:com.hadoop.compression.fourmc.FourMcInputStream.java

/**
 * Reads len bytes in a loop.//from w w w  .j  a  va 2 s.c o  m
 * <p/>
 * This is copied from IOUtils.readFully except that it throws an EOFException
 * instead of generic IOException on EOF.
 *
 * @param in  The InputStream to read from
 * @param buf The buffer to fill
 * @param off offset from the buffer
 * @param len the length of bytes to read
 */
private static void readFully(InputStream in, byte buf[], int off, int len) throws IOException, EOFException {
    int toRead = len;
    while (toRead > 0) {
        int ret = in.read(buf, off, toRead);
        if (ret < 0) {
            throw new EOFException("Premature EOF from inputStream");
        }
        toRead -= ret;
        off += ret;
    }
}