ByteArrayOutputStream - sample program in Java

By Charles Viewed: 7793 times Emailed: 90 times Printed: 136 times Bookmark and Share



ByteArrayOutputStream is an implementation of an output stream that uses a byte array as the destination. ByteArrayOutputStream has two constructors, shown here:

ByteArrayOutputStream( )
ByteArrayOutputStream(int numBytes)

In the first form, a buffer of 32 bytes is created. In the second, a buffer is created with a size equal to that specified by numBytes. The buffer is held in the protected buf field of ByteArrayOutputStream. The buffer size will be increased automatically, if needed. The number of bytes held by the buffer is contained in the protected count field of ByteArrayOutputStream.

The following example demonstrates ByteArrayOutputStream:

// Demonstrate ByteArrayOutputStream.
import java.io.*;
class ByteArrayOutputStreamDemo {
public static void main(String args[]) throws IOException {
ByteArrayOutputStream f = new ByteArrayOutputStream();
String s = "This should end up in the array";
byte buf[] = s.getBytes();
f.write(buf);
System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
byte b[] = f.toByteArray();
for (int i=0; i<b.length; i++) {
System.out.print((char) b[i]);
}
System.out.println("\\nTo an OutputStream()");
OutputStream f2 = new FileOutputStream("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a reset");
f.reset();
for (int i=0; i<3; i++)
f.write('X');
System.out.println(f.toString());
}
}

When you run the program, you will create the following output. Notice how after the call to reset(), the three X's end up at the beginning.

Buffer as a string
This should end up in the array
Into array
This should end up in the array
To an OutputStream()
Doing a reset
XXX

This example uses the writeTo() convenience method to write the contents of f to test.txt. Examining the contents of the test.txt file created in the preceding example shows the result we expected: 

This should end up in the array




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