Java ByteArrayInputStream Class

Category: Java   Tags: Java, Java File Handling, Java InputStream Class, Java ByteArrayInputStream Class

ByteArrayInputStream is an implementation of an InputStream that uses a byte array as the source. It has below declaration:

                                    public class ByteArrayInputStream
                                    extends InputStream
                                

Constructors

public ByteArrayInputStream(byte[] buf)
Creates a ByteArrayInputStream, use buf as input source.
public ByteArrayInputStream(byte[] buf, int start, int length)
Creates a ByteArrayInputStream from the subset of buf starting at start and length long.

Methods

public int read()
Returns the next byte of data, or -1 if the end of the stream has been reached.
public int read(byte[] b, int offset, int num) throws IOException
It tries to read bytes (up to num bytes) into b starting at b[offset] and returns the actual number of bytes that were successfully read. Returns -1 if the end of the file is reached.
public void close() throws IOException
Closes this file input stream and releases any system resources associated with the stream. Further read attempts will generate an IOException.
public int read() throws IOException
Reads a byte of data from this input stream. Returns -1 if the end of the file is reached.
public int read(byte[] b) throws IOException
It tries to read bytes (up to b.length bytes) into b and returns the actual number of bytes that were successfully read. Returns -1 if the end of the file is reached.
public long skip(long n) throws IOException
It skips n bytes of input, returning the number of bytes actually ignored.

Here is a simple Java program for ByteArrayInputStream:

ByteArrayInputStreamExample.java

                            package com.tutorial.java.files;

                            import java.io.ByteArrayInputStream;
                            import java.io.IOException;

                            public class ByteArrayInputStreamExample {

                                public static void main(String[] args) throws IOException {
                                    String text = "I am just a text";
                                    byte bytes[] = text.getBytes();
                                    ByteArrayInputStream inputStream = null;
                                    try {
                                        inputStream = new ByteArrayInputStream(bytes);
                                        for (int i = 0; i < text.length(); i++) {
                                            int c;
                                            while ((c = inputStream.read()) != -1) {
                                                System.out.print((char) c);
                                            }
                                        }
                                    } finally {
                                        if (inputStream != null) {
                                            inputStream.close();
                                        }
                                    }

                                }
                            }
                        

Output:

                            I am just a text