ByteArrayInputStream - sample program in Java

By Baski Viewed: 7411 times Emailed: 76 times Printed: 86 times Bookmark and Share



ByteArrayInputStream is an implementation of an input stream that uses a byte array as the source. This class has two constructors, each of which requires a byte array to provide the data source:

ByteArrayInputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)

Here, array is the input source. The second constructor creates an InputStream from a subset of your byte array that begins with the character at the index specified by start and is numBytes long.

The following example creates a pair of ByteArrayInputStreams, initializing them with the byte representation of the alphabet:

// Demonstrate ByteArrayInputStream.
import java.io.*;
class ByteArrayInputStreamDemo {
public static void main(String args[]) throws IOException {
String tmp = "abcdefghijklmnopqrstuvwxyz";
byte b[] = tmp.getBytes();
ByteArrayInputStream input1 = new ByteArrayInputStream(b);
ByteArrayInputStream input2 = new ByteArrayInputStream(b,
0,3);
}
}

The input1 object contains the entire lowercase alphabet, while input2 contains only the first three letters.

A ByteArrayInputStream implements both mark() and reset(). However, if mark() has not been called, then reset() sets the stream pointer to the start of the stream-which in this case is the start of the byte array passed to the constructor. The next example shows how to use the reset() method to read the same input twice. In this case, we read and print the letters "abc" once in lowercase and then again in uppercase.

import java.io.*;
class ByteArrayInputStreamReset {
public static void main(String args[]) throws IOException {
String tmp = "abc";
byte b[] = tmp.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(b);
for (int i=0; i<2; i++) {
int c;
while ((c = in.read()) != -1) {
if (i == 0) {
System.out.print((char) c);
} else {
System.out.print(Character.toUpperCase((char) c));
}
}
System.out.println();
in.reset();
}
}
}

This example first reads each character from the stream and prints it as is, in lowercase. It then resets the stream and begins reading again, this time converting each character to uppercase before printing. Here's the output:

abc
ABC




Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-01-01]Converting properties using PropertyEditors and Other Spring features worth mentioning
[2010-01-01]How to create an array and method in JSP
[2010-01-01]inheritance in Java
[2010-01-01]Use List, Set, SortedSet, LinkedHashSet, Map, LinkedHashMap in java
[2010-01-01]cloneable in Java
[2010-01-01]Java program for Associate keys with values
[2010-01-01]Java code for Enumeration and using java.lang.reflect.Array
[2010-01-01]Insert an element in Array, Search and Sort Array by using java program
[2010-01-01]Type Casting in Java
[2010-01-01]How to initialize an Array and how to copy the array
[2009-10-15]TCP Server and TCP Client in Java
[2009-10-03]Simple java program to add an element to specified index of Java ArrayList
[2009-10-03]How to use set, get basic and nested properties for Spring framework
[2009-10-03]How to access instance from an inner class and accessing outer class variables in java
[2009-10-03]How to use and access the inner class in java

More Latest News


Most Viewed Articles (in last 30 days)
How to use ArrayList in Java
How to Send SMS using Java Program (full code sample included)
How to use Iterator in Java
XML and Java - Parsing XML using Java Tutorial
Using substring( ) in Java
FileReader and FileWriter example program in Java
indexOf( ) and lastIndexOf( ) in Java
wait(), notify() and notifyAll() in Java - A tutorial
HashMap example in Java
compareTo( ) in Java
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Using StringTokenizer in Java
Transient vs Volatile modifiers in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
Most Emailed Articles (in last 30 days)
Components of program
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
Execute system commands in a Java Program
Why java is important to the Internet
indexOf( ) and lastIndexOf( ) in Java
History of Object
FileReader and FileWriter example program in Java
Recursion in java
What is Java?
Sample Java Script that displays a movable clock
Overview of JavaScript Objects
compareTo( ) in Java
How to use ArrayList in Java
Executing Java Scripts