ByteArrayInputStream

ByteArrayInputStream allows an application to read data from a byte array as if it were an input stream. It is especially useful for simulating input for testing or for reading memory-stored data byte-by-byte.

Commonly Used Constructors and Methods

Simple Program – Reading from ByteArrayInputStream

LotusJavaPrince wants to read a byte array "Hello, Mahesh" using ByteArrayInputStream and print each character.

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

public class SimpleByteArrayInputStream {
    public static void main(String[] args) {
        byte[] data = "Hello, Mahesh".getBytes();

        try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
            int b;
            while ((b = bais.read()) != -1) {
                System.out.print((char) b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Hello, Mahesh

ByteArrayInputStream in Validation System

Problem Statement:

LotusJavaPrince is building a system to parse CSV records stored in memory and validate user data like email and age. Instead of reading from a file, he uses ByteArrayInputStream to simulate file-like reading from a byte array.

import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class UserValidator {
    public static void main(String[] args) {
        String csvData = "name,email,age\n" +
                         "LotusJavaPrince,lotus@java.io,30\n" +
                         "Mahesh,mahesh@invalid,17\n" +
                         "Tester,test@example.com,25\n";

        byte[] byteData = csvData.getBytes();

        try (ByteArrayInputStream bais = new ByteArrayInputStream(byteData);
             BufferedReader reader = new BufferedReader(new InputStreamReader(bais))) {

            String line;
            boolean isFirstLine = true;

            while ((line = reader.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue; // skip header
                }

                String[] fields = line.split(",");
                String name = fields[0];
                String email = fields[1];
                int age = Integer.parseInt(fields[2]);

                boolean validEmail = email.contains("@") && email.contains(".");
                boolean validAge = age >= 18;

                System.out.println("User: " + name);
                System.out.println("Email Valid: " + validEmail);
                System.out.println("Age Valid: " + validAge);
                System.out.println("---");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

User: LotusJavaPrince
Email Valid: true
Age Valid: true
---
User: Mahesh
Email Valid: false
Age Valid: false
---
User: Tester
Email Valid: true
Age Valid: true
---Code language: JavaScript (javascript)

ByteArrayInputStream is a memory-based input stream for byte arrays.

  • Great for unit testing or converting data held in memory into stream-like format.
  • Supports all basic stream operations: read(), available(), reset(), etc.
  • It is self-contained and does not require external files or resources.
Scroll to Top