InputStream read(byte[], int, int) in Java

read(byte[], int, int): This method is available in the java.io.InputStream class of Java.

Syntax:

int java.io.InputStream.read(byte[] b, int off, int len) throws IOException

This method takes three arguments. This method reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read.

The number of bytes actually read is returned as an integer.

This method blocks until input data are available, the end of the file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

Parameters: Three parameters are required for this method.

b: the buffer into which the data is read.

off: the start offset in the array b at in which the data is written.

len: the maximum number of bytes to read.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Throws:

1. IOException - If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.

2. NullPointerException - If b is null.

3. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamread3 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 5;

        try {
            System.out.println(inputStream.read(b, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        }
        inputStream.close();
    }
}

Output:

-1


Approach 2: IOException

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamread3 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 5;

        inputStream.close();
        try {
            System.out.println(inputStream.read(b, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        }

    }
}

Output:

IOException occurs

Approach 3: NullPointerException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamread3 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        byte b[] = null;
        int off = 0, len = 5;

        try {
            System.out.println(inputStream.read(b, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        }
        inputStream.close();
    }
}

Output:

NullPointerException occurs

Approach 4: IndexOutOfBoundsException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamread3 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 15;

        try {
            System.out.println(inputStream.read(b, off, len));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        }
        inputStream.close();
    }
}

Output:

IndexOutOfBoundsException occurs



Some other methods of InputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when the end of the stream is detected.

close()This method closes this input stream and releases any system resources associated with the stream.

mark(int) This method marks the current position in this input stream.

markSupported()This method tests if this input stream supports the mark and reset methods.

InputStream.nullInputStream()This method returns a new InputStream that reads no bytes. The returned stream is initially open.

read()This method reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.

read(byte[])This method reads some number of bytes from the input stream and stores them into the buffer array b.

readAllBytes()This method reads all remaining bytes from the input stream.

readNBytes(int)This method reads up to a specified number of bytes from the input stream.

readNBytes(byte[], int, int)This method reads the requested number of bytes from the input stream into the given byte array.

reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.

skip(long) This method skips over and discards n bytes of data from this input stream.

skipNBytes(long)This method skips over and discards exactly n bytes of data from this input stream.

transferTo(OutputStream)This method reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.


No comments:

Post a Comment