Java InputStream Read Bytes readBytes(InputStream input, byte[] data, int capacity)

Here you can find the source of readBytes(InputStream input, byte[] data, int capacity)

Description

Read bytes up to capacity from the input stream into the given array.

License

Apache License

Parameter

Parameter Description
input a parameter
data a parameter
capacity a parameter

Exception

Parameter Description
IOException an exception

Return

the number of bytes read

Declaration

private static int readBytes(InputStream input, byte[] data, int capacity) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**//w w w.j a v  a2 s  .c o  m
     * Read bytes up to capacity from the input stream into the given array.
     * @param input
     * @param data
     * @param capacity
     * @return the number of bytes read
     * @throws IOException
     */
    private static int readBytes(InputStream input, byte[] data, int capacity) throws IOException {
        int bytes = 0;
        while (bytes < capacity) {
            // try to read (capacity - bytes) length of data
            int size = input.read(data, bytes, capacity - bytes);
            if (size < 0) {
                // end of file has met before the data is done.
                break;
            }
            bytes = bytes + size;
        }
        return bytes;
    }
}

Related

  1. readBytes(InputStream in, int len, boolean isLE)
  2. readBytes(InputStream in, int length)
  3. readBytes(InputStream in, int maxLeng)
  4. readBytes(InputStream in, long len)
  5. readBytes(InputStream input)
  6. readBytes(InputStream input, Class type)
  7. readbytes(InputStream input, int n)
  8. readBytes(InputStream input, int size, byte[] buffer)
  9. readBytes(InputStream inputStream)