ByteArrayInputStream in Java

ByteArrayInputStream class can turn a byte array into an InputStream. The ByteArrayInputStream class is a subclass of the InputStream class.

A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Constructors

ByteArrayInputStream in Java

Methods

Screen Shot 2020-04-11 at 10.46.31 AM

package com.java.io;

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

public class ByteArrayInputStreamExample {

    public static void main(String[] args) {

        ByteArrayInputStream byteArrayInputStream = null;
        String str = "Reading ByteArray Data";
        byte[] bytes = str.getBytes();
        int totalBytes = bytes.length;
        try {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            // Check if markSupported by ByteArrayInputStream
            System.out.println("Is Mark Supported:" + byteArrayInputStream.markSupported());
            if (byteArrayInputStream.markSupported()) {
                // marks the current position
                // 100 bytes to be read before the mark position becomes invalid
                byteArrayInputStream.mark(100);
            }
            // Skip 5 bytes
            byteArrayInputStream.skip(5);
            System.out.print("After Skip Content:");
            if (byteArrayInputStream.available() > 0) {
                for (int i = 0; i < totalBytes - 5; i++) {
                    // Reading one byte at a time
                    System.out.print((char) byteArrayInputStream.read());
                }

            }
            System.out.println("");
            // Reset to the earlier marked position
            System.out.println("Reset to the earlier marked position");
            byteArrayInputStream.reset();
            System.out.print("Reading bytes into byte Array:");
            byte b[] = new byte[totalBytes];
            if (byteArrayInputStream.available() > 0) {
                byteArrayInputStream.read(b);
                for (int i = 0; i < totalBytes; i++) {
                    System.out.print((char) b[i]);
                }
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            // Closing the streams
            if (byteArrayInputStream != null)
                try {
                    byteArrayInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

}
Is Mark Supported:true
After Skip Content:ng ByteArray Data
Reset to the earlier marked position
Reading bytes into byte Array:Reading ByteArray Data

Related Article

Java I/O Basics

Leave a Reply

Your email address will not be published. Required fields are marked *