Prezi

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in the manual

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Java 7,8 NIO.2 API features, a walkthrough

Most of applications nowadays depend intensively on asynchronous or non-blocking IO and file manipulation operations and some other requirements that the standard IO won’t be able to fulfill and that’s the case for most applications today.
by Mohamed Taman on 15 November 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Java 7,8 NIO.2 API features, a walkthrough

R.A.F
The Power of Java 7,8
NIO.2

Get educated with the new file I/O mechanism introduced
in the JDK 7,8 release.

Who am I?
By: Mohamed Taman
JCP EC, Systems Architect & Design supervisor
Essential Topics
Advanced Topics
Java is the my best choices since 2001, I feel coding it like

Coding the Architecture
”.
1- Working with the
new
Path
Class.
2- Changing File Attributes with
Metadata
.
3- What is
Symbolic

and
Hard
Links?
4- New API for
Files
and
Directories.
6- Traversal process using
FileVisitor
Interface.
7- Monitoring via
Watch Service

API.
5- New powerful
Random Access
Files.
8-
Networking
with
the
Sockets
APIs.
9- The
Asynchronous
Channel
API.
Agenda
But this part is all about how NIO.2
Added new Framework APIs, for powerful operations.....
This part is all about how NIO.2
streamlines & extends the I/O development power.....

We will talk about 8 topics
Path class
A path resides in a file system, which “
stores and organizes files on some form of media, generally one or more hard drives, in such a way that they can be easily retrieved.

And
Works with
This class is a
milestone
of NIO.2, and every application that involves I/O operations will exploit the powerful facilities of this class.
The file system can be accessed through the
java.nio.file.FileSystems
final class, which is used to get an instance of the
java.nio.file.FileSystem
we want to work on.
The
Path
class
supports two types of operations
Almost any operation that involves manipulating paths without accessing the file system; these are logical manipulations done in memory.
Syntactic operations
Operations over files
referenced by paths.
Remember the days when you had to do the following?

import java.io.File;
File file = new File("index.html");
Do you remember this
!!
Well, those days are gone, because with Java 7 you can do this:

import java.nio.file.Path;
import java.nio.file.Paths;
Path path = Paths.get("index.html");
Moreover, starting with Java 7, both classes are available, which means programmers can mix their powers to obtain the best of I/O APIs.
Java 7 provides a simple API for conversion between them.
Metadata
File
Attributes

Symbolic &
Hard Links
If you have ?
about a file or a directory,
such as
Whether it is hidden?
Whether it is a directory?
What its size is, and who owns it?
You can get answers to those questions (and many others) from the
metadata,
which is data about other data.
NIO.2
associates the notion of
metadata
with attributes and provides access to them through the
java.nio.file.attribute
package.
Since different file systems have different notions about which attributes should be tracked,
NIO.2
groups the attributes into
views,
each of which maps to a particular file system implementation.
Depending
On the view, other methods are available
for additional tasks.
views
Linux and UNIX users (
especially administrators
) should be familiar with the concept of links.
There are two
types of links
Symbolic
Hard
Windows Users Know this as
"shortcuts".
Following are the main differences/similarities between the two types of links:
Hard links can be created only for files, not for directories. Symbolic links can link to a file or a directory.

Hard links cannot exist across file systems. Symbolic links can exist across file systems.

The target of a hard link must exist. The target of a symbolic link may not exist.

Removing the original file that your hard link points to does not remove the hard link itself, and the hard link still provides the content of the underlying file.
Removing the original file that your symbolic link points to does not remove the attached symbolic link, but without the original file, the symbolic link is useless.

If you remove the hard link or the symbolic link itself, the original file stays intact.

A hard link is the same entity as the original file. All attributes are identical. A symbolic link is not so restrictive.

A hard link looks, and behaves, like a regular file, so hard links can be hard to find.

A symbolic link’s target may not even exist, therefore it is much flexible.
Links

commonly reach a file through several names, instead of navigating through a series of directories and subdirectories from the root.
Each method of the
Path
class knows how to detect a
link
and will behave in the default manner if no configuration of behavior is specified.
Files

&
Directories.
N
ow that you know how to point to a file or directory using the
Path
class in first point, you are ready to know how to accomplish the most common tasks for managing files and directories, such as
create
,
read
,
write
,
move
,
delete
, and so on.
NIO.2
comes with a set of brand new methods to accomplish these tasks, most of which are found in the
java.nio.file.Files
class.
Ideas
The
java.nio.file.Files
provides some methods dedicated to checking if a Path is
readable
,
writable
,
executable
,
regular
, or
hidden
.
...These checks enable you to determine what kind of file or directory you are dealing with before you apply operations such as write or read.
what's new?
java.nio.file.Files
provides file operations,
such as
reading
,
writing
,
creating
, and
opening
files.
Also you can work with the well-known
delete,

copy,
and
move
operations.
You will find at work methods for
buffered
and
unbuffered
streams, leaving coverage of the methods for channels for the next points 7, 8 and 9, in which you will see the real power of NIO.2.
Definitions !
Directory operations, allowing you to
list
,
create
, and
read
directories. You will find how to list the file system roots, create directories with methods such as
createDirectory()
and
createTempDirectory()
, write directory filters, and list a directory’s content using the
newDirectoryStream()
method.
A
stream
represents an input source or an output destination (it can be anything from disk files to memory arrays).

Streams support different kinds of data, as strings, bytes, primitive data types, localized characters, and objects.
In an
unbuffered

stream, each read or write request is handled directly by the underlying operating system, while in a buffered stream, the data is read from a memory area known as a buffer; and the native input API is called only when the buffer is empty.
Similarly,
buffered
output streams write data to a buffer, and the native output API is called only when the buffer is full. When a buffer is written out without waiting for it to fill, we say that the buffer is flushed.
Example
Basic.
DOS.
POSIX (
P
ortable
O
perating
S
ystem
I
nterface for Uni
x
).
ACL
(
A
ccess
C
ontrol
L
ist).
File Owner.
User defined attributes.
Random
Access
Files
The
advantages
of using
non-sequential
(
random
) access to a file’s contents Is it permits random access to their contents, and are known as random access files (
RAF
s).
With a RAF, you can open the file, seek a particular location, and read from or write to that file.
After you open a
RAF,
you can read from or write to it in a random manner just by using a record number, or you can add to the beginning or end of the file since you know how many records are in the file.
Is RAF powerful....
Files that can be explored sequentially are known as
sequential
files.
Sequential
files are used more often because they are easy to create, but
RAFs
are more flexible and their data can be located faster.
three
two
one
Benefits
A
RAF
allows you to read a single character, read a chunk of bytes or a line, replace a portion of the file, append lines, delete lines, and so forth, and allows you to perform all of these actions in a random manner.
Technically
Java 7 (
NIO.2
) introduces a brand-new interface for working with
RAFs.
Its name is
SeekableByteChannel
and it is available in the
java.nio.channels
package.

It extends the older
ByteChannel
interface and represents a byte channel that maintains a current position and allows that position to be modified.
Moreover,
Java 7
improves the well-known
FileChannel
class by implementing this interface and providing RAF and
FileChannel
power in a single shot. With a simple cast we can transform a
SeekableByteChannel
into a
FileChannel.
More...
FileChannel
provides facilities such as mapping a region of the file directly into memory for faster access, locking a region of the file, and reading and writing bytes from an absolute location without affecting the channel’s current position.
The new
SeekableByteChannel
interface is based on the old interfaces
ReadableByteChannel
(represents a channel that reads bytes; only one thread can read at a time) and
WritableByteChannel
(represents a channel that writes bytes; only one thread can write at a time) that have been available in
NIO
since
JDK 1.4
. These two interfaces are super interfaces for
SeekableByteChannel.
Thanks to this relationship between them.
Recursive Operations: Walks
NIO.2
takes advantage of recursion programming technique.
Based on this mechanism,
NIO.2
encapsulates the traversal process of a file tree in an interface, named
FileVisitor
, in the
java.nio.file
package.
This is a very common approach when performing tasks such as
deleting
,
copying,
or
moving
a file tree.
In Graphics
The

FileVisitor

Interface
As previously mentioned, the
FileVisitor
interface provides the support for recursively traversing a file tree.
The methods of this interface represent key points in the traversal process, enabling you to take control when a file is visited, before a directory is accessed, after a directory is accessed, and when a failure occurs.
In
other words
, this interface has hooks for before, during, and after a file is visited, as well as for when failure occurs.
Once you have control (at any of these key points), you can choose how to process the visited file and decide what should happen to it next by indicating a visit result through the
FileVisitResult
enum.
File notifications monitoring
Watch service API
The Watch Service API was introduced in Java 7 (
NIO.2
) as a thread-safe service that is capable of watching objects for changes and events.
The most common use is to monitor a directory for changes to its content through actions such as
create,

delete,
and
modify.
You’ve probably seen the effect of such a service many times.
The Watch Service API is a low-level API that can be used as is or can be customized.
By default, this API uses the underlying file system functionalities to watch the file system for changes.
When one or more of the
registered notification
events are detected by the watch service, the watch service passes the notification events to the process that is registered to handles them through a separate thread or pool of threads.
Starting with
NIO.2
You no longer need to poll the file system for changes or use other
in-house
solutions to monitor the file system changes.
You can even write a high-level API on top of it.
Java introduced support for sockets in
JDK 1.0
, but things have of course changed over time from version to version.
Networking
with the
Socket APIs
Jumping to Java 7,
NIO.2
has improved this support by updating existing classes with new methods and adding new interfaces/classes for writing
TCP/UDP
-based applications.
First of all,
NIO.2
introduces an interface named
NetworkChannel
that provides methods commons to all network channel classes — any channel that implements this interface is a channel to a network socket.
What is
TCP

&

UDP
.....
If
TCP
is like a telephone,
UDP
is like a letter. The sender writes the receiver address (remote IP and port) and sender address (local IP and port) on the envelope
(UDP
packet), puts the letter (data to be sent) into the envelope, and sends the letter.
He doesn’t know if the letter will arrive to the receiver or not. Moreover, a more recent letter can arrive faster than and old one, and a letter might never arrive at all—the letters are not aware of one another.
But Keep
in mind
that
TCP
is for high-reliability data transmissions while
UDP
is for low-overhead transmissions. Typically, use
UDP
in applications in which reliability is not critical but speed is.
The main classes dedicated to synchronous socket channels,
ServerSocketChannel,

SocketChannel,
and
DatagramChannel,
implement this interface, which comes with methods for binding to and returning local addresses, and methods for setting and getting socket options through the new
SocketOption<T>
interface and
StandardSocketOptions
class.
This interface’s methods and the ones added directly into classes (for checking connection state, getting remote addresses, and shutdown) will prevent you from having to call the
socket()
method.
NIO.2
also introduces the
MulticastChannel
interface as a sub interface of
NetworkChannel.
As its name suggests, the
MulticastChannel
interface maps a network channel that supports IP multicasting.
Keep in mind that
MulticastChannel
is implemented only by the datagram channel (the
DatagramChannel
class).
When joining a multicast group you get a membership key, which is a token that represents the membership of a multicast group.
Many real-life cases can be programmed based on multicasting, such as
online conferencing, news distribution, advertising, e-mail groups,
and
data-sharing management
.
Blocking vs.
Non-Blocking Mechanisms
When you decide to write a Java TCP server/client application, you must consider whether you need to write a blocking or non-blocking application. This decision is important because the implementations are different and the complexity may also be critical.
The main characteristic of a blocking mechanism presumes that a given thread cannot do anything more until the I/O is fully received, which may take a while in some cases—the application’s flow is blocked because the methods do not return right away.
Non-blocking mechanisms, on the other hand, immediately queue an I/O request and return the control to application flow (methods return right away). The request will be processed later by the kernel.
From a Java developer perspective, you also must take into account the degree of complexity involved by these mechanisms. Non-blocking mechanisms are much more complex to implement than blocking mechanisms, but they allow you more performance and scalability.
Keep in mind...
that even non-blocking looks like a multithreading application, this is a single-thread application based on the multiplexing technique.
In other words
, with a non-blocking mechanism, a function won’t wait while on the stack, and with an asynchronous mechanism, work may continue on behalf of the function call after that call has left the stack.
The
Asynchronous
Channel API
The Jewel of
the Crown
We’ve finally reached the most powerful feature introduced in
NIO.2
, the
asynchronous channel API
.
the asynchronous I/O
(AIO)
Java 7 journey starts in the
java.nio.channels.AsynchronousChannel
interface, which extends a channel with asynchronous I/O operations support.
This interface is implemented by three classes:
AsynchronousFileChannel.
AsynchronousSocketChannel.
AsynchronousServerSocketChannel.
Tip
In summary
This is a huge mountain
In addition, there is an asynchronous channel named
AsynchronousByteChannel
that can read and write bytes and stands up as a sub interface of
AsynchronousChannel
(this sub interface is implemented by the
AsynchronousSocketChannel
class).
Moreover, the new API introduces a class named
AsynchronousChannelGroup,
which presents the concept of an asynchronous channel group, in which each asynchronous channel belongs to a channel group (
the default one or a specified one
) that shares a pool of Java threads.
These threads receive instructions to perform I/O events and they dispatch the results to the completion handlers. All the effort is for the purpose of handling the completion of initiated asynchronous I/O operations.
Thread Pool !!!
Synchronous I/O
vs.

Asynchronous I/O.
Overview
In a
synchronous
I/O operation
A thread enters into action and waits until the I/O request is completed (the program is
“stuck”
waiting for the process to end, with no way out).
"While
....
"
In a
asynchronous
I/O operation:
When the same action occurs in an asynchronous environment, a thread performs the I/O operation with more kernel help.

Actually, it immediately passes the request to the kernel and continues on to process another job.

The kernel signals to the thread when the operation has completed, and the thread
“respects”
the signal by interrupting its current job and processing the data from the I/O operation as necessary.
The difference between synchronous and asynchronous execution may seem a bit confusing at first, so let’s clear it up.
In the Java spirit of platform independence, asynchronous I/O can be tied to multiple threads—basically, allowing something to be processed on a separate thread.
More and More.....
Asynchronous
I/O and
synchronous
I/O serve different purposes. You can use synchronous I/O if you simply want to make a request and receive a response.
Asynchronous I/O is a different programming model, because you don’t necessarily wait for a response, but rather submit your work for execution and then come back for a response either almost immediately or sometime later.
When
talking about
asynchronous

I/O in Java, we are talking about the asynchronous channels.
An asynchronous channel is a connection that supports multiple I/O operations in parallel through separate threads (connecting, reading, and writing, for example)
and provides mechanisms for controlling the operations after they’ve been initiated.
All asynchronous I/O
operations have one of two forms
Pending

result
Complete

result
I/O processing that is expected to take a large amount of time can be optimized by using
asynchronous
I/O. For relatively fast I/O operations,
synchronous
I/O would be better because the overhead of processing kernel I/O requests and kernel signals may make asynchronous I/O less beneficial.
Synchronous
I/O limits performance and scalability since it is one thread per I/O connection, and running thousands of threads significantly increases overhead on the operating system.
Therefore,
asynchronous
I/O seems to be better than synchronous I/O, since performance and scalability are keywords of the I/O system. Various important operating systems, such as Windows and Linux, support fast, scalable I/O based on the use of asynchronous notifications of I/O operations taking place in the OS layers.
Note:
That all asynchronous channels initiate I/O operations (does not block the application to perform other tasks) and provide notifications when I/O completes.
Contact Me
Thanks for
Attending & Listening.
Q
uestions
Think of a
link
as an entity mapping a file/directory path and identified through a set of names.
New Powerful;
Asynchronous is more familiar with parallel (as threading), while non-blocking often refers to polling.
Basically, there are two types of input/output (I/O) synchronization: synchronous I/O and asynchronous I/O (also referred to as overlapped I/O).
Essential Stuff
Advanced Stuff
UDP
is good for sending messages from one system to another when the order isn’t important and you don’t need all of the messages to get to the other machine.
This rule is the foundation of asynchronous channels, and from it derives the entire asynchronous channel API.
JSR 203
Like

Creating a Path.
Retrieving Information About a Path.
Removing Redundancies from a Path.
Converting a Path.
Joining Two Paths.
Creating a Path Between Two Paths.
Comparing Two Paths.
Creating a path is simple as:
Path p1 = Paths.get("/tmp/foo");
Path p2 = Paths.get(args[0]);
Path p3 = Paths.get(URI.create("file:///Users/taman/Test.java"));

Note:
Paths.get()
is a shorthand for:
Path p4 = FileSystems.getDefault().getPath("/users/taman");
You have a
Path
instance representing a file or directory, but does that file exist on the file system? Is it readable? Writable? Executable?
To convert the following path:
Path path = get("C:", "JavaOne2012/HOL2846-NIO2", "src/resources", "HOL2846_TOC.txt");
To:
A String:
path.toString();
A URI:
path.toUri();
An Absolute path:
path.toAbsolutePath();
A Real Path:
path.toRealPath(NOFOLLOW_LINKS);
A File:
path.toFile();
readAllBytes
readAlllines
newBufferedReader newBufferedWriter
newInputStream newOutputStream
newByteChannel
FileChannel
Commonly used,
small files.
for text files.
Streams, unbuffered
use with existing APIs
Channels and byte
buffers.
Advanced features,
file-locking, memory-mapped IO
File I/O Methods Arranged from Less Complex to More Complex
Many methods didn't throw exceptions when they failed.

The rename method didn't work consistently across platforms.

There was no real support for symbolic links.

More support for metadata was desired, such as file permissions, file owner, and other security attributes.

Accessing file metadata was inefficient.

Many of the File methods didn't scale. ex. Requesting a large directory listing over a server could result in a hang. Large directories could also cause memory resource problems, resulting in a denial of service.

It was not possible to write reliable code that could recursively walk a file tree and respond appropriately if there were circular symbolic links.
Prior to the Java
SE 7
release, the
java.io.File
class was the mechanism used for file I/O, but it had several drawbacks:
2013
2.0
Working as
Systems Architect & Design supervisor
for Egyptian ministry of finance company “
e
-finance
”.

JCP & EC
Member, responsible for JSRs revision, evaluation, RI testing and evangelist the new technologies specifications & JCP process standardization.

Beside that I am one of the board member of “
Oracle Egypt Architect Club
”.

A frequent Speaker

(
JavaOne, Devoxx, Oracle Days,Tunis JUG Day, jMaghreb, JEEConf & JDC... etc
),

Consultant, Trainer, Blogger, & articles writer, books author and tech. reviewer.

Regularly posts on my blog , and also on
DZone
&
JCG
community.
http://about.me/mohamedtaman
http://www.linkedin.com/in/mohamedtaman
@_tamanm
http://tamanmohamed.blogspot.com/
mohamed.taman@gmail.com
https://github.com/Adopt-a-JSR/JEE7-Hack-Sessions.git
See the full transcript