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

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

Syntax:

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

This method takes three arguments. This method reads up to len bytes of data from this input stream into an array of bytes.

If len is not zero, the method blocks until at least 1 byte of input is available; otherwise, no bytes are read and 0 is returned.

Parameters: Three parameters are required for this method.

b: the buffer into which the data is read.

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

len: the maximum number of bytes read.

Returns: The number of bytes reads.

Throws:

1. NullPointerException - If b is null.

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

3. IOException - if an I/O error occurs.

Approach 1: When no exception

Java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceInputStreamread2 {
    public static void main(String[] args) throws IOException {
        FileInputStream input1 = new FileInputStream("D:\\hello.txt");
        @SuppressWarnings("resource")
        FileInputStream input2 = new FileInputStream("D:\\hell.txt");
        SequenceInputStream sequenceInputStream =
new SequenceInputStream(input1, input2);

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

        System.out.println(sequenceInputStream.read(b, off, len));
        sequenceInputStream.close();

    }
}

Output:

3


Approach 2: NullPointerException 

Java

package com.SequenceInputStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceInputStreamread2 {
    public static void main(String[] args) throws IOException {
        FileInputStream input1 = new FileInputStream("D:\\hello.txt");
        @SuppressWarnings("resource")
        FileInputStream input2 = new FileInputStream("D:\\hell.txt");
        SequenceInputStream sequenceInputStream =
new SequenceInputStream(input1, input2);

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

        System.out.println(sequenceInputStream.read(b, off, len));
        sequenceInputStream.close();

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:191) at com.SequenceInputStream.SequenceInputStreamread2.main(SequenceInputStreamread2.java:17)


Approach 3: IndexOutOfBoundsException 

Java

package com.SequenceInputStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceInputStreamread2 {
    public static void main(String[] args) throws IOException {
        FileInputStream input1 = new FileInputStream("D:\\hello.txt");
        @SuppressWarnings("resource")
        FileInputStream input2 = new FileInputStream("D:\\hell.txt");
        SequenceInputStream sequenceInputStream =
new SequenceInputStream(input1, input2);

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

        System.out.println(sequenceInputStream.read(b, off, len));
        sequenceInputStream.close();

    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.SequenceInputStream.read(SequenceInputStream.java:193) at com.SequenceInputStream.SequenceInputStreamread2.main(SequenceInputStreamread2.java:17)


Some other methods of SequenceInputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking the next invocation of a method for the current underlying input stream.

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

read()This method reads the next byte of data from this input stream. 

read(byte[], int, int)This method reads up to len bytes of data from this input stream into an array of bytes.

SequenceInputStream(InputStream, InputStream)This method initializes a newly created SequenceInputStreamby remembering the two arguments, which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream.

SequenceInputStream(Enumeration)This method initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream.

No comments:

Post a Comment