Java I/O frameworks

concretecakeΠολεοδομικά Έργα

29 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

70 εμφανίσεις

Java I/O frameworks

Input and output

Java I/O overview


3 different frameworks:


Streams (Java 1.0)


Readers and writers (Java 1.2)


NIO


New I/O (java 1.4)


Some bridging between frameworks for
backward compatibility


Main concept: decorators (wrappers)


Followed through most of the frameworks

Decorators


Basic idea:


All classes implement the same basic functionality


Concrete suppliers actually do something


Decorators take instance of that interface as
constructor parameter, and wrap it with extra
functionality (“decorate”)

Source

Each decorator

takes the previous

one, use it, and

extends its

functionality

Decorators class diagram

BaseFunc

func()

Source

func()

Decorator

func()

Decorated object

Actually do something

Use the decorated object

to do some more

I/O decorators


Basic function is reading
or writing byte arrays


Sources are actual I/O
devices or sources, like:


Files


Sockets


Byte arrays


Pipes


Strings



Decorating functions are:


Reading/writing primitive
(int, char, float…)


Reading/writing objects


Compressing /
decompressing


Buffering


Reading lines of text


Printing


Writing lines


Filtering

Java steams


The original Java I/O framework


Located at the
java.io

package


Basic operations are reading or writing int’s and
byte arrays


InputStream

and
OutputStream
abstract classes


Sources/targets are mainly files, sockets, etc.


Decorators include object handling, filtering,
compression, (some) buffering, etc.


Deprecated



replaced with readers/writers


Java readers/writers


Java’s replacement for old streams


But also at the same package


There are bridging classes:
InputStreamReader

and
OutputStreamWriter


Many take streams as decorated objects


Similar basic operations


Reader

and
Writer


Similar sources/targets


files, sockets…


Similar decorators: buffering, filtering, strings…


Slightly added and improved functionality


Java New I/O
-

NIO


New, innovative I/O framework


Located at its own package:
java.nio


Caused changes throughout the entire library


Based on buffers and channels


Like streams, but read in big chunks


Used mainly for large
-
scale operations, like


File mapping to memory


Text charset decoding regular
-
expressions parsing


Channels gathering and scattering…


Neo