ByteArrayInputStream class in java

java.io.ByteArrayInputStream is a class in java programming language. Inside of this class exists 8 public methods. Declaration of this class as follows.

public class java.io.ByteArrayInputStream 
extends java.io.InputStream 

ByteArrayInputStream public method

There are following useful methods which is define the inside of java.io.ByteArrayInputStream class.

MethodDescription
int available() It returns the number of remaining bytes that can be read (or skipped over) from this input stream.
void close() It closing a ByteArrayInputStream has no effect.
void mark(int readAheadLimit) It set the current marked position in the stream.
boolean markSupported() It tests if this supports mark/reset.
int read() It reads the next byte of data from this input stream.
int read(byte[] b, int off, int len) It reads up to bytes of data into an array of bytes from this input stream.
void reset() It resets the buffer to the marked position.
long skip(long n) It skips bytes of input from this input stream.

This reference is belong to javadoc

Public Constructors

There is 2 types of public constructor is defined inside the class, Whose syntax as follows.

public java.io.ByteArrayInputStream(byte[])
public java.io.ByteArrayInputStream(byte[],int,int)

Example of those constructors.

import java.io.ByteArrayInputStream;
public class Example
{
    public static void main(String[] argv)
    {
        byte[] data = {
            97 , 98 , 99 , 32 , 65 , 66 , 67
        };
        // ByteArrayInputStream(byte[] buf)
        ByteArrayInputStream record1 = new ByteArrayInputStream(data);
        // ByteArrayInputStream(byte[] buf, int offset, int length)
        ByteArrayInputStream record2 = new ByteArrayInputStream(data, 2, 3);
        // print byte to char
        int info = record1.read();
        while (info != -1)
        {
            System.out.print((char) info);
            info = record1.read();
        }
        System.out.println();
        // print byte to char
        info = record2.read();
        while (info != -1)
        {
            System.out.print((char) info);
            info = record2.read();
        }
    }
}
Instance of java.io.ByteArrayInputStream class in java
abc ABC
c A


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved