SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3Sunil OS
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략if kakao
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using javaUC San Diego
 
Java Networking
Java NetworkingJava Networking
Java NetworkingSunil OS
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
SPRING - MAVEN - REST API (ITA - Luglio 2017)
SPRING - MAVEN - REST API (ITA - Luglio 2017)SPRING - MAVEN - REST API (ITA - Luglio 2017)
SPRING - MAVEN - REST API (ITA - Luglio 2017)Valerio Radice
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJSunil OS
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 

Was ist angesagt? (20)

Spring AOP
Spring AOPSpring AOP
Spring AOP
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략스프링5 웹플럭스와 테스트 전략
스프링5 웹플럭스와 테스트 전략
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 
SPRING - MAVEN - REST API (ITA - Luglio 2017)
SPRING - MAVEN - REST API (ITA - Luglio 2017)SPRING - MAVEN - REST API (ITA - Luglio 2017)
SPRING - MAVEN - REST API (ITA - Luglio 2017)
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 

Ähnlich wie 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
 

Ähnlich wie NIO and NIO2 (20)

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
 
Ejb3 Dan Hinojosa
Ejb3 Dan HinojosaEjb3 Dan Hinojosa
Ejb3 Dan Hinojosa
 

Kürzlich hochgeladen

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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
 
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
 
[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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
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
 
🐬 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
 

Kürzlich hochgeladen (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
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...
 
[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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

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