RTEditor/src/main/java/com/onegravity/rteditor/utils/io/IOUtils.java
public static byte[] toByteArray(InputStream input, int size) throws IOException {

        if (size < 0) {
            throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
        }

        if (size == 0) {
            return new byte[0];
        }

        byte[] data = new byte[size];
        int offset = 0;
        int readed;

        while (offset < size && (readed = input.read(data, offset, size - offset)) != EOF) {
            offset += readed;
        }

        if (offset != size) {
            throw new IOException("Unexpected readed size. current: " + offset + ", excepted: " + size);
        }

        return data;
    }
public static byte[] toByteArray(InputStream input, int size) throws IOException {

        if (size < 0) {
            throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
        }

        if (size == 0) {
            return new byte[0];
        }

        byte[] data = new byte[size];
        int offset = 0;
        int readed;

        while (offset < size && (readed = input.read(data, offset, size - offset)) != EOF) {
            offset += readed;
        }

        if (offset != size) {
            throw new IOException("Unexpected readed size. current: " + offset + ", excepted: " + size);
        }

        return data;
    }
public static byte [ ] toByteArray ( InputStream input , int size ) throws IOException { if ( size < 0 ) { throw new IllegalArgumentException ( "Size must be equal or greater than zero: " + size ) ; } if ( size == 0 ) { return new byte [ 0 ] ; } byte [ ] data = new byte [ size ] ; int offset = 0 ; int readed ; while ( offset < size && ( readed = input . read ( data , offset , size - offset ) ) != EOF ) { offset += readed ; } if ( offset != size ) { throw new IOException ( "Unexpected readed size. current: " + offset + ", excepted: " + size ) ; } return data ; }
Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
Use this method instead of <code>toByteArray(InputStream)</code>
when <code>InputStream</code> size is known

@param input the <code>InputStream</code> to read from
@param size  the size of <code>InputStream</code>
@return the requested byte array
@throws IOException              if an I/O error occurs or <code>InputStream</code> size differ from parameter size
@throws IllegalArgumentException if size is less than zero
@since 2.1
Get the contents of an <code > InputStream< / code > as a <code > byte [] < / code > . Use this method instead of <code > toByteArray ( InputStream ) < / code > when <code > InputStream< / code > size is known