--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Contact No : 7600044051
E-Mail : rupareleducation@gmail.com
Programming with JAVA
Byte Stream Classes in java
Byte stream classes are used to
perform reading and writing of 8-bit bytes. Streams being unidirectional in
nature can transfer bytes in one direction only, that is, either reading data
from the source into a program or writing data from a program to the
destination.
Therefore, Java further divides byte
stream classes into two classes, namely, InputStream class and OutputStrearn
class.
The subclasses of InputStrearn class
contain methods to support input and the subclasses of OutputStrearn class
contain output related methods.
Input Stream Classes
Java's input stream classes are used to read 8-bit bytes from the
stream. The InputStrearn class is the superclass for all byte-oriented input
stream classes. All the methods of this class throw an IOException. Being an
abstract class, the InputStrearn class cannot be instantiated hence, its
subclasses are used. Some of these are listed in Table
Table Input Stream Classes
Class
|
Description
|
FileInputStream
|
contains
methods to read bytes from a file
The Java.io.FileInputStream class
obtains input bytes from a file in a file system. What files are available
depends on the host environment. Following are the important points about
FileInputStream −
·
This
class is meant for reading streams of raw bytes such as image data.
·
For
reading streams of characters, use FileReader.
|
BufferedInputStream
|
contains
methods to read bytes from the buffer (memory area)
The Java.io.BufferedInputStream class
adds functionality to another input stream, the ability to buffer the input
and to support the mark and reset methods. Following are the important points
about BufferedInputStream −
·
When
the BufferedInputStream is created, an internal buffer array is created.
As bytes
from the stream are read or skipped, the internal buffer is refilled as
necessary from the contained input stream, many bytes at a time.
|
DataInputStream
|
contains
methods to read Java primitive data types
DataInputStream
The Java.io.DataInputStream class
lets an application read primitive Java data types from an underlying input
stream in a machine-independent way.Following are the important points about
DataInputStream −
·
An
application uses a data output stream to write data that can later be read by
a data input stream.
DataInputStream
is not necessarily safe for multithreaded access. Thread safety is optional
and is the responsibility of users of methods in this class.
|
Output Stream Class
The Java.io.OutputStream class
is the superclass of all classes representing an output stream of bytes. An
output stream accepts output bytes and sends them to some sink.Applications
that need to define a subclass of OutputStream must always provide at least a
method that writes one byte of output.
Method & Description
|
This method closes this output stream and releases
any system resources associated with this stream.
|
This method flushes this output stream and forces
any buffered output bytes to be written out.
|
This method writes b.length bytes
from the specified byte array to this output stream.
|
This method writes len bytes from
the specified byte array starting at offset off to this
output stream.
|
This method writes the specified byte to this output
stream.
|
FileOutputStream
|
The Java.io.FileOutputStream class
is an output stream for writing data to a File or to a FileDescriptor.
Following are the important points about FileOutputStream −
·
This
class is meant for writing streams of raw bytes such as image data.
·
For
writing streams of characters, use FileWriter.
|
BufferedOutputStream
|
The Java.io.BufferedOutputStream class implements a
buffered output stream. By setting up such an output stream, an application
can write bytes to the underlying output stream without necessarily causing a
call to the underlying system for each byte written.
|
DataOutputStream
|
The Java.io.DataOutputStream class lets an
application write primitive Java data types to an output stream in a portable
way. An application can then use a data input stream to read the data back
in.
|
StringTokenizer
The
StringTokenizer class is used to create a parser for String objects. It
parses strings according to a set of delimiter characters. It implements the
Enumeration interface in order to provide access to the tokens contained
within a string. StringTokenizer provides three constructors:
StringTokenizer(String s)
StringTokenizer(String s, String d)
StringTokenizer(String s, String d, boolean t)
s is the input string, d is a set of delimiters to be
used in the string parsing and t is a boolean value used to specify whether
the delimiter characters should be returned as tokens or not. The default
delimiter considered are : space, tab, newline, and carriage-return
characters.
The access methods provided
by StringTokenizer include the Enumeration methods, hasMoreElements() and
nextElement(), hasMoreTokens() and nextToken(), and countTokens()· The
countTokens() method returns the number of tokens in the string being parsed.
|
|
PipedStream
The PipedInputStream and
PipedOutputStream classes can be used to read and write data simultaneously.
Both streams are connected with each other using the connect() method of the
PipedOutputStream class.
The Java.io.PipedInputStream class
is a piped input stream that can be connected to a piped output stream, the
piped input stream then provides whatever data bytes are written to the piped
output stream.Following are the important points about PipedInputStream −
·
The
piped input stream contains a buffer, decoupling read operations from write
operations, within limits.
·
Attempting
to use both objects from a single thread is not recommended, as it may
deadlock the thread.
·
A
pipe is said to be broken if a thread that was providing data bytes to the
connected piped output stream is no longer alive.
The Java.io.PipedOutputStream class
is a piped output stream that can be connected to a piped input stream to
create a communications pipe.Following are the important points about
PipedOutputStream −
·
The
piped output stream is the sending end of the pipe.
·
Attempting
to use both objects from a single thread is not recommended as it may
deadlock the thread.
·
Data
is written to a PipedOutputStream object by one thread and data is read from
the connected PipedInputStream by some other thread.
The pipe is
said to be broken if a thread that was reading data bytes from the connected
piped input stream is no longer alive.
|
|
InputStreamReader
The InputStreamReader class of java.io package, is a bridge from byte
streams to character streams: It reads bytes and decodes them into characters
using a specified charset. The charset that it uses may be specified by name
or may be given explicitly, or the platform’s default charset may be
accepted. Each invocation of one of an InputStreamReader’s
read() methods may cause one or more bytes to be read from the underlying
byte-input stream. To enable the efficient conversion of bytes to characters,
more bytes may be read ahead from the underlying stream than are necessary to
satisfy the current read operation.
|
|
OutputStreamWriter
The Java OutputStreamWriter class
(java.io.OutputStreamWriter) is intended to wrap an OutputStream,
thereby turning the byte based output stream into a character
based Writer.
The Java OutputStreamWriter is useful if
you need to write characters to a file, encoded as e.g. UTF-8 or UTF-16. You
can then write the characters (char values) to
the OutputStreamWriter and it will encode them correctly and write
the encoded bytes to the underlying OutputStream.
|
|
ObjectInputStream
The Java ObjectInputStream class
(java.io.ObjectInputStream) enables you to read Java objects from
an InputStream instead of just raw bytes. You wrap
an InputStream in a ObjectInputStream and then you can
read objects from it. Of course the bytes read must represent a valid,
serialized Java object. Otherwise reading objects will fail.
Normally
you will use the ObjectInputStream to read objects written
(serialized) by a Java ObjectOutputStream .
You will see an example of that later.
|
|
ObjectOutputStream
The Java ObjectOutputStream class
(java.io.ObjectOutputStream) enables you to write Java objects to
an OutputStream instead of just raw bytes. You wrap
an OutputStream in a ObjectOutputStream and then you can
write objects to it.
The Java ObjectOutputStream is often used
together with a Java ObjectInputStream.
The ObjectOutputStream is used to write the Java objects, and
the ObjectInputStream is used to read the objects again. You will
see an example of this later.
|
Best Of Luck
Please Share and Give your reviews.... Thank you