Upload
ravi-kumar
View
227
Download
1
Embed Size (px)
Citation preview
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 1/47
Java Programming(J2EE LC)
Day 4
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 2/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd2
Session Plan
IO Streams in Java
– Brief overview of fundamental streams
– Some advanced streams
Serialization
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 3/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd3
Input and Output
The Java Input/Output system is designed to make it device independent
The classes for performing input and output operations are available in the
package java.io
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 4/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd4
Streams
Streams are channels of communication between programs andsource/destination of data
– A stream is either a source of bytes or a destination for bytes.
Provide a good abstraction between the source and destination
Abstract away the details of the communication path from I/O operationStreams hide the details of what happens to the data inside the actual I/Odevices.
Streams can read/write data from/to blocks of memory, files and networkconnections
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 5/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd5
Streams(Contd…)
Source Program
Reads
Program Destination
Writes
Stream
Stream
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 6/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd6
Java’s Input-Output System
The basic I/O system of Java can handle only two types of data
– Bytes
– Characters
Java Input/Output System
Character oriented System
Byte Oriented System
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 7/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd7
Byte I/O
Byte-Oriented System
The following are the two abstract classes provided for reading and writingbytes
– InputStream - Reading bytes
– OutputStream - Writing bytes
For performing I/O operations, we dependent on the sub classes of these two
classes
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 8/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd8
Methods in InputStream
int available()
void close()
void mark(int numberofBytes)
boolean markSupported()
int read()
int read(byte buffer[])
int read(byte buffer[], int offset, int numberofBytes)
void reset()
long skip(long numberofBytes)
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 9/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd9
Methods in OutputStream
void close()
void flush()
void write(int b)
void write(byte buffer[])
void write(byte buffer[], int offset, int numberofBytes)
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 10/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd10
Input Stream hierarchy
InputStream
FilterInputStream
ByteArrayInputStream
FileInputStream
ObjectInputStreamBufferedInputstream
DataInputStream
PushbackInputStream
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 11/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd11
Output Stream hierarchy
OutputStream
FilterOutputStream
ByteArrayOutputStream
FileOutputStream
ObjectOutputStreamBufferedOutputstream
DataOutputStream
PrintStream
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 12/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd12
Character I/O
Character-Oriented System
Since byte-oriented streams are inconvenient for Unicode-compliant
character- based I/O , Java has the following abstract classes for performingcharacter I/O
– Reader class
– Writer class
Support internationalization of Java I/O
Subclasses of Reader and Writer are used for handling Unicode characters
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 13/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd13
Methods in Reader
void close()
void mark( int )
int read()
int read(char[] c, int len)
int read(char [] c, int off , int len)
long skip(long num)
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 14/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd14
Methods in Writer
abstract void close()
abstract void flush()
void write( int c)
void write(char[] c, int off, int len)
void write(char[] c, int len)
void write(String str)
void write(String str, int off, int len)
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 15/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd15
Reader Hierarchy
Reader
CharArrayReader
BufferedReader InputStreamReader
FilteredReader FileReader
PushbackReader
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 16/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd16
Writer Hierarchy
Writer
CharArrayWriter
BufferedWriter OutputStreamWriter
PrintWriter FileWriter
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 17/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd17
OutputStream - Example
Write a program to write some bytes of data into a file
To write bytes into any output device, we have to depend on a sub class of
OuputStream
FileOutputStream is a subclass of OutputStream that helps to write bytes into a
file
The following code fragment stores byte values to the file “Data” using a
FileOutputStream object:
FileOutputStream fileOutputStream = new FileOutputStream(“Data”);
byte data1 = 65, data2 = 66, data3 = 67;
fileOutputStream.write(data1);
fileOutputStream.write(data2);
fileOutputStream.write(data3);
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 18/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd18
InputStream - Example
Write a program to read all the bytes from the the file “Data”
To read bytes values from any device, we have to depend on a sub class of
InputStream
FileInputStream is a subclass of InputStream that helps to read bytes from a
file
The following code fragment reads bytes from the file “Data” using a
FileInputStream object:
FileInputStream fileInputStream = new FileInputStream(“Data”);
int value = fileInputStream.read();
while(value != -1)
{
System.out.println((byte)value);
=
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 19/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd19
InputStream – The read() method (1 of 2)
The read() method reads a byte from the input device and returns an integer
It returns an integer value -1 when the read fails
– The read() method of FileInputStream returns -1 when it reaches end of file
condition
In case of a successful read, the returned integer can be typecast to a byte to
get the data read from the device
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 20/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd20
InputStream – The read() method (2 of 2)
Why is the byte embedded in an int?
Storing the byte into the low end of an integer, makes it possible for the
program to distinguish between -1 integer representing end of data conditionand -1 byte, a data stored in the device
On reading a data of -1 byte successfully from a device, the following integer is
returned – 00000000 00000000 00000000 11111111
– The integer value of this is 255
– This can be typecast to 11111111 which is the real data, -1 byte
When a read fails, the following integer is returned
– 11111111 11111111 11111111 11111111
– The integer value is -1 indicating end of data
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 21/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd21
Input/OutputStream – Example (1 of 2)
ByteArrayInputStream and ByteArrayOutputStream
Help the programmer to use an array of bytes just like an I/O
device
Create these streams on an existing byte array and use the
read() and write() methods to read from or write data to the array
in memory.
ByteArrayInputStream
- The constuctors have the forms :
ByteArrayInputStream(byte arr[])
ByteArrayInputStream(byte arr[],int start, int numBytes)
ByteArrayOutputStream
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 22/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd22
Input/OutputStream – Example (2 of 2)
ByteArrayInputStreamDemo
ByteArrayOutputStreamDemo
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 23/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd23
Device Independent I/O (1 of 2)
The way in which I/O is performed is not changing with the device
The following method will write three bytes into any output devicepublic void writeBytes(OutputStream outputStream){
try{
outputStream.write(65);
outputStream.write(66);
outputStream.write(67);}
catch(IOException exception){
System.out.println(exception);
}
}
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 24/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd24
Device Independent I/O (2 of 2)
ByteWriter byteWriter = new ByteWriter();
//Write to the monitor
byteWriter.writeBytes(System.out);
//Write to an array
byteWriter.writeBytes(byteArrayOutputStream);
//Write to a file
byteWriter.writeBytes(fileOutputStream);
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 25/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd25
Filter Classes
The Java I/O System uses filter classes to build specialized I/O streams that
provide particular capabilities as needed.
Typically an instance of a lower level class is created and then it is wrappedinside more specialized stream instances by passing it to the wrapper via a
constructor argument.
Typical extensions are buffering, character translation, and raw datatranslation.
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 26/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd26
Non-Buffered IO
Reading and writing a byte at a time can be expensive in terms of processing time
H a r d
D i s k
Program
FileStream
Access H/D
Gets/writes firstbyte
Access H/D
Gets/writessecond byte
Access H/D
Gets/writes third byte
gets/writesdata
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 27/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd27
Buffered IO
Buffering helps java to store an entire block of values into a buffer
It never has to go back to the source and ask for more, unless it runs out
H a r d D i s k
Program FileStream
Access H/D
Gets/writes a set ofbytes a s per the buffer
size per access
gets/writesdata
Buffer Transfe r ofgroup
o f bytes
T r a n s f e r o f o n e
b y t e a t a t i m e
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 28/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd28
Example-Buffering (1 of 2)
The following code snippet writes bytes to a file using buffering mechanism
FileOutputStream fileOutputStream=new FileOutputStream(“Data”);
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream
(fileOutputStream);
byte data1 = 65, data2 = 66, data3 = 67;
bufferedOutputStream.write(data1);
bufferedOutputStream.write(data2);
bufferedOutputStream.write(data3);
bufferedOut utStream.close ;
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 29/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd29
Example-Buffering (2 of 2)
The device (Hard Disk) is wrapped in a FileInputStream and the FileInputStream is in
turn wrapped in BufferedInputStream
B u f fe r e d O u t p u t S t r e a m
F ile O u t p u tS t r e a m
H / D
F i l t e r C la s s e s
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 30/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd30
DataInputStream and DataOutputStream (1 of 2)
InputStream and OutputStream deal with bytes only
DataInputStream and DataOutputStream are filter classes that wrap around
InputStream and OutputStream objects so that primitive types can be read and
written
DataInputStream has the methods
– readInt – readDouble
– readBoolean
– readXXX
DataOutputStream has the methods
– writeInt
– writeDouble
– writeBoolean
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 31/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd31
DataInputStream and DataOutputStream (2 of 2)
DataOutputStreamDemo
DataInputStreamDemo
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 32/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd32
PushbackInputStream
Pushback is used on an input stream for reading a byte and then pushing it
back to the stream as if it is unread
The filter class PushbackInputStream provides the following methods for
pushing back
– void unread(int bytevalue )
– void unread(byte buffer[] )
– void unread(byte buffer[], int offset, int numBytes )
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 33/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd33
PushbackInputStream Demo
PushbackInputStreamDemo
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 34/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd34
PrintStream
This filter class provides functionality to print all data types.
It provides two methods, print() and println(), that are overloaded to print any
primitive data type or object.
Objects are printed by first converting them to strings using their toString()
method inherited from the Object class.
PrintStream class has the following constructor – PrintStream(OutputStream outputStream )
System.out is a PrintStream object
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 35/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd35
Character I/O
Reader and Writer classes are to character I/O what InputStream and
OutputStream classes are to byte I/O
Classes corresponding to byte stream classes are available for character I/O
– FileOutputStream – FileWriter
– FileInputStream - FileReader
– BufferedOutputStream – BufferedWriter
– BufferedInputStream – BufferedReader
– ByteArrayOutputStream - CharArrayWriter
– ByteArrayInputStream – CharArrayReader
– etc…
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 36/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd36
“Bridge” Classes
Two classes act as bridge between byte streams and characters
– InputStreamReader
– OutputStreamWriter
InputStreamReader wraps around an InputStream object and helps to read characters
from an InputStream
OutputStreamWriter wraps around an OutputStream object and helps to write characters
into an OutputStream
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 37/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd37
InputStreamReaderDemo
InputStreamReaderDemo
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 38/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd38
Review
Java’s I/O System is device independent
InputStream, OutputStream, Reader and Write classes form the base of all I/O
classes
Filter classes wrap around the basic objects to give additional functionalities
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 39/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd39
What is Serialization?
Persistence:
– The capability of an object to exist beyond the execution of the program which
created it.
– In other words, saving the state of an object in some permanent storage device,
such as file
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 40/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd40
Serialization Mechanism
Serializable objects can be converted into stream of bytes
This stream of bytes can be written into a file
These bytes can be read back to re-create the object
Only those objects that implements java.io.Serializable interface can be
serialized
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 41/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd41
ObjectOutputStream and ObjectInputStream
Filter classes that help to write and read Serializable objects
ObjectOutputStream wraps around any OutputStream and helps to write a
Serializable object into the OutputStream
• ObjectOutputStream(OutputStream outputStream)
The method writeObject(Object object) will write the object to the underlying
OutputStream object
ObjectInputStream wraps around any InputStream and helps to read a stream
of bytes as an Object from the InputStream
• ObjectInputStream(InputStream inputStream)
The method readObject() will read a stream of bytes, convert them into an
Object and return it
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 42/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd42
Serialization Demo
SerializationDemo
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 43/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd43
De-Serialization Demo
DeSerializationDemo
E t li bl I t f
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 44/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd44
Externalizable Interface
Class which implements this interface
– can control the serialization of its instances
– methods for reading (readExternal()) and writing (writeExternal()) from the
stream must be defined in this case
ExternalizationDemo
S it i i i li ti
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 45/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd45
Security: an issue in serialization
Serialized objects can be sent over network
Can be accidentally or deliberately modified
Also sensitive data can be read
Solution
Encrypt the object during serialization using Security API
Ensure that sensitive objects do not implement Serialializable or Externalizable
S mmar
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 46/47
ER/CORP/CRS/LA10/003Version 1.00
Copyright © 2005, InfosysTechnologies Ltd
46
Summary
IO Streams in Java
– overview of fundamental streams
– Some advanced streams
Serialization
8/19/2019 Ppt La10 Java Programming 04
http://slidepdf.com/reader/full/ppt-la10-java-programming-04 47/47
ER/CORP/CRS/LA10/003
Version 1.00Copyright © 2005, Infosys
Technologies Ltd47
Thank You!