Byte Streams - IT magazine

IT magazine

Knowledge that matters

Byte Streams

Share This

Streams:
         Streams are used to transfer the data between program and source/destination. They transfer the data in unique way irrespective of source/destination. Streams are defined in java.io package in java. Depending up on the direction of the transfer the streams are classified in to two categories.
  • Input Stream: These streams carry the data from some source in to the program. The source may be a keyboard, a file …Here read operation is performed to input the data in to the program.
  • Output Stream: They carry the data from program to some destination. The destination may be a monitor, printer, a file … Here write operation is performed to send data from the program.
Streams that carry the data in form of bytes into the program from a source or from a program to These streams carry the data in the form of bytes. They use 8 bit (1 byte) of storage to read the data. Byte streams are present form the first version of the java. InputStream is the base class for all byte input streams and OutputStream is the base class for all byte output streams.
InputStream:
InputStream is an abstract class and is the base class for all byte input streams. Since it is abstract class objects cannot be created for InputStream class. This class defines various methods which can be used in all byte input streams. 
Methods:
  • int   read(): Reads next byte from the stream as integer and returns -1 if no data is available in the stream.
  • int    read(byte  b[]): Reads an array full of bytes from the stream and returns actual number of bytes read.
  • int    read(byte  b[], int start, int end): Reads bytes in to array from the specified start and end position form the stream.
  • long   available(): Returns how many number of bytes yet to be read in the stream.
  • long  skip(long  n): Skips specified number of bytes in the input stream and returns actual number of bytes skipped
  • void  mark(int readLimit): Marks the current position and it is valid till specified read limit.
  • boolean  isMarkSupported(): Checks whether the Stream supports the mark facility or not
  • void  reset(): Moves to the recent marked position or beginning of the stream
  • void close(): Closes the stream.
Byte Input Streams Hierarchy
Stream Class Name
Use
FileInputStream
used to read from files
PipedInputStream
used to read from pipes
ByteArrayInputStream
used to read from a byte array
StringBufferInputStream
used to read from a String buffer object
ObjectInputStream
used to read objects from an input stream
SequenceInputStream
used to combine two or more input streams
BufferedInputStream
provides buffer facility to the input stream
DataInputStream
used to read primitive data from the input stream
PushBackInputStream
provides un reading facility to the input stream

OutputStream:
OutputStream is base class for all byte output streams and it an abstract class. OutputStream defines a set of methods which are used to write the data into the streams.
Methods:

  • void    write(int b): Writes one byte to output stream.
  • void     write(byte  b[]): Writes an array full of bytes to output stream.
  • void    write(byte  b[], int start, int end): Writes bytes from array to output stream from the specified start and end position.
  • void  flush(): Flushes the output stream i.e., immediately releases the pending data from stream.
  • void close(): Closes the output stream.

Byte output Streams Hierarchy:
Stream Class Name
Use
FileOutputStream
used to write data into a file
PipedOutputStream
used to write data to a pipe
ByteArrayOutputStream
used to write data to a byte array
ObjectOutputStream
used to write objects to a output stream
BufferedOutputStream
provides buffer facility to the output stream
DataOutputStream
used to write primitive data to an input stream
zPrintStream
Used to print any data on output stream

No comments:

Post a Comment