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

private static void readIntoBuffer(final InputStream is, final int length) throws IOException {
    int read = is.read(BUFFER, 0, length);
    if (read != length) {
        throw new EOFException();
    }/*from  w  w  w . j a va 2 s.  c o m*/
}

From source file:Main.java

public static final int readInt(InputStream i) throws IOException, EOFException {
    InputStream in = i;//  w ww  . j  av a 2  s  .co m
    int ch1 = in.read();
    int ch2 = in.read();
    int ch3 = in.read();
    int ch4 = in.read();
    if ((ch1 | ch2 | ch3 | ch4) < 0)
        throw new EOFException();
    return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

From source file:Main.java

/**
 * Returns the ASCII characters up to but not including the next "\r\n", or
 * "\n"./* w  ww  .j  a  v a  2s . c o  m*/
 *
 * @throws java.io.EOFException if the stream is exhausted before the next newline
 *                              character.
 */
public static String readAsciiLine(InputStream in) throws IOException {
    StringBuilder result = new StringBuilder(80);
    while (true) {
        int c = in.read();
        if (c == -1) {
            throw new EOFException();
        } else if (c == '\n') {
            break;
        }

        result.append((char) c);
    }
    int length = result.length();
    if (length > 0 && result.charAt(length - 1) == '\r') {
        result.setLength(length - 1);
    }
    return result.toString();
}

From source file:Main.java

/**
 * Simple wrapper around {@link InputStream#read()} that throws EOFException
 * instead of returning -1./*w w  w .  jav a2s  .  c o m*/
 */
public static int read(InputStream is) throws IOException {
    int b = is.read();
    if (b == -1) {
        throw new EOFException();
    }
    return b;
}

From source file:Main.java

public static void writeFully(final WritableByteChannel channel, final ByteBuffer buf) throws IOException {
    do {/*from   w  w  w .  j av a 2  s  .  c  o  m*/
        int written = channel.write(buf);
        if (written < 0) {
            throw new EOFException();
        }
    } while (buf.hasRemaining());
}

From source file:Main.java

/**
 * Returns the ASCII characters up to but not including the next "\r\n", or
 * "\n"./*from ww  w  .j  a  v a2 s.  c  o m*/
 *
 * @throws java.io.EOFException if the stream is exhausted before the next newline
 * character.
 */
public static String readAsciiLine(InputStream in) throws IOException {
    // TODO: support UTF-8 here instead
    StringBuilder result = new StringBuilder(80);
    while (true) {
        int c = in.read();
        if (c == -1) {
            throw new EOFException();
        } else if (c == '\n') {
            break;
        }

        result.append((char) c);
    }
    int length = result.length();
    if (length > 0 && result.charAt(length - 1) == '\r') {
        result.setLength(length - 1);
    }
    return result.toString();
}

From source file:Main.java

public static final int readInt(InputStream stream, int length) throws IOException {
    int result = 0;
    for (int i = 0; i != length; ++i) {
        int b = stream.read();
        if (b == -1) {
            throw new EOFException();
        }/*from  w  w w  .j a v a  2 s.  c o  m*/
        result |= (b << (i * 8));
    }
    return result;
}

From source file:Main.java

public static byte[] readBytes(InputStream is, long count) throws IOException {
    byte[] bytes = new byte[(int) count];

    int start = 0;

    do {/*ww  w.j av a  2  s.c o m*/
        int read = is.read(bytes, start, (int) count);

        // If we're at EOF, but trying to read the first set of bytes, return null
        if (read == -1) {
            if (start > 0) {
                throw new EOFException();
            }

            return null;
        }

        start += read;
        count -= read;
    } while (count > 0);

    return bytes;
}

From source file:Main.java

/**
 * Reads exactly 'byteCount' bytes from 'in' (into 'dst' at offset 'offset'), and throws
 * EOFException if insufficient bytes are available.
 *
 * Used to implement {@link java.io.DataInputStream#readFully(byte[], int, int)}.
 *//*from  w ww.  j a va 2s . co  m*/
public static void readFully(InputStream in, byte[] dst, int offset, int byteCount) throws IOException {
    if (byteCount == 0) {
        return;
    }
    if (in == null) {
        throw new NullPointerException("in == null");
    }
    if (dst == null) {
        throw new NullPointerException("dst == null");
    }
    checkOffsetAndCount(dst.length, offset, byteCount);
    while (byteCount > 0) {
        int bytesRead = in.read(dst, offset, byteCount);
        if (bytesRead < 0) {
            throw new EOFException();
        }
        offset += bytesRead;
        byteCount -= bytesRead;
    }
}

From source file:com.turn.ttorrent.bcodec.StreamBDecoder.java

@Override
protected byte readByte() throws IOException {
    int value = in.read();
    if (value == -1)
        throw new EOFException();
    return (byte) value;
}