java new i/o ron hitchens [email protected] java nio book website java metroplex user's group...

43
Java New I/O Ron Hitchens [email protected] http://www.ronsoft.com Java NIO Book Website http://javanio.info Java Metroplex User's Group May 14, 2003 © 2003, Ronsoft Technologies

Upload: todd-cooper

Post on 25-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Java New I/O

Ron [email protected]

http://www.ronsoft.com

Java NIO Book Websitehttp://javanio.info

Java Metroplex User's GroupMay 14, 2003

© 2003, Ronsoft Technologies

Page 2: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,
Page 3: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Pig-Footed Bandicoot

See http://javanio.info for details

Page 4: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Check Your Local Bookstore

Page 5: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Speaker Bio25+ Years Computer Experience

Mainframe to Micro

Unix/Linux kernel, academic, internet ~20 years

5+ Years Java ExperienceHeavy server-side, web apps, startups

Independent Consultant“Bit twiddling at it's finest”

Will hack NIO for fun and profit

Hook ‘Em Horns

Page 6: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Why NIO?

Where did it come from?

What does it do for me?

When should I use it?

Should I stop using java.io?

Page 7: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Efficiency – Scalability – Reliability

Efficiency – The Need For SpeedWhy should the JVM do what the OS can do better?

Scalability – Livin' LargeBig applications have big appetites

Reliability – Enough Wheels InventedThe infrastructure exists – concentrate on the app

No Longer CPU BoundJSR 51 (http://www.jcp.org/en/jsr/detail?id=51)

Page 8: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

What Does NIO Do For Me?

New AbstractionsBuffers, Channels and Selectors

New CapabilitiesNon-Blocking Sockets

File Locking

Memory Mapping

Readiness Selection

Regular Expressions

Pluggable Charset Transcoders

Page 9: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Use NIO When You Need To:

Move large amounts of data efficientlyNIO is primarily block oriented – java.io uses streams

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

Multiplex large numbers of open socketsOperates 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

Page 10: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Should I Stop Using java.io?

Nope

java.nio is not a replacement for java.io

NIO addresses different needs

java.io is not going away

Page 11: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

What Makes Up NIO?

Buffers

Channels

Selectors

Regular Expressions

Character Set Coding

Page 12: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

NIO Buffers

Fixed size containers of primitive data typesByteBuffer, CharBuffer, FloatBuffer, etc.

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

Direct and Non-direct ByteBuffersDirect 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

Page 13: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Buffer Classes

Page 14: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Buffer Objects (Empty/Fill)

Page 15: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Buffer Objects (Flip)

Page 16: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Buffer Views (Dupe/Slice)

Page 17: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Buffer Views (Char View)

Page 18: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

I'm Confused...Show Me

Page 19: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Hello?

public class HelloWorld{

public static void main (String [] argv){

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

}

Page 20: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

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);wbc.close();

}}

Page 21: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

NIO Channels

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

Channels do bulk data transfers to and from bufferschannel.write (buffer) ~= buffer.get (byteArray)channel.read (buffer) ~= buffer.put (byteArray)

Scatter/gather, channel-to-channel transfers

Three primary channel implementationsFileChannel: 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)

Page 22: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Channel Copy – Simple #1*

public void channelCopy (ReadableByteChannel src, WritableByteChannel dest)throws IOException

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

while (src.read (buffer) != -1) {// prepare the buffer to be drainedbuffer.flip();

// make sure the buffer was fully drained.while (buffer.hasRemaining()) {

dest.write (buffer);}

// make the buffer empty, ready for fillingbuffer.clear();

}}

* No buffer copies, but potentially more system calls.

Page 23: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Channel Copy – Simple #2*

public void channelCopy (ReadableByteChannel src, WritableByteChannel dest)throws IOException

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

while (src.read (buffer) != -1) {// prepare the buffer to be drainedbuffer.flip();

// write to the channel, may blockdest.write (buffer);

// if partial transfer, shift remaining elements down// if buffer was empty, same as doing clearbuffer.compact();

}

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.

Page 24: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

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.

Page 25: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Memory Mapped Buffers

RandomAccessFile raf = new RandomAccessFile (fileName, "rw");FileChannel fc = raf.getChannel();MappedByteBuffer buffer = fc.map (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 fileNameAny change to one affects the other

Page 26: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Non-Blocking Sockets – Simple Really

ByteBuffer buffer = ByteBuffer.allocate (1024);SocketChannel socketChannel = SocketChannel.open();socketChannel.configureBlocking (false);

...

while (true) { ... if (socketChannel.read (buffer) != 0) { processInput (buffer); } ...}

Page 27: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Non-Blocking Server Socket

ServerSocketChannel ssc = ServerSocketChannel.open();

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

while (true) {SocketChannel newConnection = ssc.accept();

if (newConnection == null) {doSomethingToKeepBusy();

} else {doSomethingWithSocket (newConnection);

}}

Page 28: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

NIO Selectors

Multiplexing Channels – Readiness Selection

Selectable Channels are registered with SelectorsSelectionKey encapsulates selector/channel relationship

A subset of ready channels is selected from the Selector's set of registered channels (Selector.select())

Selected Set contains those keys with non-empty Ready Sets

Each SelectionKey holds an Interest Set and a Ready SetPossible 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

Page 29: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Selectors, Keys and Channels

Page 30: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Registering With a Selector

ServerSocketChannel serverChannel = ServerSocketChannel.open();Selector selector = Selector.open();

serverChannel.socket().bind (new InetSocketAddress (port));serverChannel.configureBlocking (false);serverChannel.register (selector, SelectionKey.OP_ACCEPT);

Page 31: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

The Selection Process

Create a Selector and register channels with itThe register() method is on SelectableChannel, not Selector

Invoke select() on the Selector object

Retrieve the Selected Set of keys from the SelectorSelected set: Registered keys with non-empty Ready Sets

keys = selector.selectedKeys()

Iterate over the Selected SetCheck 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 (key.channel()) as appropriate (read, write, etc)

Page 32: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Running a Selection Loopwhile (true) { selector.select();

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

while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next();

it.remove();

if (key.isAcceptable()) { ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel = server.accept(); channel.configureBlocking (false); channel.register (selector, SelectionKey.OP_READ); }

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

Page 33: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Scalability With Selectors

One Thread to Rule Them AllMore threads != More Efficient – Context Switching, CPU Availability

OS and/or JVM do the hard work for youOnly the kernel can efficiently do Readiness Selection

No more thread-per-socket nonsenseSimpler, easier to maintain code

Less concurrency hassles – locking overhead, thread races

Single point of dispatch

Not necessarily single-threadedSingle selection thread can dispatch to multiple worker threads

Page 34: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

How Does That Work...Exactly?

Page 35: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

NIO Regular Expressions

Perl 5-ish syntax

New CharSequence interface in java.lang

Pattern and Matcher objects

String class has regex convenience methods added

Page 36: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

java.lang.CharSequence

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

Page 37: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Regex CSV Tokenizer

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

for (int i = 0; i < tokens.length; i++) {System.out.println ("" + i + ": " + tokens [i]);

}

Page 38: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

Regex Email Address Parsingpublic static final String VALID_EMAIL_PATTERN =

"([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]"+ "{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);}...

}

Page 39: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

NIO Charsets

Character Set Coding

Character Set, Coded Character Set, Coding Scheme

Encoding and decoding objects

Character sets are pluggable (SPI)

Page 40: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

The JNI ConnectionJava Code Can Allocate Native Memory (Direct)

Native Code Can Create and/or Use Buffers

Buffers Can Wrap Arbitrary Memory SpacesVideo memory, device controllers, etc.

All Buffers Are Java ObjectsScoping, Garbage Collection, Typing, Etc.

Zoom ZoomOpenGL For Java (http://www.jausoft.com)

JCanyon F16 Flight Simulator

Page 41: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Ronsoft Technologieshttp://javanio.info

http://www.ronsoft.com

What Did They Leave Out?

Formatted I/O (ala printf/scanf)Will leverage Regular Expressions

Enhanced Filesystem InterfaceMore consistent across OS platforms

Better access to file/directory attributes

Pluggable access to new filesystem types

True Asynchronous I/OUnder consideration, may never happen

Page 42: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

Questions

?? ?

??

?

???

?

Page 43: Java New I/O Ron Hitchens ron@ronsoft.com  Java NIO Book Website  Java Metroplex User's Group May 14, 2003 © 2003,

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

Ron (and Miranda) Hitchens [email protected]

http://www.ronsoft.com http://javanio.info