handling i/o in java
DESCRIPTION
An overview of handling I/O in Java, best practices and patterns.TRANSCRIPT
![Page 1: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/1.jpg)
Handling I/O in JavaTechniques , Gotchas & Best Practices
Hiranya JayathilakaSenior Technical Lead – WSO2
Apache Committer
![Page 2: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/2.jpg)
Overview & Techniques
![Page 3: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/3.jpg)
What is I/O?
Communication between the computer and the outside world
![Page 4: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/4.jpg)
What’s in the Outside World?
• Human users• Hardware devices–Monitors, keyboards, disk drives …
• Other computers and programs
![Page 5: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/5.jpg)
Why Should We Care?
• An essential part of most programs• Computers spend a lot of time performing
nothing but I/O operations• Very complicated business to deal with• Ranks No.1 in the performance killer list
![Page 6: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/6.jpg)
What are the Different Types of I/O?
• Console I/O• Keyboard I/O• File I/O• Network I/O• …and possibly more
![Page 7: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/7.jpg)
How Does Java Support I/O?
• The Java I/O API– The java.io package– Since JDK 1.0– Extensible
![Page 8: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/8.jpg)
Stream
http://natureadore.blogspot.com/p/stream.html
![Page 9: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/9.jpg)
Reading and Writing with Streams
InputStream in = openInputStream();int b = in.read();
byte[] data = new byte[1024];int len = in.read(data);
OutputStream out = openOutputStream();out.write(b);Out.write(data, 0, len);
![Page 10: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/10.jpg)
Readers and Writers
• Utilities for reading and writing character streams
Reader reader = getReader();int ch = reader.read();
char[] data = new char[1024];int len = reader.read(data);
Writer writer = getWriter();writer.write(ch);writer.write(data, 0, len);
![Page 11: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/11.jpg)
Bridging the Gap Between Bytes and Characters
InputStream in = openInputStream();Reader reader = new InputStreamReader(in, “UTF-8”);
OutputStream out = openOutputStream();Writer writer = new OutputStreamWriter(out, “UTF-8”);
![Page 12: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/12.jpg)
Reading and Writing Typed Data
• Utilities to read primitive data and strings
DataInputStream in = new DataInputStream(openInputStream());
float num = in.readFloat();
DataOutputStream out = new DataOutputStream(openOutputStream());
out.writeFloat(num);
![Page 13: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/13.jpg)
Buffered Vs Unbuffered
• Unbuffered – Each I/O request to the stream is directly handled by the underlying OS.
• Buffered – Each I/O request to the stream is executed on an in-memory buffer. OS invoked only when the buffer is empty.
![Page 14: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/14.jpg)
Buffered I/O
InputStream in = new BufferedInputStream(openInputStream());
OutputStream out = new BufferedOutputStream(openOutputStream());
BufferedReader reader = new BufferedReader(getReader());
BufferedWriter writer = new BufferedWriter(getWriter());
![Page 15: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/15.jpg)
Problems with Streams
• Old school• Slow• Blocking
http://www.flickriver.com/photos/shazral/3565328977/
![Page 16: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/16.jpg)
Alternatives?
• The Java New I/O (NIO) API– The java.nio package– Introduced in JDK 1.4– Leverages most efficient I/O operations of the
underlying OS– Support for multiplexed, non-blocking I/O
![Page 17: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/17.jpg)
Buffers
• A contiguous block of memory used to read and write bytes
• Memory can be allocated in a manner so that the underlying OS can directly access it (Direct buffers)
![Page 18: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/18.jpg)
Reading and Writing with Buffers
byte[] src = getData();ByteBuffer buf = ByteBuffer.allocateDirect(1024);
buf.put(src);
byte[] dest = new byte[1024];buf.flip();buf.get(dest);
![Page 19: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/19.jpg)
Channels
• Represents an open connection to an I/O device
• Facilitates efficient bulk data transfers between devices and buffers
• A channel instance can be obtained from a class of the standard I/O package
![Page 20: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/20.jpg)
Reading from a ChannelFileInputStream fin = new
FileInputStream(“config.xml”);FileChannel channel = fin.getChannel();ByteBuffer buffer =
ByteBuffer.allocateDirect(1024);while (true) {
buffer.clear();int len = channel.read(buffer);if (len == -1) {
break;}buffer.flip();...
}
![Page 21: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/21.jpg)
Direct Channel Transfers
FileChannel in = new FileInputStream(source).getChannel();
FileChannel out = new FileOutputStream(target).getChannel();
in.transferTo(0, in.size(), out);
![Page 22: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/22.jpg)
Memory Mapped Files
FileInputStream input = new FileInputStream(filename);
FileChannel channel = input.getChannel();int fileLength = (int)channel.size();MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,0, fileLength);
![Page 23: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/23.jpg)
Multiplexed I/O with Selectors
• A mechanism to register for I/O events on multiple channels and wait until at least one event occurs
http://tutorials.jenkov.com/java-nio/selectors.html
![Page 24: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/24.jpg)
Registering for I/O Events
Selector selector = Selector.open();channel1.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
channel2.register(selector, SelectionKey.OP_CONNECT);
channel3.register(selector, SelectionKey.OP_WRITE);
![Page 25: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/25.jpg)
‘Selecting’ a Channel
while (selector.select(500) > 0) {Set readyKeys = selector.selectedKeys();
…}
![Page 26: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/26.jpg)
Non-blocking I/O
• Channels generally block until the I/O operations are complete (similar to streams)
• But some channel implementations can be made to work in a non-blocking manner
SocketChannel ch = SocketChannel.open();ch.configureBlocking(false);
![Page 27: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/27.jpg)
Standard I/O Vs. NIO
• A small demonstration…
![Page 28: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/28.jpg)
NIO 2.0
• Introduced in JDK 1.7• Even better support for file manipulation– Path, FileAttribute– Tree walk, File watch
• Asynchronous I/O
![Page 29: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/29.jpg)
Hiranya’s Laws for Proper I/O Handling
By a Developer for the Developers
![Page 30: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/30.jpg)
Clean up your messAlways close your streams and channels when you are done with
them
![Page 31: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/31.jpg)
Buffering can be both your friend and enemyDon’t blindly wrap all streams with their buffered counter parts. Buffering can
yield good performance if you’re doing a lot of small I/O operations. Otherwise it only contributes to increased memory consumption.
![Page 32: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/32.jpg)
Don’t forget to flush after you!If you’re using any buffered streams, make sure to flush them out before closing the streams. Failing to do so may result in data loss.
![Page 33: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/33.jpg)
There’s no escape from Karma and IOExceptionDevelopers often tend to ignore certain I/O exceptions thinking that they will never
be thrown. But be aware that all I/O operations are prone to errors and may throw an exception at any time.
![Page 34: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/34.jpg)
Java is platform independent – I/O is notYou need to do some additional work to make your I/O handling code truly platform
independent. Use the java.io.File abstraction instead of strings to deal with files. Don’t make any assumptions about how the underlying OS handles certain I/O operations.
![Page 35: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/35.jpg)
Concurrency and I/O Don’t Go TogetherIn a multi-user or multi-threaded environment, make sure that individual I/O
operations are properly isolated. Attempting to access the same file or channel with concurrent threads can lead to disasters.
![Page 36: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/36.jpg)
Nothing is perfect – Not even NIONIO is generally faster, but might not yield a significant performance gain in
some scenarios. NIO code has a tendency to become complex and difficult to understand. Also note that NIO is still an evolving technology.
![Page 37: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/37.jpg)
Best way to write good I/O code is to not write them
Use existing utilities and libraries that specialize in I/O handling.
![Page 38: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/38.jpg)
Apache Commons IO
• A quick peek at the API and a demo
![Page 39: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/39.jpg)
Questions?
![Page 40: Handling I/O in Java](https://reader035.vdocuments.us/reader035/viewer/2022062418/555c255ad8b42a09438b4c17/html5/thumbnails/40.jpg)
Thank You