Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
432 views

Object Streams & Serialization

Serialization is the process of converting an object into a byte stream to persist it so that it can be recreated when needed. Object streams allow serialization and deserialization of objects through object input and output streams. Byte streams like FileInputStream and FileOutputStream are used for input and output of bytes while character streams like FileReader and FileWriter are used for input and output of characters. Streams simplify code by allowing pipelining of operations and making parallel code easier to write.

Uploaded by

sai bhaskar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
432 views

Object Streams & Serialization

Serialization is the process of converting an object into a byte stream to persist it so that it can be recreated when needed. Object streams allow serialization and deserialization of objects through object input and output streams. Byte streams like FileInputStream and FileOutputStream are used for input and output of bytes while character streams like FileReader and FileWriter are used for input and output of characters. Streams simplify code by allowing pipelining of operations and making parallel code easier to write.

Uploaded by

sai bhaskar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

OBJECT STREAMS

AND
SERIALIZATION
Team 2
Serialization
WHAT IS SERIALIZATION ?

• Serialization is a mechanism of converting the state of an object into a byte


stream.

• This mechanism is used to persist the object.

• Its main purpose is to recreate it when needed.


Uses of serialization

• We need serialization because the hard disk or network infrastructure are


hardware components and we cannot send java objects because it understands
just byte codes not java objects.
How to serialize?

• A java object is serializable if its class or any of its super classes implements
java.io.serializable interface or its sub interface, java.io.externalizable.
• Serializable is a marker interface that adds serializable behavior to the class
implementing it.
• Marker interface is used as a tag to inform a message to the Java compiler
so that it can add special behavior to the class implementing it. Java marker
interface has no members in it.
Difference between serializable and externalizable

• Serializable is a marker interface i.e. does not contain any method but
Externalizable interface contains two methods writeExternal() and
readExternal().
• When any class in Java implement java.io.Externalizable than its your
responsibility to implement Serialization process i.e. preserving all important
information.
• Performance.
Example of java serialisation
Advantages
• Easy to use and customized.
• The byte stream created is platform independent. So, the object serialized on
one platform can be deserialized on a different platform.
• Serialized stream can be encrypted, authenticated and compressed supporting
needs of secure java computing.
• It can be used for exchanging objects between java and c++ libraries using
third party libraries.
Disadvantages

• It should ideally not used with large sized objects, as it offers overhead.
• Doesn’t offer fine grained control over object access.
• Doesn’t offer any transaction control mechanism.
Object streams
OBJECT STREAMS

• A stream is a sequence of objects that supports various methods which can be


pipelined to produce desired result.
• Just as data streams support i/o of primitive data types, object streams support
i/o of objects.
• Most but not all standard classes support serialization of their objects.
Object stream classes

• Object stream classes are object input stream and object output stream.

• These classes implement object input and object output which are sub
interfaces of data input and data output.
Byte streams

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

• FileInputStream is used for reading data from files.


• Objects can be created using keyword new and several types of
constructors available.
• Following constructor takes file name as string to create an input object
stream to read the file.

• Following constructor takes a file object to create an input stream object to


read the file. First we create a file object using file() method as follows:
Methods with description
FileOutputStream
• FileOutputStream is used to create a file and write data into it.
• The stream would create a file , if it doesn’t already exist, before opening it
for output.
• Here are two constructors :
• Following constructor takes file name as string to create an output stream
object to write the file.

• Following constructor takes a file object to create an output stream object


to write the file. First we create a file object using file() method as follows:
Methods with description
Character Streams
• Java byte streams are used to perform input and output of 8- bit bytes, where
as character streams are used to perform input and output for 16- bit Unicode.
• Though there are many classes related to character streams but most
frequently used classes are,
FileReader
FileWriter.

• Though internally FileReader uses FileInputStream and FileWriter uses


FlieOutputStream but here major difference is that FileReader reads two
bytes at a time and FileWriter writes two bytes at a time.
FileWriter

• This class inherits from the OutputStream class.


• The constructors of this class assume that the default character encoding and
the default byte-buffer size are acceptable. To specify these values yourself,
construct an OutputStreamWriter on a FileOutputStream.
• FileWriter is meant for writing streams of characters. For writing streams of
raw bytes, consider using a FileOutputStream.
• FileWriter creates the output file , if it is not present already.
FileReader

• FileReader is useful to read data in the form of characters from a ‘text’ file.
• This class inherit from the InputStreamReader Class.
• The constructors of this class assume that the default character encoding and
the default byte-buffer size are appropriate. To specify these values yourself,
construct an InputStreamReader on a FileInputStream.
• FileReader is meant for reading streams of characters. For reading streams of
raw bytes, consider using a FileInputStream.
Uses of streams

• Main reason for the use of streams is to make your code simpler and easy to
read.
• The goal of streams in Java is to simplify the complexity of writing parallel
code.
• It's inspired by functional programming.
• The serial stream is just to make the code cleaner.
THANK YOU

You might also like