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

Similar a NIO and NIO2

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
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)Ortus Solutions, Corp
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...Ortus Solutions, Corp
 

Similar a NIO and NIO2 (20)

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
 
Ejb3 Dan Hinojosa
Ejb3 Dan HinojosaEjb3 Dan Hinojosa
Ejb3 Dan Hinojosa
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
 

Último

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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave 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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[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
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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
 

Último (20)

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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave 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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[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
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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
 

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