Java New I/O - PowerPoint PPT Presentation

Java new i o l.jpg
1 / 43

  • Updated On :
  • Presentation posted in: General

Java New I/O. Ron Hitchens Java NIO Book Website Java Metroplex User's Group May 14, 2003. © 2003, Ronsoft Technologies. Pig-Footed Bandicoot. See for details. Check Your Local Bookstore. Speaker Bio.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Java New I/O

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Java new i o l.jpg

Java New I/O

Ron Hitchens

Java NIO Book Website

Java Metroplex User's Group

May 14, 2003

© 2003, Ronsoft Technologies

Pig footed bandicoot l.jpg

Pig-Footed Bandicoot

See for details

Check your local bookstore l.jpg

Check Your Local Bookstore

Speaker bio l.jpg

Speaker Bio

  • 25+ Years Computer Experience

    • Mainframe to Micro

    • Unix/Linux kernel, academic, internet ~20 years

  • 5+ Years Java Experience

    • Heavy server-side, web apps, startups

  • Independent Consultant

    • “Bit twiddling at it's finest”

    • Will hack NIO for fun and profit

  • Hook ‘Em Horns

Ronsoft Technologies

Why nio l.jpg

Why NIO?

  • Where did it come from?

  • What does it do for me?

  • When should I use it?

  • Should I stop using

Ronsoft Technologies

Efficiency scalability reliability l.jpg

Efficiency – Scalability – Reliability

  • Efficiency – The Need For Speed

    • Why should the JVM do what the OS can do better?

  • Scalability – Livin' Large

    • Big applications have big appetites

  • Reliability – Enough Wheels Invented

    • The infrastructure exists – concentrate on the app

  • No Longer CPU Bound

  • JSR 51(

Ronsoft Technologies

What does nio do for me l.jpg

What Does NIO Do For Me?

  • New Abstractions

    • Buffers, Channels and Selectors

  • New Capabilities

    • Non-Blocking Sockets

    • File Locking

    • Memory Mapping

    • Readiness Selection

    • Regular Expressions

    • Pluggable Charset Transcoders

Ronsoft Technologies

Use nio when you need to l.jpg

Use NIO When You Need To:

  • Move large amounts of data efficiently

    • NIO is primarily block oriented – uses streams

    • Uses direct buffers to do raw I/O – bypassing the JVM

  • Multiplex large numbers of open sockets

    • Operates in non-blocking mode

    • One thread can manage huge numbers of socket channels

  • Use OS-level file locking or memory mapping

  • Do character set Transcoding

Ronsoft Technologies

Should i stop using java io l.jpg

Should I Stop Using

  • Nope

  • java.nio is not a replacement for

  • NIO addresses different needs

  • is not going away

Ronsoft Technologies

What makes up nio l.jpg

What Makes Up NIO?

  • Buffers

  • Channels

  • Selectors

  • Regular Expressions

  • Character Set Coding

Ronsoft Technologies

Nio buffers l.jpg

NIO Buffers

  • Fixed size containers of primitive data types

    • ByteBuffer, CharBuffer, FloatBuffer, etc.

  • Byte buffers are special, used for I/O with channels

  • Direct and Non-direct ByteBuffers

    • Direct ByteBuffers address raw memory – direct I/O

  • Buffers can be views of other buffers or wrap arrays

  • Byte order (endian-ness)

    • Affects byte swabbing in views of ByteBuffers

Ronsoft Technologies

Buffer classes l.jpg

Buffer Classes

Ronsoft Technologies

Buffer objects empty fill l.jpg

Buffer Objects (Empty/Fill)

Ronsoft Technologies

Buffer objects flip l.jpg

Buffer Objects (Flip)

Ronsoft Technologies

Buffer views dupe slice l.jpg

Buffer Views (Dupe/Slice)

Ronsoft Technologies

Buffer views char view l.jpg

Buffer Views (Char View)

Ronsoft Technologies

I m confused show me l.jpg

I'm Confused...Show Me

Ronsoft Technologies

Hello l.jpg


public class HelloWorld


public static void main (String [] argv)


System.out.println ("Hello World");



Ronsoft Technologies

Hello nio l.jpg

Hello NIO?

import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;

import java.nio.channels.Channels;

public class HelloWorldNio


public static void main (String [] argv)

throws Exception


String hello = "Hello World" + System.getProperty ("line.separator");

ByteBuffer bb = ByteBuffer.wrap (hello.getBytes ("UTF-8"));

WritableByteChannel wbc = Channels.newChannel (System.out);

wbc.write (bb);




Ronsoft Technologies

Nio channels l.jpg

NIO Channels

  • New I/O metaphor: Conduit to an I/O service (“nexus”)

  • Channels do bulk data transfers to and from buffers

    • channel.write (buffer) ~= buffer.get (byteArray)

    • (buffer) ~= buffer.put (byteArray)

  • Scatter/gather, channel-to-channel transfers

  • Three primary channel implementations

    • FileChannel: File locks, memory mapping, cross-connect transfers

    • Sockets: Non-blocking, selectable, async connections, peers

    • Pipe: loopback channel pair, selectable, generic channels

  • Selectable Channel Implementations are pluggable (SPI)

Ronsoft Technologies

Channel copy simple 1 l.jpg

Channel Copy – Simple #1*

public void channelCopy (ReadableByteChannel src, WritableByteChannel dest)

throws IOException


ByteBuffer buffer = ByteBuffer.allocate (16 * 1024);

while ( (buffer) != -1) {

// prepare the buffer to be drained


// make sure the buffer was fully drained.

while (buffer.hasRemaining()) {

dest.write (buffer);


// make the buffer empty, ready for filling




* No buffer copies, but potentially more system calls.

Ronsoft Technologies

Channel copy simple 2 l.jpg

Channel Copy – Simple #2*

public void channelCopy (ReadableByteChannel src, WritableByteChannel dest)

throws IOException


ByteBuffer buffer = ByteBuffer.allocate (16 * 1024);

while ( (buffer) != -1) {

// prepare the buffer to be drained


// write to the channel, may block

dest.write (buffer);

// if partial transfer, shift remaining elements down

// if buffer was empty, same as doing clear



buffer.flip();// EOF leaves buffer in fill state

while (buffer.hasRemaining()) {

dest.write (buffer);



* Minimal system calls, but may do buffer copies. Post loop cleanup needed.

Ronsoft Technologies

Channel copy transfer l.jpg

Channel Copy – Transfer*

public void channelCopy (FileChannel src, WritableByteChannel dest)

throws IOException


src.transferTo (0, src.size(), dest);


public void channelCopy (ReadableByteChannel src, FileChannel dest)

throws IOException


dest.transferFrom (src, 0, Long.MAX_VALUE);


* Very easy, but one end must always be a FileChannel. Transfer could occur entirely in kernel space.

Ronsoft Technologies

Memory mapped buffers l.jpg

Memory Mapped Buffers

RandomAccessFile raf = new RandomAccessFile (fileName, "rw");

FileChannel fc = raf.getChannel();

MappedByteBuffer buffer = (FileChannel.MapMode.READ_WRITE, 0, fc.size());

byte b = buffer.get();// reads from file


buffer.put (someOtherByte);// writes to file

The content of buffer is the content of fileName

Any change to one affects the other

Ronsoft Technologies

Non blocking sockets simple really l.jpg

Non-Blocking Sockets – Simple Really

ByteBuffer buffer = ByteBuffer.allocate (1024);

SocketChannel socketChannel =;

socketChannel.configureBlocking (false);


while (true) {


if ( (buffer) != 0) {

processInput (buffer);




Ronsoft Technologies

Non blocking server socket l.jpg

Non-Blocking Server Socket

ServerSocketChannel ssc =;

ssc.socket().bind (new InetSocketAddress (port));

ssc.configureBlocking (false);

while (true) {

SocketChannel newConnection = ssc.accept();

if (newConnection == null) {


} else {

doSomethingWithSocket (newConnection);



Ronsoft Technologies

Nio selectors l.jpg

NIO Selectors

  • Multiplexing Channels – Readiness Selection

  • Selectable Channels are registered with Selectors

    • SelectionKey encapsulates selector/channel relationship

  • A subset of ready channels is selected from the Selector's set of registered channels (

    • Selected Set contains those keys with non-empty Ready Sets

  • Each SelectionKey holds an Interest Set and a Ready Set

    • Possible members of Interest Set: accept, read, write, connect

    • Ready set is a subset of interest set –as-of the last select() call

  • Readiness Selection means less work – ignore idle channels

Ronsoft Technologies

Selectors keys and channels l.jpg

Selectors, Keys and Channels

Ronsoft Technologies

Registering with a selector l.jpg

Registering With a Selector

ServerSocketChannel serverChannel =;

Selector selector =;

serverChannel.socket().bind (new InetSocketAddress (port));

serverChannel.configureBlocking (false);

serverChannel.register (selector, SelectionKey.OP_ACCEPT);

Ronsoft Technologies

The selection process l.jpg

The Selection Process

  • Create a Selector and register channels with it

    • The register() method is on SelectableChannel, not Selector

  • Invoke select() on the Selector object

  • Retrieve the Selected Set of keys from the Selector

    • Selected set: Registered keys with non-empty Ready Sets

    • keys = selector.selectedKeys()

  • Iterate over the Selected Set

    • Check each key's Ready Set (set of operations ready to go as-of last select())

    • Remove the key from the Selected Set (iterator.remove())

      • Bits in the Ready Sets are never reset while the key is in the Selected Set

      • The Selector never removes keys from the Selected Set – you must do so

    • Service the channel ( as appropriate (read, write, etc)

  • Ronsoft Technologies

    Running a selection loop l.jpg

    Running a Selection Loop

    while (true) {;

    Iterator it = selector.selectedKeys().iterator();

    while (it.hasNext()) {

    SelectionKey key = (SelectionKey);


    if (key.isAcceptable()) {

    ServerSocketChannel server =


    SocketChannel channel = server.accept();

    channel.configureBlocking (false);

    channel.register (selector, SelectionKey.OP_READ);


    if (key.isReadable()) readDataFromSocket (key);



    Ronsoft Technologies

    Scalability with selectors l.jpg

    Scalability With Selectors

    • One Thread to Rule Them All

      • More threads != More Efficient – Context Switching, CPU Availability

    • OS and/or JVM do the hard work for you

      • Only the kernel can efficiently do Readiness Selection

    • No more thread-per-socket nonsense

      • Simpler, easier to maintain code

      • Less concurrency hassles – locking overhead, thread races

      • Single point of dispatch

    • Not necessarily single-threaded

      • Single selection thread can dispatch to multiple worker threads

    Ronsoft Technologies

    How does that work exactly l.jpg

    How Does That Work...Exactly?

    Ronsoft Technologies

    Nio regular expressions l.jpg

    NIO Regular Expressions

    • Perl 5-ish syntax

    • New CharSequence interface in java.lang

    • Pattern and Matcher objects

    • String class has regex convenience methods added

    Ronsoft Technologies

    Java lang charsequence l.jpg


    Package java.lang;

    public interface CharSequence


    int length();

    char charAt(int index);

    CharSequence subSequence(int start, int end);

    public String toString();


    Implemented by String, StringBuffer and CharBuffer

    Ronsoft Technologies

    Regex csv tokenizer l.jpg

    Regex CSV Tokenizer

    String [] tokens = lineBuffer.split ("\\s*,\\s*");

    for (int i = 0; i < tokens.length; i++) {

    System.out.println ("" + i + ": " + tokens [i]);


    Ronsoft Technologies

    Regex email address parsing l.jpg

    Regex Email Address Parsing

    public static final String VALID_EMAIL_PATTERN =


    + "{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))"

    + "([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)";


    public void setupPerson (Person person, ..., String emailAddress)



    if (emailAddress.matches (VALID_EMAIL_PATTERN)) {

    person.setEmailAddress (emailAddress);

    } else {

    throw new IllegalArgumentException (emailAddress);




    Ronsoft Technologies

    Nio charsets l.jpg

    NIO Charsets

    • Character Set Coding

    • Character Set, Coded Character Set, Coding Scheme

    • Encoding and decoding objects

    • Character sets are pluggable (SPI)

    Ronsoft Technologies

    The jni connection l.jpg

    The JNI Connection

    • Java Code Can Allocate Native Memory (Direct)

    • Native Code Can Create and/or Use Buffers

    • Buffers Can Wrap Arbitrary Memory Spaces

      • Video memory, device controllers, etc.

    • All Buffers Are Java Objects

      • Scoping, Garbage Collection, Typing, Etc.

    • Zoom Zoom

      • OpenGL For Java (

      • JCanyon F16 Flight Simulator

    Ronsoft Technologies

    What did they leave out l.jpg

    What Did They Leave Out?

    • Formatted I/O (ala printf/scanf)

      • Will leverage Regular Expressions

    • Enhanced Filesystem Interface

      • More consistent across OS platforms

      • Better access to file/directory attributes

      • Pluggable access to new filesystem types

    • True Asynchronous I/O

      • Under consideration, may never happen

    Ronsoft Technologies

    Questions l.jpg












    Bye bye buy my daddy s book i think i see one right over there l.jpg

    Bye ByeBuy my Daddy's book. I think I see one right over there.

    Ron (and Miranda) Hitchens

  • Login