Java InputStream Read Bytes readBytes(InputStream in, int length)

Here you can find the source of readBytes(InputStream in, int length)

Description

simple method to read some bytes

License

Open Source License

Parameter

Parameter Description
in data is read from this InputStream
length amount of bytes to read

Exception

Parameter Description
EOFException if end of stream is reached

Declaration


public static byte[] readBytes(InputStream in, int length) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*from  ww w. ja  v a 2 s. c om*/
     * simple method to read some bytes
     * @param in data is read from this InputStream
     * @param length amount of bytes to read
     * @throws EOFException if end of stream is reached
     * */

    public static byte[] readBytes(InputStream in, int length) throws IOException {
        byte[] data = new byte[length];
        readFully(in, data, 0, length);
        return data;
    }

    /**
     * reads bytes.length bytes or throws an EOFException
     * @param in data is read from this InputStream
     * @param bytes data is read into this array
     * @return the the byte array
     * @throws EOFException if end of stream is reached
     */
    public static byte[] readFully(InputStream in, byte[] bytes) throws IOException {
        return readFully(in, bytes, 0, bytes.length);
    }

    /**
     * fills the array with len bytes starting from off
     * @param in data is read from this InputStream
     * @param bytes data is read into this array
     * @param off starting point of data
     * @param len amount of bytes to read
     * @return the the byte array
     * @throws EOFException if end of stream is reached
     */
    public static byte[] readFully(InputStream in, byte[] bytes, int off, int len) throws IOException {
        if (len == 0)
            return bytes;
        if (off < 0 | off > bytes.length)
            throw new IllegalArgumentException("offset");
        if (len < 0 | len > bytes.length)
            throw new IllegalArgumentException("length");
        if (len + off > bytes.length)
            throw new IllegalArgumentException("range");
        int end = off + len;
        while (off != end) {
            int read = in.read(bytes, off, end - off);
            if (read == -1)
                throw new EOFException();
            off += read;
        }
        return bytes;
    }
}

Related

  1. readBytes(InputStream in, byte[] data, int offset, int length)
  2. readBytes(InputStream in, int bufferSize, boolean forceClose)
  3. readBytes(InputStream in, int bytesToRead, byte[] buffer, int bufferOffset)
  4. readBytes(InputStream in, int expectedBytes, long timeoutMs)
  5. readBytes(InputStream in, int len, boolean isLE)
  6. readBytes(InputStream in, int maxLeng)
  7. readBytes(InputStream in, long len)
  8. readBytes(InputStream input)
  9. readBytes(InputStream input, byte[] data, int capacity)