CH - 3 Part - 2 Stream

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Stream

Stream

A stream of information
JAVA
SOURCE INFORMATION: READS
PROGRAM

A stream of information
JAVA DESTINATION
INFORMATION: WRITES
PROGRAM
Stream to make I/O operation fast.

java.io package – for input and output operations

A stream is a sequence of data.

A stream is composed of bytes.

Eg. Like stream of water flows


STREAMS

Character Byte
Byte streams are used to perform input and output of 8-bit bytes.

Character streams are used to perform input and output for 16-bit
bytes
• Eg. FileReader and FileWriter classes

Frequently used classes: FileInputStream and FileOutputStream.

Streams created automatically


• System.out: standard output stream
• System.in: standard input stream
• System.err: standard error stream

• System.out.println("simple message");
• //returns ASCII code of 1st character
• int i=System.in.read();
• System.err.println("error message");
Character Streams
Byte Streams
• Types : Input Stream and Output Stream
• Output Stream: write data to a destination, it may be a file, an array, peripheral device or socket.
• Input Stream: Read data from a source, it may be a file, an array, peripheral device or socket.
InputStream and OutputStream
SENDER

import java.net.*;
import java.io.*;
// import java.io.File;
// import java.io.FileInputStream;
// import java.io.IOException
// import java.io.BufferedInputStream;
// import java.io.DataInputStream;
public class Server {
// IOException − If an I/O error occurs.
public static void main (String [] args ) throws IOException {

// ServerSocket - To assign Port Number


ServerSocket serverSocket = new ServerSocket(15123);

// To Create Client Socket using accept()


Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);

// File Path
File transferFile = new File ("D:\\P2PFILE\\SENDER.txt");
// length of file in bytes is stored as int in bytearray
byte [] bytearray = new byte [(int)transferFile.length()];
// To read bytes from file
FileInputStream fin = new FileInputStream(transferFile);
// To read input as stream using internally created buffer array (many bytes at a time)
BufferedInputStream bin = new BufferedInputStream(fin);

// int read(byte[] b, int off, int len)


bin.read(bytearray,0,bytearray.length);

// To write bytes from file.


OutputStream os = socket.getOutputStream();
System.out.println("Sending Files to Client...");

// write(byte[] ary, int off, int len)


// ary – the data, off – the start offset in the data, len – number of bytes to write
// i.e. write len bytes from the byte array starting at offset off to the file output stream.
os.write(bytearray,0,bytearray.length);

// off = 0, the first byte written,


// off +len-1, means, is the last byte written

/* Flush() - flushes this output stream and forces any buffered output bytes to be written
out. (file or console) */
os.flush();

// close the socket


socket.close();
System.out.println("File transfer complete"); } }
RECEIVER
// embedded with IOException (if any I/O exception occurs)
public static void main (String [] args ) throws IOException {

// File Size in Bytes


int filesize=1022386;

// bytesRead contain the current statistics of the bytes read from the input ie inputstream,
int bytesRead;

// currentTot contains the total number of bytes read.


int currentTot = 0;

// localhost and port


Socket socket = new Socket("127.0.0.1",15123);

// creates a buffer array as bytearray for filesize = 1022386


byte [] bytearray = new byte [filesize];

// for input as a stream of bytes


InputStream is = socket.getInputStream();

//This creates a file output stream to write to the file


FileOutputStream fos = new FileOutputStream("D:\\P2PFILE2\\RECEIVED.txt");
// To write data to the output file via a byte array
BufferedOutputStream bos = new BufferedOutputStream(fos);

// To read the data from the inputstream using the read method
// The data read from the input channel is stored in the byte array
bytesRead = is.read(bytearray,0,bytearray.length);

// initially set currentTot to number of bytes read.


currentTot = bytesRead;

// It read again from the input stream and


// if the bytesRead is >=0 then we update out currentTot object.
// byteRead is -1 // i.e. there is no data left on the inputstream (then loop exits.)

do {
// Read the bytes on the file
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);

// write the bytes


bos.write(bytearray, 0 , currentTot);

// flush the stream, close the file and socket


bos.flush(); bos.close(); socket.close();
THANK YOU

You might also like