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
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.
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.
The following constructors are used for supporting the ByteArrayInputStream class of java.
ByteArrayInputStream[byte a]
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)
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.
Below are the methods of Java ByteArrayInputStream:
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.
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.
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.
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.
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.
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.
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.
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.
Below are the examples of Java ByteArrayInputStream:
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!"); } } }
Output:
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); } }
Output:
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); } }
Output:
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()); } }
Output:
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 ); } }
Output:
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>
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!