SlideShare una empresa de 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

Más contenido relacionado

La actualidad más candente

Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMashleypuls
 
Java and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystemJava and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystemRafael Winterhalter
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheeltcurdt
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeletonIram Ramrajkar
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agentsRafael Winterhalter
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 
Clojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVMClojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVMsunng87
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVMRafael Winterhalter
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code ExamplesNaresh Chintalcheru
 

La actualidad más candente (20)

Java 7 & 8 New Features
Java 7 & 8 New FeaturesJava 7 & 8 New Features
Java 7 & 8 New Features
 
What`s new in Java 7
What`s new in Java 7What`s new in Java 7
What`s new in Java 7
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASM
 
Java and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystemJava and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystem
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agents
 
The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
Byte code field report
Byte code field reportByte code field report
Byte code field report
 
Clojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVMClojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVM
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
Ejb3 Dan Hinojosa
Ejb3 Dan HinojosaEjb3 Dan Hinojosa
Ejb3 Dan Hinojosa
 
Java Programming - 06 java file io
Java Programming - 06 java file ioJava Programming - 06 java file io
Java Programming - 06 java file io
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 

Destacado

Destacado (8)

Invoke dynamics
Invoke dynamicsInvoke dynamics
Invoke dynamics
 
nescala 2013
nescala 2013nescala 2013
nescala 2013
 
Scala magic
Scala magicScala magic
Scala magic
 
Jdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamicJdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamic
 
Implicit Explicit Scala
Implicit Explicit ScalaImplicit Explicit Scala
Implicit Explicit Scala
 
Implicit Implicit Scala
Implicit Implicit ScalaImplicit Implicit Scala
Implicit Implicit Scala
 
Lunch_and_Learn_20150603
Lunch_and_Learn_20150603Lunch_and_Learn_20150603
Lunch_and_Learn_20150603
 
NE Scala 2016 roundup
NE Scala 2016 roundupNE Scala 2016 roundup
NE Scala 2016 roundup
 

Similar a Nio nio2

Java nio ( new io )
Java nio ( new io )Java nio ( new io )
Java nio ( new io )Jemin Patel
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
 
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
 

Similar a Nio nio2 (20)

JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
Java nio ( new io )
Java nio ( new io )Java nio ( new io )
Java nio ( new io )
 
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
 
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

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Último (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Nio 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