Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding getInputStream and getOutputStream

Tags:

java

stream

Here is a code

import java.io.*;
import java.net.*;
public class Whois
{
    public static void main(String[] args)
        throws Exception
    {
        // TODO Auto-generated method stub
        int c;
        Socket s = new Socket("whois.internic.net",43);
        *InputStream in = s.getInputStream();
        *OutputStream out = s.getOutputStream();
        String str = (args.length == 0 ? "osborne.com" : args[0] ) + "\n";
        byte buf[] = str.getBytes();
        *out.write(buf);
        System.out.print("hey baby");
        while ((c=in.read()) != -1)
        {
            System.out.print((char) c);
        }
        s.close();
    }
}

I have marked the statements that I have problems understanding. I do not understand what OutputStream object out will hold when it is assigned s.getOutputStream() and what is the need of passing buf to out by out.write(buf).

I have learned Input and Output Streams using files but I do not understand getinputstream and outputstreams. I have googled it, read it here on SO as well as from many different books and from oracle documents as well. Please discuss it in detail.

I know how to read from files and how to write to them, but here I do not understand what is the need of passing buf array which holds only a string. What I mean to ask is that when in has the input stream of the socket why can't we directly just read from it? What exactly is a socket inputstream and outputstream?

I found something here on SO here is the link "https://stackoverflow.com/questions/12715321/java-networking-explain-inputstream-and-outputstream-in-socket", here an answer by DNA says

In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data)."

This is confusing me, when outputStream is used to send data via socket what was the need of out.write(buf)? Why do we need to send "google.com" to outputStream?

like image 744
Junaid Shirwani Avatar asked Mar 21 '14 16:03

Junaid Shirwani


2 Answers

first thing you need to understand is what is STREAM

A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

****Next is type of streams****

 we have byte stream and character stream.

enter image description here

classes we have in Input Stream and output stream 

enter image description here

as the name suggests in simple terms input stream is used to input the data and output stream is used to output the data

Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream. also

Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are , FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.

For reference

  1. What is InputStream & Output Stream? Why and when do we use them?

  2. java DataOutputStream getOutputStream() getInputStream()

Example for getInputStream and getOutputStream

  1. http://zerioh.tripod.com/ressources/sockets.html

New Link http://docs.oracle.com/javase/tutorial/essential/io/buffers.html

like image 180
Divya Avatar answered Oct 12 '22 05:10

Divya


InputStream in and OutputStream out will hold references to two types of streams that you can either read data from or write data to. Don't expect them to hold values from the stream itself - instead, they hold the ability to work with the stream. When you create these objects, you're not sending/receiving any data - you're just getting the object that you can use to send/receive data.

out.write(buf) is sending the contents of buf over the Socket so that any readers of the socket (in your case, in) can receive that data. Whatever data is sent to out will be seen on the other side of the Socket by an InputStream.

like image 25
David Koelle Avatar answered Oct 12 '22 05:10

David Koelle