Last Updated : 30 Nov, 2022
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:
// 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();
}
}
}
}
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4