SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Java Programming Language
Objectives


                In this session, you will learn to:
                   Describe Input/Output fundamentals
                   Construct node and processing streams, and use them
                   appropriately
                   Distinguish readers and writers from streams, and select
                   appropriately between them
                   Develop code to set up the network connection
                   Understand the TCP/IP Protocol
                   Use ServerSocket and Socket classes for implementation of
                   TCP/IP clients and servers




     Ver. 1.0                        Session 14                        Slide 1 of 26
Java Programming Language
I/O Fundamentals


               A stream can be thought of as a flow of data from a source
               or to a sink.
               A source stream initiates the flow of data, also called an
               input stream.
               A sink stream terminates the flow of data, also called an
               output stream.
               Sources and sinks are both node streams.
               Types of node streams are files, memory, and pipes
               between threads or processes.




    Ver. 1.0                      Session 14                       Slide 2 of 26
Java Programming Language
I/O Fundamentals (Contd.)


                The fundamental stream classes are:

                 Stream           Byte Streams      Character Streams
                 Source streams   InputStream       Reader
                 Sink streams     OutputStream      Writer




     Ver. 1.0                          Session 14                       Slide 3 of 26
Java Programming Language
Data Within Streams


                  Java technology supports two types of streams:
                      character
                      byte
                • Input and output of character data is handled by readers
                  and writers.
                • Input and output of byte data is handled by input streams
                  and output streams.
                • Normally, the term stream refers to a byte stream.
                • The terms reader and writer refer to character streams.




     Ver. 1.0                         Session 14                       Slide 4 of 26
Java Programming Language
The InputStream Methods


               The three basic read methods are:
                int read()
                int read(byte[] buffer)
                int read(byte[] buffer, int offset, int
                length)
               Other methods include:
                void close()
                int available()
                long skip(long n)
                boolean markSupported()
                void mark(int readlimit)
                void reset()


    Ver. 1.0                 Session 14               Slide 5 of 26
Java Programming Language
The OutputStream Methods


               The three basic write methods are:
                void write(int c)
                void write(byte[] buffer)
                void write(byte[] buffer, int offset, int
                length)
               Other methods include:
                void close()
                void flush()




    Ver. 1.0                 Session 14               Slide 6 of 26
Java Programming Language
The Reader Methods


               The three basic read methods are:
                 int read()
                 int read(char[] cbuf)
                 int read(char[] cbuf, int offset, int
                 length)
               Other methods include:
                 void close()
                 boolean ready()
                 long skip(long n)
                 boolean markSupported()
                 void mark(int readAheadLimit)
                 void reset()


    Ver. 1.0                 Session 14                  Slide 7 of 26
Java Programming Language
The Writer Methods


                The basic write methods are:
                  void write(int c)
                  void write(char[] cbuf)
                  void write(char[] cbuf, int offset, int
                  length)
                  void write(String string)
                  void write(String string, int offset, int
                  length)
                Other methods include:
                  void close()
                  void flush()




     Ver. 1.0                 Session 14               Slide 8 of 26
Java Programming Language
Node Streams


               Various types of Character and Byte Stream classes are:

                 Type             Character Streams   Byte Streams

                 File             FileReader          FileInputStream
                                  FileWriter          FileOutputStream

                 Memory: array    CharArrayReader     ByteArrayInputStream
                                  CharArrayWriter     ByteArrayOutputStream

                 Memory: string   StringReader        N/A
                                  StringWriter

                 Pipe             PipedReader         PipedInputStream
                                  PipedWriter         PipedOutputStream




    Ver. 1.0                           Session 14                             Slide 9 of 26
Java Programming Language
I/O Stream Chaining


                  Input Stream Chain:

                    Data Source                                                                  Program

                                    FileInputStream
                                                      BufferedInputStream   DataInputStream




                • Output Stream Chain:

                    Program                                                                   Data Sink
                                                                    FileOutputStream
                                                 BufferedOutputStream
                              DataOutputStream




     Ver. 1.0                                    Session 14                                                Slide 10 of 26
Java Programming Language
Processing Streams


                • Processing Streams are Node Streams that use filters in
                   between while transferring the data.
                • Various types of Processing Streams are:

                   Type                  Character Streams    Byte Streams
                   Buffering             BufferedReader       BufferedInputStream
                                         BufferedWriter       BufferedOutputStream
                   Filtering             FilterReader         FilterInputStream
                                         FilterWriter         FilterOutputStream
                   Converting between    InputStreamReader
                   bytes and character   OutputStreamWriter

                   Performing object                          ObjectInputStream
                   serialization                              ObjectOutputStream
                   Performing data                            DataInputStream
                   Conversion                                 DataOutputStream
                   Counting              LineNumberReader     LineNumberInputStream




     Ver. 1.0                                  Session 14                             Slide 11 of 26
Java Programming Language
InputStream


               The InputStream Class Hierarchy:




    Ver. 1.0                     Session 14       Slide 12 of 26
Java Programming Language
OutputStream


               The OutputStream Class Hierarchy:




    Ver. 1.0                    Session 14         Slide 13 of 26
Java Programming Language
Reader Class


                The Reader Class Hierarchy:




     Ver. 1.0                    Session 14   Slide 14 of 26
Java Programming Language
Writer Class


                The Writer Class Hierarchy:




     Ver. 1.0                     Session 14   Slide 15 of 26
Java Programming Language
Networking


               Basics of Networking:
                  Computers running on the Internet communicating to each
                  other using the Transmission Control Protocol (TCP) / Internet
                  Protocol (IP).


                     Client.bar.com
                     18000


                                                    Server. foo.com
                                                    3000              Port no.


                      Client.baz.com
                      18002




    Ver. 1.0                           Session 14                            Slide 16 of 26
Java Programming Language
Networking (Contd.)


                Networking with Java Technology:
                   Sockets:
                    • Sockets hold two streams, an input stream and an output stream.
                    • Each end of the socket has a pair of streams.
                   Setting Up the Connection:
                      Setup of a network connection is similar to a telephone.
                      One end must dial the other end, which must be listening.




     Ver. 1.0                        Session 14                                   Slide 17 of 26
Java Programming Language
Networking (Contd.)


                  To address the connection, include the following:
                   • The address or name of remote machine.
                   • A port number to identify the purpose at the server.
                – Port numbers range from 0–65535




     Ver. 1.0                        Session 14                             Slide 18 of 26
Java Programming Language
Networking (Contd.)


                Java Networking Model:



                        Server
                      SeverSocket(port#)        Register with
                     ServerSocket.accept()      this service        Client
                                                Wait for a
                                                                 Socket(host, port#)
                           Socket()             connection      (Attempt to connect)

                        OutputStream                               OutputStream
                        InputStream                                InputStream


                        Socket.close()
                                                                   Socket.close()




     Ver. 1.0                            Session 14                                    Slide 19 of 26
Java Programming Language
ServerSocket and Socket Classes


                Code Snippet for Creating Minimal TCP/IP Server:
                 ServerSocket s = null;
                 s = new ServerSocket(5432); //Register your service
                on port 5432
                while (true) // Run the listen/accept loop forever
                {
                  Socket s1 = s.accept(); // Wait here and listen for a
                  connection
                  OutputStream s1out = s1.getOutputStream();
                  // Get output stream associated with the socket
                  BufferedWriter bw = new BufferedWriter(new
                  OutputStreamWriter(s1out));
                  bw.write(“Hello Net World!n”); // Send your
                  string!


     Ver. 1.0                       Session 14                      Slide 20 of 26
Java Programming Language
ServerSocket and Socket Classes (Contd.)


                  bw.close(); // Close the connection, but not the server
                  socket
                  s1.close();
                 }
                Code Snippet for Creating Minimal TCP/IP Client:
                // Open your connection to a server, at port 5432
                // localhost used here
                Socket s1 = new Socket("127.0.0.1", 5432);
                // Get an input stream from the socket
                InputStream is = s1.getInputStream();
                //Decorate it with a "data" input stream
                DataInputStream dis = new
                DataInputStream(is);



     Ver. 1.0                        Session 14                     Slide 21 of 26
Java Programming Language
ServerSocket and Socket Classes (Contd.)


                // Read the input and print it to the screen
                System.out.println(dis.readUTF());
                // When done, just close the steam and connection
                dis.close();
                s1.close();




     Ver. 1.0                        Session 14                     Slide 22 of 26
Java Programming Language
Demonstration


               Lets see how to create a TCP based Java client, and use user-
               provided system properties to drive a Java program.




    Ver. 1.0                        Session 14                       Slide 23 of 26
Java Programming Language
Summary


               In this session, you learned that:
                – A stream is a flow of data from a source or to a sink.
                – A source stream are also called an input stream.
                – A sink stream terminates the flow of data, also called an output
                  stream.
                – InputStream, OutputStream, Reader, and Writer are
                  fundamental stream classes.
                – Three basic read methods of InputStream class are:
                      int read()
                      int read(byte[] buffer)
                      int read(byte[] buffer, int offset, int length)
                – Three basic write methods of OutputStream class are:
                    • void write(int c)
                    • void write(byte[] buffer)
                    • void write(byte[] buffer, int offset, int length)

    Ver. 1.0                        Session 14                           Slide 24 of 26
Java Programming Language
Summary (Contd.)


               – Three basic read methods of Reader class are:
                    int read()
                    int read(char[] cbuf)
                    int read(char[] cbuf, int offset, int length)
               – The basic write methods of Writer class are:
                    void   write(int c)
                    void   write(char[]    cbuf)
                    void   write(char[]    cbuf, int offset, int length)
                    void   write(String    string)
                    void   write(String    string, int offset, int length)
               – FileReader and FileWriter are file type character
                 streams.
               – FileInputStream and FileOutputStream are byte
                 streams classes.
               – ServerSocket and Socket are main classes to establish the
                 networking in Java Programs.


    Ver. 1.0                      Session 14                       Slide 25 of 26
Java Programming Language
Summary (Contd.)


               – To setup a network connection the address or name of remote
                 machine and a port number is required.
               – accept() method of ServerSocket class is used in server
                 program to receive client sockets to establish connection with
                 the server.
               – close() method of Socket class is required to close the
                 connection.




    Ver. 1.0                       Session 14                          Slide 26 of 26

Weitere ähnliche Inhalte

Was ist angesagt? (19)

Assembly
AssemblyAssembly
Assembly
 
Chapter 2 basic element of programming
Chapter 2 basic element of programming Chapter 2 basic element of programming
Chapter 2 basic element of programming
 
Pointers c imp
Pointers c impPointers c imp
Pointers c imp
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)
 
73d32 session1 c++
73d32 session1 c++73d32 session1 c++
73d32 session1 c++
 
Advanced C
Advanced C Advanced C
Advanced C
 
Pointers In C
Pointers In CPointers In C
Pointers In C
 
IPC with Qt
IPC with QtIPC with Qt
IPC with Qt
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
Python programming
Python programmingPython programming
Python programming
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
BIT204 1 Software Fundamentals
BIT204 1 Software FundamentalsBIT204 1 Software Fundamentals
BIT204 1 Software Fundamentals
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
 
Programming
ProgrammingProgramming
Programming
 
Notes on c++
Notes on c++Notes on c++
Notes on c++
 

Ähnlich wie Java session14

CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxSadhilAggarwal
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Nhap xuat trong java
Nhap xuat trong javaNhap xuat trong java
Nhap xuat trong javatuhn
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)Om Ganesh
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنةجامعة القدس المفتوحة
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptxssuser9d7049
 
Java session08
Java session08Java session08
Java session08Niit Care
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesSakkaravarthiS1
 
Java input output package
Java input output packageJava input output package
Java input output packageSujit Kumar
 
Using Input Output
Using Input OutputUsing Input Output
Using Input Outputraksharao
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptxssusera0d3d2
 
L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slidesunny khan
 

Ähnlich wie Java session14 (20)

javaiostream
javaiostreamjavaiostream
javaiostream
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptx
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Nhap xuat trong java
Nhap xuat trong javaNhap xuat trong java
Nhap xuat trong java
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
Md121 streams
Md121 streamsMd121 streams
Md121 streams
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنة
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
 
Java session08
Java session08Java session08
Java session08
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 
Java input output package
Java input output packageJava input output package
Java input output package
 
Using Input Output
Using Input OutputUsing Input Output
Using Input Output
 
Basic IO
Basic IOBasic IO
Basic IO
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptx
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
 

Mehr von Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Kürzlich hochgeladen

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Kürzlich hochgeladen (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Java session14

  • 1. Java Programming Language Objectives In this session, you will learn to: Describe Input/Output fundamentals Construct node and processing streams, and use them appropriately Distinguish readers and writers from streams, and select appropriately between them Develop code to set up the network connection Understand the TCP/IP Protocol Use ServerSocket and Socket classes for implementation of TCP/IP clients and servers Ver. 1.0 Session 14 Slide 1 of 26
  • 2. Java Programming Language I/O Fundamentals A stream can be thought of as a flow of data from a source or to a sink. A source stream initiates the flow of data, also called an input stream. A sink stream terminates the flow of data, also called an output stream. Sources and sinks are both node streams. Types of node streams are files, memory, and pipes between threads or processes. Ver. 1.0 Session 14 Slide 2 of 26
  • 3. Java Programming Language I/O Fundamentals (Contd.) The fundamental stream classes are: Stream Byte Streams Character Streams Source streams InputStream Reader Sink streams OutputStream Writer Ver. 1.0 Session 14 Slide 3 of 26
  • 4. Java Programming Language Data Within Streams Java technology supports two types of streams: character byte • Input and output of character data is handled by readers and writers. • Input and output of byte data is handled by input streams and output streams. • Normally, the term stream refers to a byte stream. • The terms reader and writer refer to character streams. Ver. 1.0 Session 14 Slide 4 of 26
  • 5. Java Programming Language The InputStream Methods The three basic read methods are: int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length) Other methods include: void close() int available() long skip(long n) boolean markSupported() void mark(int readlimit) void reset() Ver. 1.0 Session 14 Slide 5 of 26
  • 6. Java Programming Language The OutputStream Methods The three basic write methods are: void write(int c) void write(byte[] buffer) void write(byte[] buffer, int offset, int length) Other methods include: void close() void flush() Ver. 1.0 Session 14 Slide 6 of 26
  • 7. Java Programming Language The Reader Methods The three basic read methods are: int read() int read(char[] cbuf) int read(char[] cbuf, int offset, int length) Other methods include: void close() boolean ready() long skip(long n) boolean markSupported() void mark(int readAheadLimit) void reset() Ver. 1.0 Session 14 Slide 7 of 26
  • 8. Java Programming Language The Writer Methods The basic write methods are: void write(int c) void write(char[] cbuf) void write(char[] cbuf, int offset, int length) void write(String string) void write(String string, int offset, int length) Other methods include: void close() void flush() Ver. 1.0 Session 14 Slide 8 of 26
  • 9. Java Programming Language Node Streams Various types of Character and Byte Stream classes are: Type Character Streams Byte Streams File FileReader FileInputStream FileWriter FileOutputStream Memory: array CharArrayReader ByteArrayInputStream CharArrayWriter ByteArrayOutputStream Memory: string StringReader N/A StringWriter Pipe PipedReader PipedInputStream PipedWriter PipedOutputStream Ver. 1.0 Session 14 Slide 9 of 26
  • 10. Java Programming Language I/O Stream Chaining Input Stream Chain: Data Source Program FileInputStream BufferedInputStream DataInputStream • Output Stream Chain: Program Data Sink FileOutputStream BufferedOutputStream DataOutputStream Ver. 1.0 Session 14 Slide 10 of 26
  • 11. Java Programming Language Processing Streams • Processing Streams are Node Streams that use filters in between while transferring the data. • Various types of Processing Streams are: Type Character Streams Byte Streams Buffering BufferedReader BufferedInputStream BufferedWriter BufferedOutputStream Filtering FilterReader FilterInputStream FilterWriter FilterOutputStream Converting between InputStreamReader bytes and character OutputStreamWriter Performing object ObjectInputStream serialization ObjectOutputStream Performing data DataInputStream Conversion DataOutputStream Counting LineNumberReader LineNumberInputStream Ver. 1.0 Session 14 Slide 11 of 26
  • 12. Java Programming Language InputStream The InputStream Class Hierarchy: Ver. 1.0 Session 14 Slide 12 of 26
  • 13. Java Programming Language OutputStream The OutputStream Class Hierarchy: Ver. 1.0 Session 14 Slide 13 of 26
  • 14. Java Programming Language Reader Class The Reader Class Hierarchy: Ver. 1.0 Session 14 Slide 14 of 26
  • 15. Java Programming Language Writer Class The Writer Class Hierarchy: Ver. 1.0 Session 14 Slide 15 of 26
  • 16. Java Programming Language Networking Basics of Networking: Computers running on the Internet communicating to each other using the Transmission Control Protocol (TCP) / Internet Protocol (IP). Client.bar.com 18000 Server. foo.com 3000 Port no. Client.baz.com 18002 Ver. 1.0 Session 14 Slide 16 of 26
  • 17. Java Programming Language Networking (Contd.) Networking with Java Technology: Sockets: • Sockets hold two streams, an input stream and an output stream. • Each end of the socket has a pair of streams. Setting Up the Connection: Setup of a network connection is similar to a telephone. One end must dial the other end, which must be listening. Ver. 1.0 Session 14 Slide 17 of 26
  • 18. Java Programming Language Networking (Contd.) To address the connection, include the following: • The address or name of remote machine. • A port number to identify the purpose at the server. – Port numbers range from 0–65535 Ver. 1.0 Session 14 Slide 18 of 26
  • 19. Java Programming Language Networking (Contd.) Java Networking Model: Server SeverSocket(port#) Register with ServerSocket.accept() this service Client Wait for a Socket(host, port#) Socket() connection (Attempt to connect) OutputStream OutputStream InputStream InputStream Socket.close() Socket.close() Ver. 1.0 Session 14 Slide 19 of 26
  • 20. Java Programming Language ServerSocket and Socket Classes Code Snippet for Creating Minimal TCP/IP Server: ServerSocket s = null; s = new ServerSocket(5432); //Register your service on port 5432 while (true) // Run the listen/accept loop forever { Socket s1 = s.accept(); // Wait here and listen for a connection OutputStream s1out = s1.getOutputStream(); // Get output stream associated with the socket BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s1out)); bw.write(“Hello Net World!n”); // Send your string! Ver. 1.0 Session 14 Slide 20 of 26
  • 21. Java Programming Language ServerSocket and Socket Classes (Contd.) bw.close(); // Close the connection, but not the server socket s1.close(); } Code Snippet for Creating Minimal TCP/IP Client: // Open your connection to a server, at port 5432 // localhost used here Socket s1 = new Socket("127.0.0.1", 5432); // Get an input stream from the socket InputStream is = s1.getInputStream(); //Decorate it with a "data" input stream DataInputStream dis = new DataInputStream(is); Ver. 1.0 Session 14 Slide 21 of 26
  • 22. Java Programming Language ServerSocket and Socket Classes (Contd.) // Read the input and print it to the screen System.out.println(dis.readUTF()); // When done, just close the steam and connection dis.close(); s1.close(); Ver. 1.0 Session 14 Slide 22 of 26
  • 23. Java Programming Language Demonstration Lets see how to create a TCP based Java client, and use user- provided system properties to drive a Java program. Ver. 1.0 Session 14 Slide 23 of 26
  • 24. Java Programming Language Summary In this session, you learned that: – A stream is a flow of data from a source or to a sink. – A source stream are also called an input stream. – A sink stream terminates the flow of data, also called an output stream. – InputStream, OutputStream, Reader, and Writer are fundamental stream classes. – Three basic read methods of InputStream class are: int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length) – Three basic write methods of OutputStream class are: • void write(int c) • void write(byte[] buffer) • void write(byte[] buffer, int offset, int length) Ver. 1.0 Session 14 Slide 24 of 26
  • 25. Java Programming Language Summary (Contd.) – Three basic read methods of Reader class are: int read() int read(char[] cbuf) int read(char[] cbuf, int offset, int length) – The basic write methods of Writer class are: void write(int c) void write(char[] cbuf) void write(char[] cbuf, int offset, int length) void write(String string) void write(String string, int offset, int length) – FileReader and FileWriter are file type character streams. – FileInputStream and FileOutputStream are byte streams classes. – ServerSocket and Socket are main classes to establish the networking in Java Programs. Ver. 1.0 Session 14 Slide 25 of 26
  • 26. Java Programming Language Summary (Contd.) – To setup a network connection the address or name of remote machine and a port number is required. – accept() method of ServerSocket class is used in server program to receive client sockets to establish connection with the server. – close() method of Socket class is required to close the connection. Ver. 1.0 Session 14 Slide 26 of 26