Table of Contents
How Does ByteArrayInputStream Class Work in Java?
Constructor of Java ByteArrayInputStream
Methods of Java ByteArrayInputStream
1. public int read()
2. public int read(byte[] r, int off, int len)
3. public int available()
4. public void mark(int read)
5. public long skip(long n)
6. public boolean markSupported()
7. public void reset()
8. public void close()
Examples to Implement of Java ByteArrayInputStream
Example #6
Conclusion
Home Java javaTutorial Java ByteArrayInputStream

Java ByteArrayInputStream

Aug 30, 2024 pm 04:09 PM
java

The ByteArrayInputStream class is composed of two phases: Byte Array and one for Input Stream. Byte Array plays a pivotal role in holding the important data and the necessary bytes with respect to the input stream. Java ByteArrayInputStream class comprises of the internal buffer, which is responsible for reading the byte array as a stream. Byte array passes the data to be fed as an input stream. This data, when present in the buffer, gets increased. Traversal and retrieval of data become flexible and seamless using this class.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

public class ByteArrayInputStream extends InputStream
Copy after login

As part of the Java ByteArrayInputStream, the flow of execution is as follows:

A public class of ByteArrayInputStream is declared, which extends the interface as a medium to interact with the stream being put with the InputStream.

How Does ByteArrayInputStream Class Work in Java?

The working of ByteArrayInputStream is quite straight forward whose main principle is to convert the byte array into the input stream seamlessly. As an added advantage of this class, the buffer makes sure that the java.io.ByteArrayInput package consisting of the API lets you read the data from byte arrays as streams of the byte array, which is then turned into the input stream, which will be fed into the buffer. The ByteArrayInputstream is a subclass of the InputStream class. Therefore, the ByteArrayInputStream can be used as an InputStream. If the data is arranged and stored in an array as an input stream, this can be wrapped into a byte array and can turn into a stream easily. Once the ByteArrayInputStream object is ready, then a list of helper methods can be used for reading and doing operations on the stream.

Constructor of Java ByteArrayInputStream

The following constructors are used for supporting the ByteArrayInputStream class of java.

ByteArrayInputStream[byte a]
Copy after login

As part of the Java ByteArrayInputStream, this constructor works in a way that is used for accepting a prepared set of the byte array, especially as a parameter in the memory of the internal buffer present as part of the package and the class.

ByteArrayInputStream(byte [] a, int off, int len)
Copy after login

This constructor as part of the java ByteArrayInputStream class passes three arguments as a parameter from the stream, which takes into account the byte array, integer off and length of the defined integer, and the flow also maintains the order when fed as an input in the memory of the buffer which means that first the byte array a[], then two integer values where off Is the first byte to be read followed by the length of the number of bytes to be read.

Methods of Java ByteArrayInputStream

Below are the methods of Java ByteArrayInputStream:

  1. public int read()
  2. public int read(byte[] r, int off, int len)
  3. public int available()
  4. public void mark(int read)
  5. public long skip(long n)
  6. public boolean markSupported()
  7. public void reset()
  8. public void close()

1. public int read()

This method is part of the ByteArrayInputStream class is used for reading the next byte available in the already flowing input stream, which returns an int type of range 0-255. If there is no byte present in the buffer as the input stream, it has returned to an end of the stream, which returns -1 as value.

2. public int read(byte[] r, int off, int len)

This method reads the bytes upto the length of the number of bytes starting from the off from the input stream into an array and returns the total number of bytes until the last byte of -1 gets returned.

3. public int available()

As part of the ByteArrayInputStream class, this method is used for reading and acknowledging the total number of bytes that will be available from the Input stream to read.

4. public void mark(int read)

As part of the ByteArrayInputStream class, this method is used for marking and setting the current position of the input stream. Basically, it sets a reading limit for obtaining the maximum number of bytes that can be read before the marked limit set becomes invalid.

5. public long skip(long n)

As part of the ByteArrayInputStream class, this method is used for skipping the number of bytes in the input stream as an argument to the method.

6. public boolean markSupported()

This method is used for testing the input stream whether it supports the marked limit or functioning without its presence. It has one special feature that whenever this mark supported is used as a method; it returns a value always true.

7. public void reset()

This method is used to reset the position of the marker as it is provoked by the mark() method. The added advantage is to reposition and reset the marker for traversing.

8. public void close()

This method plays a crux to release all the resources once close. When It gets called, the input stream gets closed, and the stream gets associated with the garbage collector.

Examples to Implement of Java ByteArrayInputStream

Below are the examples of Java ByteArrayInputStream:

Example #1

This program is used to illustrate the read() method byte by byte in ByteArrayInputStream.

Code:

import java.io.*;
public class Input_Buffer_Stream1 {
public static void main(String[] args) throws Exception {
byte guava = 0;
byte pine = 0;
byte kiwi = 0;
byte orange = 0;
byte[] buffr = {guava, pine, kiwi,orange};
ByteArrayInputStream fruits = new ByteArrayInputStream(buffr);
int k = 0;
while((k=fruits.read())!=-1) {
System.out.println("These fruits are really tasty and relising to have & Its the time to have ad enjoy!");
}
}
}
Copy after login

Output:

Java ByteArrayInputStream

Example #2

This program illustrates the available method of ByteArrayInputStream.

Code :

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream2 {
public static void main(String[] args) {
byte[] buffr= {20,22};
ByteArrayInputStream bytes = new ByteArrayInputStream(buffr);
int bytes_Available = bytes.available();
System.out.println("no of bytes available:" + bytes_Available);
}
}
Copy after login

Output:

Java ByteArrayInputStream

Example #3

This program illustrates the mark method of the ByteArrayInputStream class.

Code:

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream3 {
public static void main(String[] args) {
byte[] buffr= {20,22,19,10};
ByteArrayInputStream bytes_arr = new ByteArrayInputStream(buffr);
bytes_arr.mark(0);
System.out.println("These are the marked bytes of the stream:" + bytes_arr);
}
}
Copy after login

Output:

Java ByteArrayInputStream

Example #4

This program illustrates the skip method of the ByteArrayInputStream class.

Code :

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream4 {
public static void main(String[] args) throws Exception {
byte[] buffr= {20,22,18,10};
ByteArrayInputStream learning = null;
learning = new ByteArrayInputStream(buffr);
long num = learning.skip(1);
System.out.println("Char : "+(char)learning.read());
}
}
Copy after login

Output:

Java ByteArrayInputStream

Example #5

This program illustrates the boolean mark supported method of the ByteArrayInputStream class.

Code :

import java.io.ByteArrayInputStream;
public class Input_Buffer_Stream_5 {
public static void main(String[] args) {
byte[] buff = {15, 18, 20, 40, 52};
ByteArrayInputStream educba = null;
educba = new ByteArrayInputStream(buff);
boolean checker = educba.markSupported();
System.out.println("\n mark is supported for : "+ checker );
}
}
Copy after login

Output:

Java ByteArrayInputStream

Example #6

This program illustrates the presence of boolean mark, reset, and close method of the ByteArrayInputStream class.

Code:

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Input_Buffer_Stream_5 {
public static void main(String[] args) {
byte[] buff = {15, 18, 20, 40, 52};
ByteArrayInputStream educba = null;
educba = new ByteArrayInputStream(buff);
boolean checker = educba.markSupported();
System.out.println("\n mark is supported for : "+ checker );
if(educba.markSupported())
{
educba.reset();
System.out.println("\n supports for the marking limit once reset");
System.out.println("Char : "+(char)educba.read());
}
else
{
System.out.println("It is not supporting the positioning using reset method");
}
System.out.println("educba.markSupported() supported reset() : "+checker);
if(educba!=null)
{
try {
educba.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
} <strong>Output:</strong>
Copy after login

Java ByteArrayInputStream

Conclusion

Java ByteArrayInputStream is a class that has a lot of capability and versatility to play around with the arrays in the internal buffer, which is the beauty of the class. It does not require any external class or plugin to support its base methods which work with a lot of functionality. ByteArrayInputStream together forms a perfect combination to feed the input and output stream related data.

The above is the detailed content of Java ByteArrayInputStream. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles