Here you can find the source of readBytes(InputStream in, int length)
Parameter | Description |
---|---|
in | data is read from this InputStream |
length | amount of bytes to read |
Parameter | Description |
---|---|
EOFException | if end of stream is reached |
public static byte[] readBytes(InputStream in, int length) throws IOException
//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; } }