CS340 Advances in Software Development

squawkpsychoticSoftware and s/w Development

Dec 2, 2013 (3 years and 4 months ago)

59 views

CS340
Advances in Software
Development
Object Oriented Programming in Java
Lecture 12
Hüseyin Akcan
huseyin.akcan@ieu.edu.tr


Java I/O Streams

I/O Stream represents an input source or an
output destination

Disk files

Devices

Programs

Memory arrays

Database

Network


Streams
Input stream
Output stream
Streams can
handle all
kinds of data,
from the most
primitive
(byte etc) to
the most
complex
(objects etc.)


Character Streams


Grey readers connect to the actual source


White readers do some processing


Byte Streams


Using Streams

Variety of stream input/output classes are
defined.

Use the one appropriate for you and your data

If doing low level I/O is not something you
need, use a derived class of
InputStream/OutputStream


Character Streams

Java character stream I/O automatically
translates to and from local character set

It has built in support for internationalization

Use character streams instead of byte streams
when working with characters to benefit from
this feature


Character streams (2)

Character streams use byte streams for low
level I/O

Two general purpose byte-to-character bridge
streams available

InputStreamReader

OutputStreamWriter

Use these to create character streams if none
available


Buffered Streams

Previously mentioned streams perform
unbuffered I/O

Means each read and write is reflected to the
underlying OS immediately

This makes programs much less efficient

Instead Java uses BufferedStreams, which
read/write to a buffer and the changes are
reflected when the buffer is empty/full.


Buffered Streams (2)

Usually you create a buffered stream by using
an unbuffered stream
inputStream = new BufferedReader(newFileReader("inputfile.txt"));
outputStream = new BufferedWriter(new FileWriter("outputFile.txt"));

Available buffered stream classes

BufferedInputStream

BufferedOutputStream

BufferedReader

BufferedWriter
See InputTest.java for more...


Data Streams

How can we write and read built in types?

Java has data streams for this!!!

Data streams support binary I/O of primitive
data type values (boolean, char, byte, short, int,
long, float, and double) as well as String values


Data Streams example
Read and write:
Data Type
Description
Double
Item price
Int
Item count
String
Item description
How can you write these in a single file?
See DataStreamsDemo for more...


Object streams

We can send basic types to streams, can we
also do the same with complex objects?

Yes, using the Java object serialization


Object streams (2)

Object has to implement
serializable
interface

A markup interface with no methods

If all fields of the class is either

Primitive types

Serializable objects

Marked
transient

Transient fields are not serialized, or restored during
deserialization


What to serialize?

You cannot serialize some of the Java objects

Thread

OutputStream

Socket

Anything which has an active connection, such as
Database connections

Other than that, most of the Java objects are
serializable


What are written into the stream?

Class of the object

Signature of the class

Values of the fields

Be carefull here, if you have references to other
objects, they are written too!!!

Might be a huge stream if your objects have a lot of
references to other huge objects

All reference objects should also be serializable!
See SerializationDemo.java for more...


How is the object written?
When you serialize an object, all the fields that are necessary
to reconstruct that object are also written into the stream


Recursive objects?

Each object is assigned a serial number

ObjectOutputStream checks the serial number

Will not write the previously written objects

Each object is written once, and this is where
serialization
comes from
Object 1
Object 2


Custom serialization

You can also customize how an object is
serialized

Add private readObject and writeObject
methods to your class

private void writeObject(ObjectOutputStream out)
throws IOException

private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException

Good idea to call out.defaultWriteObject() and
in.defaultReadObject() from these methods


Use case for custom serialization

Assume you have an hashMap in your class, that is
inconcenient to serialize

Might have extra unnecessary fields

Serializing hashMaps are not a good idea, the
implementation can change with JDK version

Each JDK system has a different random generator, so the
order of elements might not be correct

Mark the hashmap transient, and within writeObject
serialize each element separately

When reading create an empty hashmap, and add each
object separately into hashmap
See CustomSerialization.java for more...