SlideShare uma empresa Scribd logo
1 de 66
What's New in NIO 2 Ranjith Kumar N JUG-Chennai  9th June 2011
Agenda: NIO  ,[object Object]
Channels
SelectorsNIO 2.0	 ,[object Object]
New File System API
Asynchronous I/O,[object Object]
NIO Features Channel and Buffers File locking Memory Mapped Files Scatter  and Gather Channel to Channel Transfer Non-Blocking Sockets Multiplexed I/O
Buffersjava.nio
Buffer's  Basic Attributes: Capacity Limit Position	 Mark
Buffer Basic Operations: Creating Filling and Draining Flipping and Rewind Marking Comparing Duplicating
Creating public static XXXBuffer allocate (int capacity) public static XXXBuffer wrap (XXX [] array) public static XXXBuffer wrap (XXX [] array, int  offset,int length) For e.g. CharBuffer charBuffer= CharBuffer.allocate(10); char [] charArray = new char [10]; CharBuffercharbuffer = CharBuffer.wrap (charArray ); CharBuffercharbuffer = CharBuffer.wrap (charArray, 2, 7);
Filling and Draining XXXBuffer put (XXX b); XXXBuffer put (int index, XXX b); XXX get( ); XXX get(int index); XXXBufferput (XXX[] src); XXXBuffer put(XXX [] src, int offset, int length); XXXBuffer get(XXX[] dest); XXXBuffer get(XXX [] dest, int offset, int length);
Flipping and Rewind Manually Flipping a Buffer: buffer.limit(buffer.position( )).position(0) API provides Flip and rewind method: Buffer flip() Buffer rewind()
Marking Buffer mark() Buffer reset() For e.g buffer.position(2).mark( ).position(4);
Marking - cont. After calling reset method
Comparing boolean equals (Object ob) int compareTo (Object ob) Two buffers are considered to be equal if and only if: ,[object Object]
Both buffers have the same number of remaining elements.
The sequence of remaining data elements, which would be returned from get( ), must be identical in each buffer.,[object Object]
Comparing – cont. Two buffers considered to be unequal
Duplicating CharBuffer duplicate( ); CharBuffer asReadOnlyBuffer( ); CharBuffer slice( ); For e.g CharBuffer buffer = CharBuffer.allocate (8); buffer.position(3).limit (6).mark( ).position (5); 	CharBuffer dupeBuffer = buffer.duplicate( ); buffer.clear( );
Duplicating – cont.
Duplicating – cont. e.g.  CharBuffer buffer = CharBuffer.allocate (8); buffer.position(3).limit (5); 	CharBuffer sliceBuffer = buffer.slice( );
Byte Buffers
Direct Buffers: ByteBuffer allocateDirect (int capacity) boolean isDirect( ); If Direct Buffers are not used ,[object Object]
Copy the content of the nondirect buffer to the temporary buffer.
Perform the low-level I/O operation using the temporary buffer.
The temporary buffer object goes out of scope and is eventually garbage collected.,[object Object]
View Buffers: cont. ByteBuffer byteBuffer =ByteBuffer.allocate (7); CharBuffer charBuffer = byteBuffer.asCharBuffer( );
Channeljava.nio.channels
Channel basics Creating Reading/Writing Scatter/Gather Closing
Creating  SocketChannel sc = SocketChannel.open( ); ServerSocketChannelssc = ServerSocketChannel.open( ); DatagramChannel dc = DatagramChannel.open( ); RandomAccessFileraf = new RandomAccessFile ("somefile", "r"); 	FileChannel fc = raf.getChannel( );
Reading/Writing ReadableByteChannel ,[object Object],WritableByteChannel ,[object Object],E.g.. FileInputStream input = new FileInputStream(fileName); 		FileChannel channel = input.getChannel( ); channel.read(buffer);
Scatter/Gather ScatteringByteChannel read (ByteBuffer [] dsts) read (ByteBuffer [] dsts, int offset, int length) GatheringByteChannel write(ByteBuffer[] srcs) write(ByteBuffer[] srcs, int offset, int length)
Closing Channel ,[object Object]
void close( ),[object Object]
File Locking FileLock lock( ) FileLock lock (long position, long size,boolean shared) FileLocktryLock( ) FileLocktryLock (long position, long size,boolean shared)
Memory-Mapped Files MappedByteBuffer map (MapMode mode, long position,long size)
Channel 2 Channel transfer transferTo (long position, long count, WritableByteChannel target) transferFrom (ReadableByteChannel src,long position, long count)
Socket Channelsjava.nio.channels
Non-blocking Mode SelectableChannel configureBlocking (boolean block) boolean isBlocking( ) Object blockingLock( ) e.g. SocketChannel sc = SocketChannel.open( ); sc.configureBlocking (false); // nonblocking mode sc.configureBlocking (true); // blocking mode
Readiness Selection Socket channels can be check for readiness A single thread can monitor a large number of socket Steps to do readiness selection Create a Selector instance Register one or more non-blocking channels with it Implement a infinite loop and wait for events Get the selected keys list and iterate the keys Process the events. Remove the key from the list
Readiness Selection Selector selector = Selector.open(); ServerSocketChannelserverChannel = ServerSocketChannel.open(); serverChannel.socket().bind (new InetSocketAddress (port)); serverChannel.configureBlocking (false); SelectionKeyregisteredkey  = serverChannel.register (selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Iterator it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); //process  it.remove(); 		} }
Selector Key Methods: Selector open( ) int select( ) int select (long timeout) int selectNow( ) Set keys( ) Set selectedKeys( )
SelectableChannel Key methods: SelectionKey register (Selector sel, int ops) booleanisRegistered( ); SelectionKeykeyFor (Selector sel); int validOps( );
SelectionKey Key methods: SelectableChannel channel( ) Selector selector( )  void cancel( ) boolean isValid( ) booleanisReadable( ) boolean isWritable( ) boolean isConnectable( ) boolean isAcceptable( ) Operations to select: int OP_READ int OP_WRITE int OP_CONNECT int OP_ACCEPT
NIO 2JSR-203
FileChanneljava.nio.channels FileChannel open(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)  FileChannel open(Path path, OpenOption... options) e.g. Path file= Paths.get("C:homejugcread.txt"); FileChannelfc = (FileChannel.open(file, WRITE, APPEND ));
StandardOpenOption Enumjava.nio.file
SeekableByteChannel A byte channel that maintains a current position and allows the position to be changed.  Key Methods SeekableByteChannel position(long newPosition)  SeekableByteChannel truncate(long size)
NetworkChannel & MulticastChanneljava.nio.channels NetworkChannels: All the network-oriented channels implements the new NetworkChannel interface We can easily bind the channel socket, set and query for socket options MulticastChannels: We can send and receive IP datagrams from a complete group Implement by DatagramChannel and AsynchronousDatagramChannel
Pathjava.nio.file Locates a file using a system dependent path Defines methods to access and manipulate paths Defines methods to access files
Creating a Path // Microsoft Windows Path p1 = Paths.get("C:homejoefoo");  // Solaris syntax Path path = Paths.get("/home/joe/foo"); Path p4 = FileSystems.getDefault().getPath("/users/sally");
Key Methods Path normalize() Path toAbsolutePath() Path toRealPath(LinkOption... options) Path resolve(Path other) Path relativize(Path other) Pathjava.nio.file
Files java.nio.file Helper Class  Copy Move Delete Create file, directory and links
Copy long copy(InputStream in, Path target, CopyOption... options) long copy(Path source, OutputStream out) Path copy(Path source, Path target, CopyOption... options)
Move & Delete  void delete(Path path) booleandeleteIfExists(Path path) Path move(Path source, Path target, CopyOption... options)
File, Directory and link Path createFile(Path path, FileAttribute<?>... attrs) Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) Path createDirectory(Path dir, FileAttribute<?>... attrs) Path createDirectories(Path dir, FileAttribute<?>... attrs) Path createTempDirectory(Path dir, String prefix, FileAttribute<?>... attrs) Path createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
Walk File Tree Walk a file tree rooted at a given starting file Implement FileVisitor interface Initiate the process by calling anyone of the below method of java.nio.file.FilesClass walkFileTree(Path start, FileVisitor<? super Path> visitor) walkFileTree(Path start, Set<FileVisitOption> options, intmaxDepth, FileVisitor<? super Path> visitor)
FileVisitorjava.nio.file FileVisitResultpreVisitDirectory(T dir); FileVisitResultvisitFile(T file, BasicFileAttributes attrs); FileVisitResultpostVisitDirectory(T dir, IOExceptionexc); FileVisitResultpreVisitDirectoryFailed(T dir, IOExceptionexc); FileVisitResultvisitFileFailed(T file, IOExceptionexc);
FileVisitor Callback Methods Start Previsit Directory postVisitDirectory File 1 Link Root 1 visitFile visitFile Previsit Directory postVisitDirectory File 2 File 3 visitFile visitFile
File Change Notification Looking for file changes in FileSystem Using the native event facility whenever available Steps required to implement a watch service Create a WatchService "watcher" for the file system. For each directory that you want monitored, register it with the watcher. Implement an infinite loop to wait for incoming events Retrieve the key from the watcher's queue. Process all the events associated with the Key. Reset the key, and resume waiting for events. Close the service
File Change Notification – cont. WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir= Paths.get("C:homewatchdir");  WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, 				ENTRY_MODIFY); for( ; ; ){ WatchKeysignalledkey = watcher.take(); 	for (WatchEvent<?> event: signalledkey .pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); 		//  process the event 	} boolean valid = signalledkey .reset(); 	if (!valid) { 	break; 	} }
WatchServicejava.nio.file Watch registered objects for events and changes Key Methods WatchKey poll() WatchKey poll(long timeout, TimeUnit unit) WatchKey take() void close()
WatchKeyjava.nio.file WatchKey represents registration of a watchable 	object with a WatchService. Key Methods: List<WatchEvent<?>> pollEvents() Watchable watchable() boolean reset() boolean isValid() void cancel()
File Attributesjava.nio.file.attribute Meta-data associated with file Generalized metadata API Fetch a file's attributes in one bulk operation  Map<String,Object> readAttributes(Path, String, LinkOption...) <A extends BasicFileAttributes> readAttributes(Path, Class<A>, LinkOption...) Grouping of related attributes and Views to access the attribute groups.
Attributes View BasicFileAttributeView DosFileAttributeView PosixFileAttributeView FileOwnerAttributeView UserDefinedFileAttributeView File Attributesjava.nio.file.attribute

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
File handling
File handlingFile handling
File handling
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Java Exception Handling and Applets
Java Exception Handling and AppletsJava Exception Handling and Applets
Java Exception Handling and Applets
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Java 8 Date-Time API
Java 8 Date-Time APIJava 8 Date-Time API
Java 8 Date-Time API
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Files in java
Files in javaFiles in java
Files in java
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 

Semelhante a NIO and NIO2

Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code ExamplesNaresh Chintalcheru
 
WhatsNewNIO2.pdf
WhatsNewNIO2.pdfWhatsNewNIO2.pdf
WhatsNewNIO2.pdfMohit Kumar
 
5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системеDEVTYPE
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?Kevin Pilch
 
Laboratory 04 Circularly Linked ListDownload the following files.docx
Laboratory 04 Circularly Linked ListDownload the following files.docxLaboratory 04 Circularly Linked ListDownload the following files.docx
Laboratory 04 Circularly Linked ListDownload the following files.docxfestockton
 
JDK1.7 features
JDK1.7 featuresJDK1.7 features
JDK1.7 featuresindia_mani
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
 
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Charles Nutter
 
Buffer Overflows
Buffer OverflowsBuffer Overflows
Buffer OverflowsSumit Kumar
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoPaulo Morgado
 

Semelhante a NIO and NIO2 (20)

JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
Java
JavaJava
Java
 
NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
WhatsNewNIO2.pdf
WhatsNewNIO2.pdfWhatsNewNIO2.pdf
WhatsNewNIO2.pdf
 
5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
 
Laboratory 04 Circularly Linked ListDownload the following files.docx
Laboratory 04 Circularly Linked ListDownload the following files.docxLaboratory 04 Circularly Linked ListDownload the following files.docx
Laboratory 04 Circularly Linked ListDownload the following files.docx
 
5java Io
5java Io5java Io
5java Io
 
JDK1.7 features
JDK1.7 featuresJDK1.7 features
JDK1.7 features
 
package
packagepackage
package
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
Biopython
BiopythonBiopython
Biopython
 
Solr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene EuroconSolr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene Eurocon
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
 
C++ 11 usage experience
C++ 11 usage experienceC++ 11 usage experience
C++ 11 usage experience
 
Buffer Overflows
Buffer OverflowsBuffer Overflows
Buffer Overflows
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 

Último

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Último (20)

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

NIO and NIO2

  • 1. What's New in NIO 2 Ranjith Kumar N JUG-Chennai 9th June 2011
  • 2.
  • 4.
  • 6.
  • 7. NIO Features Channel and Buffers File locking Memory Mapped Files Scatter and Gather Channel to Channel Transfer Non-Blocking Sockets Multiplexed I/O
  • 9. Buffer's Basic Attributes: Capacity Limit Position Mark
  • 10. Buffer Basic Operations: Creating Filling and Draining Flipping and Rewind Marking Comparing Duplicating
  • 11. Creating public static XXXBuffer allocate (int capacity) public static XXXBuffer wrap (XXX [] array) public static XXXBuffer wrap (XXX [] array, int offset,int length) For e.g. CharBuffer charBuffer= CharBuffer.allocate(10); char [] charArray = new char [10]; CharBuffercharbuffer = CharBuffer.wrap (charArray ); CharBuffercharbuffer = CharBuffer.wrap (charArray, 2, 7);
  • 12. Filling and Draining XXXBuffer put (XXX b); XXXBuffer put (int index, XXX b); XXX get( ); XXX get(int index); XXXBufferput (XXX[] src); XXXBuffer put(XXX [] src, int offset, int length); XXXBuffer get(XXX[] dest); XXXBuffer get(XXX [] dest, int offset, int length);
  • 13. Flipping and Rewind Manually Flipping a Buffer: buffer.limit(buffer.position( )).position(0) API provides Flip and rewind method: Buffer flip() Buffer rewind()
  • 14. Marking Buffer mark() Buffer reset() For e.g buffer.position(2).mark( ).position(4);
  • 15. Marking - cont. After calling reset method
  • 16.
  • 17. Both buffers have the same number of remaining elements.
  • 18.
  • 19. Comparing – cont. Two buffers considered to be unequal
  • 20. Duplicating CharBuffer duplicate( ); CharBuffer asReadOnlyBuffer( ); CharBuffer slice( ); For e.g CharBuffer buffer = CharBuffer.allocate (8); buffer.position(3).limit (6).mark( ).position (5); CharBuffer dupeBuffer = buffer.duplicate( ); buffer.clear( );
  • 22. Duplicating – cont. e.g. CharBuffer buffer = CharBuffer.allocate (8); buffer.position(3).limit (5); CharBuffer sliceBuffer = buffer.slice( );
  • 24.
  • 25. Copy the content of the nondirect buffer to the temporary buffer.
  • 26. Perform the low-level I/O operation using the temporary buffer.
  • 27.
  • 28. View Buffers: cont. ByteBuffer byteBuffer =ByteBuffer.allocate (7); CharBuffer charBuffer = byteBuffer.asCharBuffer( );
  • 30. Channel basics Creating Reading/Writing Scatter/Gather Closing
  • 31. Creating SocketChannel sc = SocketChannel.open( ); ServerSocketChannelssc = ServerSocketChannel.open( ); DatagramChannel dc = DatagramChannel.open( ); RandomAccessFileraf = new RandomAccessFile ("somefile", "r"); FileChannel fc = raf.getChannel( );
  • 32.
  • 33. Scatter/Gather ScatteringByteChannel read (ByteBuffer [] dsts) read (ByteBuffer [] dsts, int offset, int length) GatheringByteChannel write(ByteBuffer[] srcs) write(ByteBuffer[] srcs, int offset, int length)
  • 34.
  • 35.
  • 36. File Locking FileLock lock( ) FileLock lock (long position, long size,boolean shared) FileLocktryLock( ) FileLocktryLock (long position, long size,boolean shared)
  • 37. Memory-Mapped Files MappedByteBuffer map (MapMode mode, long position,long size)
  • 38. Channel 2 Channel transfer transferTo (long position, long count, WritableByteChannel target) transferFrom (ReadableByteChannel src,long position, long count)
  • 40. Non-blocking Mode SelectableChannel configureBlocking (boolean block) boolean isBlocking( ) Object blockingLock( ) e.g. SocketChannel sc = SocketChannel.open( ); sc.configureBlocking (false); // nonblocking mode sc.configureBlocking (true); // blocking mode
  • 41. Readiness Selection Socket channels can be check for readiness A single thread can monitor a large number of socket Steps to do readiness selection Create a Selector instance Register one or more non-blocking channels with it Implement a infinite loop and wait for events Get the selected keys list and iterate the keys Process the events. Remove the key from the list
  • 42. Readiness Selection Selector selector = Selector.open(); ServerSocketChannelserverChannel = ServerSocketChannel.open(); serverChannel.socket().bind (new InetSocketAddress (port)); serverChannel.configureBlocking (false); SelectionKeyregisteredkey = serverChannel.register (selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Iterator it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); //process it.remove(); } }
  • 43. Selector Key Methods: Selector open( ) int select( ) int select (long timeout) int selectNow( ) Set keys( ) Set selectedKeys( )
  • 44. SelectableChannel Key methods: SelectionKey register (Selector sel, int ops) booleanisRegistered( ); SelectionKeykeyFor (Selector sel); int validOps( );
  • 45. SelectionKey Key methods: SelectableChannel channel( ) Selector selector( ) void cancel( ) boolean isValid( ) booleanisReadable( ) boolean isWritable( ) boolean isConnectable( ) boolean isAcceptable( ) Operations to select: int OP_READ int OP_WRITE int OP_CONNECT int OP_ACCEPT
  • 47. FileChanneljava.nio.channels FileChannel open(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)  FileChannel open(Path path, OpenOption... options) e.g. Path file= Paths.get("C:homejugcread.txt"); FileChannelfc = (FileChannel.open(file, WRITE, APPEND ));
  • 49. SeekableByteChannel A byte channel that maintains a current position and allows the position to be changed. Key Methods SeekableByteChannel position(long newPosition) SeekableByteChannel truncate(long size)
  • 50. NetworkChannel & MulticastChanneljava.nio.channels NetworkChannels: All the network-oriented channels implements the new NetworkChannel interface We can easily bind the channel socket, set and query for socket options MulticastChannels: We can send and receive IP datagrams from a complete group Implement by DatagramChannel and AsynchronousDatagramChannel
  • 51. Pathjava.nio.file Locates a file using a system dependent path Defines methods to access and manipulate paths Defines methods to access files
  • 52. Creating a Path // Microsoft Windows Path p1 = Paths.get("C:homejoefoo"); // Solaris syntax Path path = Paths.get("/home/joe/foo"); Path p4 = FileSystems.getDefault().getPath("/users/sally");
  • 53. Key Methods Path normalize() Path toAbsolutePath() Path toRealPath(LinkOption... options) Path resolve(Path other) Path relativize(Path other) Pathjava.nio.file
  • 54. Files java.nio.file Helper Class Copy Move Delete Create file, directory and links
  • 55. Copy long copy(InputStream in, Path target, CopyOption... options) long copy(Path source, OutputStream out) Path copy(Path source, Path target, CopyOption... options)
  • 56. Move & Delete void delete(Path path) booleandeleteIfExists(Path path) Path move(Path source, Path target, CopyOption... options)
  • 57. File, Directory and link Path createFile(Path path, FileAttribute<?>... attrs) Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) Path createDirectory(Path dir, FileAttribute<?>... attrs) Path createDirectories(Path dir, FileAttribute<?>... attrs) Path createTempDirectory(Path dir, String prefix, FileAttribute<?>... attrs) Path createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
  • 58. Walk File Tree Walk a file tree rooted at a given starting file Implement FileVisitor interface Initiate the process by calling anyone of the below method of java.nio.file.FilesClass walkFileTree(Path start, FileVisitor<? super Path> visitor) walkFileTree(Path start, Set<FileVisitOption> options, intmaxDepth, FileVisitor<? super Path> visitor)
  • 59. FileVisitorjava.nio.file FileVisitResultpreVisitDirectory(T dir); FileVisitResultvisitFile(T file, BasicFileAttributes attrs); FileVisitResultpostVisitDirectory(T dir, IOExceptionexc); FileVisitResultpreVisitDirectoryFailed(T dir, IOExceptionexc); FileVisitResultvisitFileFailed(T file, IOExceptionexc);
  • 60. FileVisitor Callback Methods Start Previsit Directory postVisitDirectory File 1 Link Root 1 visitFile visitFile Previsit Directory postVisitDirectory File 2 File 3 visitFile visitFile
  • 61. File Change Notification Looking for file changes in FileSystem Using the native event facility whenever available Steps required to implement a watch service Create a WatchService "watcher" for the file system. For each directory that you want monitored, register it with the watcher. Implement an infinite loop to wait for incoming events Retrieve the key from the watcher's queue. Process all the events associated with the Key. Reset the key, and resume waiting for events. Close the service
  • 62. File Change Notification – cont. WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir= Paths.get("C:homewatchdir"); WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); for( ; ; ){ WatchKeysignalledkey = watcher.take(); for (WatchEvent<?> event: signalledkey .pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); // process the event } boolean valid = signalledkey .reset(); if (!valid) { break; } }
  • 63. WatchServicejava.nio.file Watch registered objects for events and changes Key Methods WatchKey poll() WatchKey poll(long timeout, TimeUnit unit) WatchKey take() void close()
  • 64. WatchKeyjava.nio.file WatchKey represents registration of a watchable object with a WatchService. Key Methods: List<WatchEvent<?>> pollEvents() Watchable watchable() boolean reset() boolean isValid() void cancel()
  • 65. File Attributesjava.nio.file.attribute Meta-data associated with file Generalized metadata API Fetch a file's attributes in one bulk operation Map<String,Object> readAttributes(Path, String, LinkOption...) <A extends BasicFileAttributes> readAttributes(Path, Class<A>, LinkOption...) Grouping of related attributes and Views to access the attribute groups.
  • 66. Attributes View BasicFileAttributeView DosFileAttributeView PosixFileAttributeView FileOwnerAttributeView UserDefinedFileAttributeView File Attributesjava.nio.file.attribute
  • 67. File Attributesjava.nio.file.attribute Path file = Paths.get("C:homefile.txt"); ; BasicFileAttributesattr = Files.readAttributes(file, BasicFileAttributes.class); DosFileAttributesdosattr = Files.readAttributes(file, DosFileAttributes.class); PosixFileAttributesposixattr = Files.readAttributes(file, PosixFileAttributes.class);
  • 68. Asynchronous I/Ojava.nio.channels They provide asynchronous operations for both sockets and files. All operations work in non-blocking mode. All the asynchronous I/O operations have one of two forms : The first one returns a java.util.concurrent.Future that represent the pending result The second one is created using a CompletionHandler.
  • 69. Futurejava.util.concurrent AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(“C:homeasyncfile.txt ")); ByteBuffer buffer = ByteBuffer.allocate(100); Future result = channel.read(buffer, 100); boolean done = result.isDone();
  • 70. CompletionHandler Future result = channel.read(buffer, 100, buffer, new CompletionHandler(){ public void completed(Integer result, ByteBuffer buffer){ //Compute the result } public void failed(Throwable exception, ByteBuffer buffer){ // Handle the error } });
  • 71. References: Java NIO by Ron Hitchens Publisher: O’Reilly http://java.sun.com/developer/technicalArticles/javase/nio/ http://download.oracle.com/javase/tutorial/essential/io/index.html