digitization, transformation, binary

Class OutputStream in Java

Class OutputStream in Java:

OutputStream class is an abstract superclass of all classes that represent an output stream of bytes. It defines the basic output methods that all output stream classes provide.

An application that needs to define a subclass of OutputStream must always provide at least a method that writes one byte of output.

write() writes a single byte or an array or subarray of bytes.

flush() forces any buffered output to be written. flush() tells the OutputStream to write any buffered output to the underlying device, which may be a disk drive or a network.

close() closes the stream and frees up any system resources associated with it. The stream may not be used once close() has been called.

Some OutputStream subclasses may implement buffering to increase efficiency.

The classes and subclasses of Class OutputStream are:

 

ClassSubclassFurther Subclass
OutputStreamPipedOutputStream
FilterOutputtStreamDataOutputStream
BufferedOutputStream
PrintStream
ByteArrayOutputStream
FileOutputStream
ObjectOutputStream

 

The class structure of OutputStream is given as:

public abstract class java.io.OutputStream extends java.lang.Object{
//methods:
public void close();//closes the input source;further read attempt on a closed input will generate an IOException.
public void flush();//flush the file buffer
public void write(byte b[])throws IOException;;//writes an entire byte array i.e block of bytes at a time.
public void write(byte b[],int offSet,int length)throws IOException;;//write out length number of bytes from the block of  bytes b starting at offset offSet.
public abstract void write(int b)throws IOException;//writes one byte at a time
}
 

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
The details of the methods are given as:

public void close();

public void close() method closes the output stream and releases any resources associated with it.

The implementation of the close() method in OutputStream does nothing; a subclass should
override this method to handle cleanup for the stream.

public void flush();

public void flush() method forces any bytes that may be buffered by the output stream to be written.

The implementation of flush() in OutputStream does nothing; a subclass should override
this method as needed.

public abstract void write(int b);

public abstract void write(int b) method writes a byte of output. The method blocks until the byte is actually written.
A subclass of OutputStream must implement this method.

Parameter
b – The value to write to the stream.

public void write(byte[] b);

public void write(byte[] b) method writes the bytes from the given array by calling write(b, 0, b.length). The method blocks until the bytes are actually written.

A subclass does not usually need to override this method, as it can override write(byte[],
int, int) and have write(byte[]) work automatically.

Parameter
b – An array of bytes to write to the stream.

public void write(byte[] b, int off, int len);

public void write(byte[] b, int off, int len) method writes len bytes of output from the given array, starting at offset off. The method blocks until the bytes are actually written.

The implementation of this method in OutputStream uses write(int) repeatedly to write
the bytes. Although it is not strictly necessary, a subclass should override this method to write a block of data more efficiently.

Parameter
b – An array of bytes to write to the stream.
off – An offset into the byte array.
len – The number of bytes to write.

Apart from these OutputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *