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

public final int readBits(int n) throws IOException {
    int x = 0;/*from w w w. java2 s  .  c o  m*/
    while (n > leftBits) {
        n -= leftBits;
        x |= rightBits(leftBits, byteBuf) << n;
        byteBuf = in.read();
        if (byteBuf == -1) {
            throw new EOFException("reached end of stream");
        }
        leftBits = 8;
    }
    leftBits -= n;
    return x | rightBits(n, byteBuf >>> leftBits);
}

From source file:com.mebigfatguy.inventory.utils.LengthLimitedInputStream.java

@Override
public int read(byte[] b, int off, int len) throws IOException {
    if (len > remainingLength) {
        len = (int) remainingLength;
    }/*from  w ww  .j  a  v a2 s. co m*/

    if (len <= 0) {
        throw new EOFException("Read past the end of the stream");
    }

    int actLen = parentStream.read(b, off, len);
    remainingLength -= actLen;
    return actLen;
}

From source file:net.darkmist.alib.io.BufferUtil.java

/**
 * Allocate a buffer and fill it from a channel. The returned
 * buffer will be rewound to the begining.
 * @return Buffer containing size bytes from the channel.
 * @throws IOException if the channel read does.
 * @throws EOFException if a end of stream is encountered
 *    before the full size is read.// w w  w. j ava 2s  .  c o  m
 */
public static ByteBuffer allocateAndReadAll(int size, ReadableByteChannel channel) throws IOException {
    ByteBuffer buf = ByteBuffer.allocate(size);
    int justRead;
    int totalRead = 0;

    // FIXME, this will be a tight loop if the channel is non-blocking...
    while (totalRead < size) {
        logger.debug("reading totalRead={}", totalRead);
        if ((justRead = channel.read(buf)) < 0)
            throw new EOFException("Unexpected end of stream after reading " + totalRead + " bytes");
        totalRead += justRead;
    }
    buf.rewind();
    return buf;
}

From source file:org.freedesktop.dbus.MessageReader.java

public Message readMessage() throws IOException, DBusException {
    int rv;/*from   ww w  . j  av  a2 s.com*/
    /* Read the 12 byte fixed header, retrying as neccessary */
    if (null == this.buf) {
        this.buf = new byte[12];
        this.len[0] = 0;
    }
    if (this.len[0] < 12) {
        try {
            rv = this.in.read(this.buf, this.len[0], 12 - this.len[0]);
        } catch (SocketTimeoutException STe) {
            return null;
        }
        if (-1 == rv)
            throw new EOFException("Underlying transport returned EOF");
        this.len[0] += rv;
    }
    if (this.len[0] == 0)
        return null;
    if (this.len[0] < 12) {
        if (log.isDebugEnabled()) {
            log.debug("Only got " + this.len[0] + " of 12 bytes of header");
        }
        return null;
    }

    /* Parse the details from the header */
    byte endian = this.buf[0];
    byte type = this.buf[1];
    byte protover = this.buf[3];
    if (protover > Message.PROTOCOL) {
        this.buf = null;
        throw new MessageProtocolVersionException(
                String.format("Protocol version %s is unsupported", protover));
    }

    /* Read the length of the variable header */
    if (null == this.tbuf) {
        this.tbuf = new byte[4];
        this.len[1] = 0;
    }
    if (this.len[1] < 4) {
        try {
            rv = this.in.read(this.tbuf, this.len[1], 4 - this.len[1]);
        } catch (SocketTimeoutException STe) {
            log.debug("Socket timeout", STe);
            return null;
        }
        if (-1 == rv)
            throw new EOFException("Underlying transport returned EOF");
        this.len[1] += rv;
    }
    if (this.len[1] < 4) {
        if (log.isDebugEnabled()) {
            log.debug("Only got " + this.len[1] + " of 4 bytes of header");
        }
        return null;
    }

    /* Parse the variable header length */
    int headerlen = 0;
    if (null == this.header) {
        headerlen = (int) Message.demarshallint(this.tbuf, 0, endian, 4);
        if (0 != headerlen % 8)
            headerlen += 8 - (headerlen % 8);
    } else
        headerlen = this.header.length - 8;

    /* Read the variable header */
    if (null == this.header) {
        this.header = new byte[headerlen + 8];
        System.arraycopy(this.tbuf, 0, this.header, 0, 4);
        this.len[2] = 0;
    }
    if (this.len[2] < headerlen) {
        try {
            rv = this.in.read(this.header, 8 + this.len[2], headerlen - this.len[2]);
        } catch (SocketTimeoutException STe) {
            log.debug("Socket timeout", STe);
            return null;
        }
        if (-1 == rv)
            throw new EOFException("Underlying transport returned EOF");
        this.len[2] += rv;
    }
    if (this.len[2] < headerlen) {
        if (log.isDebugEnabled()) {
            log.debug("Only got " + this.len[2] + " of " + headerlen + " bytes of header");
        }
        return null;
    }

    /* Read the body */
    int bodylen = 0;
    if (null == this.body)
        bodylen = (int) Message.demarshallint(this.buf, 4, endian, 4);
    if (null == this.body) {
        this.body = new byte[bodylen];
        this.len[3] = 0;
    }
    if (this.len[3] < this.body.length) {
        try {
            rv = this.in.read(this.body, this.len[3], this.body.length - this.len[3]);
        } catch (SocketTimeoutException STe) {
            log.debug("Socket timeout", STe);
            return null;
        }
        if (-1 == rv)
            throw new EOFException("Underlying transport returned EOF");
        this.len[3] += rv;
    }
    if (this.len[3] < this.body.length) {
        if (log.isDebugEnabled()) {
            log.debug("Only got " + this.len[3] + " of " + this.body.length + " bytes of body");
        }
        return null;
    }

    Message m;
    switch (type) {
    case Message.MessageType.METHOD_CALL:
        m = new MethodCall();
        break;
    case Message.MessageType.METHOD_RETURN:
        m = new MethodReturn();
        break;
    case Message.MessageType.SIGNAL:
        m = new DBusSignal();
        break;
    case Message.MessageType.ERROR:
        m = new Error();
        break;
    default:
        throw new MessageTypeException(String.format("Message type %s unsupported", type));
    }
    if (log.isTraceEnabled()) {
        Hex h = new Hex();
        log.trace(h.encode(this.buf));
        log.trace(h.encode(this.tbuf));
        log.trace(h.encode(this.header));
        log.trace(h.encode(this.body));
    }
    try {
        m.populate(this.buf, this.header, this.body);
    } catch (DBusException DBe) {
        this.buf = null;
        this.tbuf = null;
        this.body = null;
        this.header = null;
        throw DBe;
    } catch (RuntimeException Re) {
        this.buf = null;
        this.tbuf = null;
        this.body = null;
        this.header = null;
        throw Re;
    }

    log.info("=> " + m);

    this.buf = null;
    this.tbuf = null;
    this.body = null;
    this.header = null;
    return m;
}

From source file:com.mebigfatguy.inventory.utils.LengthLimitedInputStream.java

@Override
public long skip(long n) throws IOException {
    if (remainingLength == 0) {
        throw new EOFException("Read past the end of the stream");
    }//from   w w  w . j a va  2  s  .  co m

    if (n > remainingLength) {
        remainingLength = 0;
        return remainingLength;
    }

    remainingLength -= n;
    return n;

}

From source file:org.codice.alliance.video.stream.mpegts.netty.BitReader.java

/**
 * Get the next bit's value, but do not actually consume the bit.
 *
 * @return true if the next bit is 1, false if the next bit is 0
 *//*w  w  w .  j a  va  2  s  .c  o m*/
public boolean testBit() throws EOFException {
    if (currentByte == null) {
        if (byteBuf.readableBytes() < 1) {
            throw new EOFException("read past end-of-file");
        }
        currentByte = byteBuf.readByte();
        currentIndex = MAX_BIT_INDEX;
    }

    byte result = (byte) ((currentByte >> currentIndex) & 0b1);

    return result == 0b1;
}

From source file:UTF8Util.java

/**
 * Skip the requested number of characters from the stream.
 * <p>/*w  w w .java  2s  .  c o  m*/
 * @param in byte stream with UTF-8 encoded characters
 * @param charsToSkip number of characters to skip
 * @return The number of bytes skipped.
 * @throws EOFException if end-of-stream is reached before the requested
 *      number of characters are skipped
 * @throws IOException if reading from the stream fails
 * @throws UTFDataFormatException if an invalid UTF-8 encoding is detected
 */
public static final long skipFully(InputStream in, long charsToSkip) throws EOFException, IOException {
    SkipCount skipped = internalSkip(in, charsToSkip);
    if (skipped.charsSkipped() != charsToSkip) {
        throw new EOFException("Reached end-of-stream prematurely at " + "character/byte position "
                + skipped.charsSkipped() + "/" + skipped.bytesSkipped() + ", trying to skip " + charsToSkip);
    }
    return skipped.bytesSkipped();
}

From source file:org.eclipse.smarthome.binding.lirc.internal.connector.LIRCStreamReader.java

@Override
public void run() {
    reader = new BufferedReader(new InputStreamReader(in));
    String line;// ww  w .  j  a v a  2s  .c o m
    String responseText = "";
    while (!interrupted) {
        try {
            line = reader.readLine();
            if (line == null) {
                throw new EOFException("lost connection");
            } else {
                logger.trace("Received message: {}", line);
                Matcher m = EVENT_PATTERN.matcher(line);
                if (m.matches()) {
                    String code = m.group(1);
                    String repeatsHex = m.group(2);
                    String button = m.group(3);
                    String remote = m.group(4);
                    int repeats = Integer.parseInt(repeatsHex, 16);
                    LIRCButtonEvent buttonMessage = new LIRCButtonEvent(remote, button, repeats, code);
                    connector.sendButtonToListeners(buttonMessage);
                } else {
                    if ("BEGIN".equals(line)) {
                        responseText = "";
                    } else if ("END".equals(line)) {
                        processResponse(responseText);
                        responseText = null;
                    } else {
                        responseText += line + "\n";
                    }
                }
            }
        } catch (InterruptedIOException e) {
            Thread.currentThread().interrupt();
            logger.error("Interrupted via InterruptedIOException");
        } catch (EOFException e) {
            logger.error("Lost connection to LIRC server", e);
            connector.sendErrorToListeners(e.getMessage());
            this.interrupt();
        } catch (IOException e) {
            if (!interrupted) {
                logger.error("Reading from socket failed", e);
                connector.sendErrorToListeners(e.getMessage());
            }
        } catch (LIRCResponseException e) {
            logger.error("Invalid message received", e);
        }
    }
    IOUtils.closeQuietly(reader);
}

From source file:Main.java

/**
 * Skip the requested number of characters or fail if there are not enough left.
 * <p>//from   w w  w  . j  a  va 2 s . c o m
 * This allows for the possibility that {@link Reader#skip(long)} may
 * not skip as many characters as requested (most likely because of reaching EOF).
 * 
 * @param input stream to skip
 * @param toSkip the number of characters to skip
 * @see Reader#skip(long)
 * 
 * @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
 * @since Commons IO 2.0
 */
public static void skipFully(Reader input, long toSkip) throws IOException {
    long skipped = skip(input, toSkip);
    if (skipped != toSkip) {
        throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped);
    }
}

From source file:org.apache.hadoop.hbase.codec.BaseDecoder.java

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