SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Deep Dive In To Redis Replication: Vishy Kasar
Deep Dive In To Redis Replication: Vishy KasarDeep Dive In To Redis Replication: Vishy Kasar
Deep Dive In To Redis Replication: Vishy KasarRedis Labs
 
Project ACRN Device Passthrough Introduction
Project ACRN Device Passthrough IntroductionProject ACRN Device Passthrough Introduction
Project ACRN Device Passthrough IntroductionProject ACRN
 
Servlet 3.1 Async I/O
Servlet 3.1 Async I/OServlet 3.1 Async I/O
Servlet 3.1 Async I/OSimone Bordet
 
Security in PHP - 那些在滲透測試的小技巧
Security in PHP - 那些在滲透測試的小技巧Security in PHP - 那些在滲透測試的小技巧
Security in PHP - 那些在滲透測試的小技巧Orange Tsai
 
BPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLabBPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLabTaeung Song
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniqueAngel Boy
 
滲透測試 Talk @ Nisra
滲透測試 Talk @ Nisra滲透測試 Talk @ Nisra
滲透測試 Talk @ NisraOrange Tsai
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking ExplainedThomas Graf
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19José Paumard
 
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...Maksim Shudrak
 
Advanced heap exploitaion
Advanced heap exploitaionAdvanced heap exploitaion
Advanced heap exploitaionAngel Boy
 
DeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows KernelDeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows KernelPeter Hlavaty
 
Java nio ( new io )
Java nio ( new io )Java nio ( new io )
Java nio ( new io )Jemin Patel
 
MacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) ExploitationMacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) ExploitationAngel Boy
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in JavaAllan Huang
 

Tendances (20)

Deep Dive In To Redis Replication: Vishy Kasar
Deep Dive In To Redis Replication: Vishy KasarDeep Dive In To Redis Replication: Vishy Kasar
Deep Dive In To Redis Replication: Vishy Kasar
 
Applied Computer Science Concepts in Android
Applied Computer Science Concepts in AndroidApplied Computer Science Concepts in Android
Applied Computer Science Concepts in Android
 
Project ACRN Device Passthrough Introduction
Project ACRN Device Passthrough IntroductionProject ACRN Device Passthrough Introduction
Project ACRN Device Passthrough Introduction
 
OpenCL Heterogeneous Parallel Computing
OpenCL Heterogeneous Parallel ComputingOpenCL Heterogeneous Parallel Computing
OpenCL Heterogeneous Parallel Computing
 
Servlet 3.1 Async I/O
Servlet 3.1 Async I/OServlet 3.1 Async I/O
Servlet 3.1 Async I/O
 
Security in PHP - 那些在滲透測試的小技巧
Security in PHP - 那些在滲透測試的小技巧Security in PHP - 那些在滲透測試的小技巧
Security in PHP - 那些在滲透測試的小技巧
 
BPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLabBPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLab
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit Technique
 
滲透測試 Talk @ Nisra
滲透測試 Talk @ Nisra滲透測試 Talk @ Nisra
滲透測試 Talk @ Nisra
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 
Signal
SignalSignal
Signal
 
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
 
Advanced heap exploitaion
Advanced heap exploitaionAdvanced heap exploitaion
Advanced heap exploitaion
 
DeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows KernelDeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows Kernel
 
Java nio ( new io )
Java nio ( new io )Java nio ( new io )
Java nio ( new io )
 
Applets
AppletsApplets
Applets
 
MacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) ExploitationMacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) Exploitation
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 

Similaire à 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
 

Similaire à NIO and NIO2 (20)

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

Dernier

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Dernier (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

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