Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.io.BufferedInputStream class in Java
Next article icon

Java.io.ByteArrayInputStream class in Java

Last Updated : 30 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

ByteArrayInputStream class of java.io package contains all the buffers, containing bytes to be read from the Input Stream. There is no IO exception in case of ByteArrayInputStream class methods. Methods of this class can be called even after closing the Stream, there is no effect of it on the class methods. The class view is as follows: 

--> java.io Package     --> ByteArrayInputStream Class   

Syntax: 

public class ByteArrayInputStream extends InputStream

There are certain Fields:

  • protected byte[] buf: An array of bytes that was provided by the creator of the stream.
  • protected int count: The index one is greater than the last valid character in the input stream buffer.
  • protected int mark: The currently marked position in the stream.
  • protected int pos: This is the index of the next character to read from the input stream buffer.

Constructor of ByteArrayInputStream Class

Constructor Action Performed
ByteArrayInputStream(byte[] buffer)  It creates ByteArrayInputStream to use buffer array – “buffer”.
ByteArrayInputStream(byte[] buf, int offset, int length)  It creates ByteArrayInputStream that uses some part of “buffer” i.e. buffer array

Methods of ByteArrayInputStream class

Method  Action Performed
available() It tells the total number of bytes from the input stream to be read.
close() It closes the input stream and releases system resources.
mark() It marks the current position of the input stream which means setting the read limit. 
markSupported() It tests if this input stream supports the mark and reset method.
read() It reads the next byte of data from the input stream.
reset() It repositions the input stream to the marked position and is called the mark() method 
skip() Skips the “args” in the input stream.

Implementation:

Java




// Java Program to Demonstrate ByteArrayInputStream Class
// Via mark(), read(), skip(), available(),
// markSupported(), close(), reset() Method
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Declaring and initializing byte array
        byte[] buffer = { 71, 69, 69, 75, 83 };
        ByteArrayInputStream geek = null;
 
        // Try block to check for exceptions
        try {
            geek = new ByteArrayInputStream(buffer);
 
            // Telling the no. of bytes to be read
            // using available() method
            int number = geek.available();
            System.out.println(
                "Use of available() method : " + number);
 
            // Reading and printing Characters one by one
            // using read() method
            System.out.println("\nChar : "
                               + (char)geek.read());
            System.out.println("Char : "
                               + (char)geek.read());
            System.out.println("Char : "
                               + (char)geek.read());
 
            // Usage of mark() method
            geek.mark(0);
 
            // Skipping 'k' from "GEEKS"
            // using skip() method
            geek.skip(1);
            System.out.println(
                "skip() method comes to play");
            System.out.println(
                "mark() method comes to play");
            System.out.println("Char : "
                               + (char)geek.read());
 
            // Usage of markSupported() method
            boolean check = geek.markSupported();
            System.out.println("\nmarkSupported() : "
                               + check);
 
            if (geek.markSupported()) {
 
                // Repositioning the stream to marked
                // positions using reset() method
                geek.reset();
 
                System.out.println("\nreset() invoked");
                System.out.println("Char : "
                                   + (char)geek.read());
                System.out.println("Char : "
                                   + (char)geek.read());
            }
            else {
                System.out.println(
                    "reset() method not supported.");
            }
 
            System.out.println(
                "geek.markSupported() supported reset() : "
                + check);
        }
 
        // Catch block to handle the exceptions
        catch (Exception except) {
 
            // Displaying the exception along with line
            // number using printStackTrace() method
            except.printStackTrace();
        }
 
        // finally block that execute for sure
        finally {
 
            // Releasing the resources back to GC when
            // closes
            if (geek != null) {
 
                // Closing the file and releasing resources
                // using close() method
                geek.close();
            }
        }
    }
}
 
 
Output
Use of available() method : 5  Char : G Char : E Char : E skip() method comes to play mark() method comes to play Char : S  markSupported() : true  reset() invoked Char : K Char : S geek.markSupported() supported reset() : true

 



Next Article
Java.io.BufferedInputStream class in Java

M

Mohit Gupta
Improve
Article Tags :
  • Java
  • Java-I/O
Practice Tags :
  • Java

Similar Reads

  • Java.io.ByteArrayOutputStream() Class in Java
    java.io.ByteArrayOutputStream class creates an Output Stream for writing data into byte array. The size of buffer grows automatically as data is written to it. There is no affect of closing the byteArrayOutputStream on the working of it's methods. They can be called even after closing the class. Thu
    4 min read
  • Java.io.BufferedInputStream class in Java
    A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refil
    4 min read
  • Java.io.FilterInputStream Class in Java
    Filter Streams filters data as they read and write data in the Input Stream, filters it and pass it on to the underlying Streams. Filter Streams are FilterInputStream FilterOutput Stream FilterInputStream : Java.io.FilterInputStream class works almost like InputStream class in Java but what it does
    9 min read
  • Java.io.DataOutputStream in Java
    A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. Let us do discuss the constructor of this class prior to moving ahead to the methods of this class.  Constructor:
    5 min read
  • ByteArrayInputStream close() method in Java with Examples
    The close() method is a built-in method of the Java.io.ByteArrayInputStream closes the input stream and releases system resources associated with this stream to Garbage Collector. Syntax: public void close() Parameters: The function does not accepts any parameter. Return Value: The function returns
    2 min read
  • ByteArrayInputStream read() method in Java with Examples
    The read() method of ByteArrayInputStream class in Java is used in two ways: 1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read into the form of an integer and if the input stream is e
    3 min read
  • ByteArrayInputStream mark() method in Java with Examples
    The mark() method in ByteArrayInputStream in Java is used to set a mark at the current position in the stream. This allows you to later reset the stream to this marked position using the reset() method. The mark() method is particularly useful when you need to read from a certain point and then retu
    3 min read
  • Java.io.DataInputStream class in Java | Set 2
    Java.io.DataInputStream class in Java | Set 1 More Methods: byte readByte() : Reads and returns one input byte. Syntax:public final byte readByte() throws IOException Returns: the next byte of this input stream as a signed 8-bit byte. Throws: EOFException IOException float readFloat() : Reads four i
    3 min read
  • Java.io.DataInputStream class in Java | Set 1
    A data input stream enables an application to read primitive Java data types from an underlying input stream in a machine-independent way(instead of raw bytes). That is why it is called DataInputStream - because it reads data (numbers) instead of just bytes.  An application uses a data output stream
    3 min read
  • Java.io.FilterOutputStream Class in Java
    java.io.FilterInputStream Class in Java Java.io.FilterOutputStream class is the superclass of all those classes which filters output streams. The write() method of FilterOutputStream Class filters the data and write it to the underlying stream, filtering which is done depending on the Streams. Decla
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences