Java InputStream Read Bytes readbytes(InputStream input, int n)

Here you can find the source of readbytes(InputStream input, int n)

Description

read a number of signed bytes

License

Open Source License

Declaration

public static byte[] readbytes(InputStream input, int n) throws IOException 

Method Source Code


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

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

public class Main {
    /**//from  ww  w .  j  av a  2s. co m
     * read a number of signed bytes
     */
    public static byte[] readbytes(InputStream input, int n) throws IOException {
        byte[] buffer = new byte[n];
        readbytes_into(input, buffer, 0, n);
        return buffer;
    }

    /**
     * read a number of signed bytes into the specified location in an existing byte array
     */
    public static void readbytes_into(InputStream input, byte[] buffer, int offset, int length) throws IOException {
        while (length > 0) {
            int read = input.read(buffer, offset, length);
            if (read == -1)
                throw new IOException("expected more bytes in input stream");
            offset += read;
            length -= read;
        }
    }
}

Related

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