A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/java_bytearrayinputstream.htm below:

Java - ByteArrayInputStream Class

Java - ByteArrayInputStream Class

The Java ByteArrayInputStream class contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.Following are the important points about ByteArrayInputStream −

Class declaration

Following is the declaration for java.io.ByteArrayInputStream class −

public class ByteArrayInputStream
   extends InputStream
Field

Following are the fields for java.io.ByteArrayInputStream class −

Class constructors Sr.No. Constructor & Description 1

ByteArrayInputStream(byte[] buf)

This creates a ByteArrayInputStream so that it uses buf as its buffer array.

2

ByteArrayInputStream(byte[] buf, int offset, int length)

This creates ByteArrayInputStream that uses buf as its buffer array.

Class methods Methods inherited

This class inherits methods from the following classes −

Example - Using ByteArrayInputStream available() method

The following example shows the usage of Java ByteArrayInputStream available() method.

ByteArrayInputStreamDemo.java
package com.tutorialspoint;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) {
      // Create a byte array
      byte[] data = {65, 66, 67, 68, 69}; // Corresponds to 'A', 'B', 'C', 'D', 'E'

      // Create a ByteArrayInputStream using the byte array
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

      // Check the number of bytes available initially
      System.out.println("Bytes available at the start: " + inputStream.available());

      // Read the first byte
      int firstByte = inputStream.read();
      System.out.println("Read byte: " + (char) firstByte);

      // Check the number of bytes available after reading one byte
      System.out.println("Bytes available after reading one byte: " + inputStream.available());

      // Read all remaining bytes
      while (inputStream.available() > 0) {
         int byteData = inputStream.read();
         System.out.println("Read byte: " + (char) byteData);
      }

      // Check the number of bytes available at the end
      System.out.println("Bytes available at the end: " + inputStream.available());
   }
}
Output

Let us compile and run the above program, this will produce the following result −

Bytes available at the start: 5
Read byte: A
Bytes available after reading one byte: 4
Read byte: B
Read byte: C
Read byte: D
Read byte: E
Bytes available at the end: 0
Explanation Example - Using ByteArrayInputStream close() method

The following example shows the usage of Java ByteArrayInputStream close() method.

ByteArrayInputStreamDemo.java
package com.tutorialspoint;

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

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) {
      // Create a byte array
      byte[] data = {72, 101, 108, 108, 111}; // Corresponds to 'H', 'e', 'l', 'l', 'o'

      // Create a ByteArrayInputStream
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

      // Read and print data from the stream
      try {
         int byteData;
         System.out.println("Reading bytes from ByteArrayInputStream:");
         while ((byteData = inputStream.read()) != -1) {
            System.out.print((char) byteData);
         }
         System.out.println();

         // Close the ByteArrayInputStream
         inputStream.close();
         System.out.println("Stream closed successfully.");
      } catch (IOException e) {
         System.err.println("An I/O error occurred: " + e.getMessage());
      }
   }
}
Output

Let us compile and run the above program, this will produce the following result −

Reading bytes from ByteArrayInputStream:
Hello
Stream closed successfully.
Explanation Example - Using ByteArrayInputStream mark() method

The following example shows the usage of Java ByteArrayInputStream mark() method.

ByteArrayInputStreamDemo.java
package com.tutorialspoint;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamMarkExample {
   public static void main(String[] args) {
      // Create a byte array
      byte[] data = {65, 66, 67, 68, 69}; // Corresponds to 'A', 'B', 'C', 'D', 'E'

      // Create a ByteArrayInputStream
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

      // Read and print the first two bytes
      System.out.println("Reading first two bytes:");
      System.out.println((char) inputStream.read()); // Read 'A'
      System.out.println((char) inputStream.read()); // Read 'B'

      // Mark the current position in the stream
      inputStream.mark(0); // Mark at position 2 (after 'B')
      System.out.println("Marked the current position.");

      // Read the next two bytes
      System.out.println("Reading next two bytes:");
      System.out.println((char) inputStream.read()); // Read 'C'
      System.out.println((char) inputStream.read()); // Read 'D'

      // Reset the stream to the marked position
      inputStream.reset();
      System.out.println("Stream reset to the marked position.");

      // Read again from the marked position
      System.out.println("Reading bytes again from the marked position:");
      System.out.println((char) inputStream.read()); // Read 'C' again
      System.out.println((char) inputStream.read()); // Read 'D' again

      // Read the last byte
      System.out.println((char) inputStream.read()); // Read 'E'
   }
}
Output

Let us compile and run the above program, this will produce the following result −

Reading first two bytes:
A
B
Marked the current position.
Reading next two bytes:
C
D
Stream reset to the marked position.
Reading bytes again from the marked position:
C
D
E
Explanation

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