Java NIO (New I/O)
Table of Contents
Core NIO Components #
Buffers #
- Buffer: The base class for all buffers with position, limit, and capacity management
- ByteBuffer: Byte buffer implementation with direct and heap variants
- CharBuffer: Character buffer for text processing
- IntBuffer: Integer buffer implementation
- LongBuffer: Long buffer implementation
- FloatBuffer: Float buffer implementation
- DoubleBuffer: Double buffer implementation
- ShortBuffer: Short buffer implementation
Channels #
- Channel Interface: The core I/O operation interface
- FileChannel: File-based channel for random access
- SocketChannel: TCP socket channel implementation
- ServerSocketChannel: TCP server socket channel
- DatagramChannel: UDP datagram channel
- Pipe.SinkChannel: Pipe sink channel implementation
- Pipe.SourceChannel: Pipe source channel implementation
Selectors and Multiplexing #
- Selector: I/O multiplexing with select, poll, and epoll
- SelectionKey: Channel registration and event tracking
- SelectableChannel: Channel that can be multiplexed
Charsets and Encoding #
- Charset: Character encoding and decoding
- CharsetEncoder: Character to byte encoding
- CharsetDecoder: Byte to character decoding
- StandardCharsets: Predefined standard charsets
File I/O (NIO.2) #
- Path Interface: Platform-independent file path representation
- Paths Class: Factory methods for creating Path objects
- Files Class: File system operations (copy, move, delete, etc.)
- FileSystem: File system provider interface
- FileSystems: Factory for file system instances
- FileStore: File store (partition/drive) representation
File Attributes #
- BasicFileAttributes: Basic file attribute view
- DosFileAttributes: DOS/Windows specific attributes
- PosixFileAttributes: POSIX file attributes
- FileAttributeView: File attribute view interface
- FileOwnerAttributeView: File ownership attributes
- PosixFilePermission: POSIX file permissions
Asynchronous I/O #
- AsynchronousFileChannel: Async file I/O operations
- AsynchronousSocketChannel: Async TCP socket operations
- AsynchronousServerSocketChannel: Async TCP server socket
- CompletionHandler: Async operation completion callback
Memory-Mapped Files #
- MappedByteBuffer: Memory-mapped file buffer
- FileChannel.MapMode: Memory mapping modes
Utilities and Supporting Classes #
- ByteOrder: Endianness specification
- BufferOverflowException: Buffer overflow handling
- BufferUnderflowException: Buffer underflow handling
- InvalidMarkException: Invalid buffer mark handling
NIO Architecture Overview #
Application
↓
Buffers (ByteBuffer, CharBuffer, etc.)
↓
Channels (FileChannel, SocketChannel, etc.)
↓
Selectors (I/O Multiplexing)
↓
Operating System (epoll, kqueue, IOCP)
Performance Characteristics #
| Component | Read | Write | Random Access | Memory Mapping |
|---|---|---|---|---|
| FileChannel | O(1) | O(1) | O(1) | Yes |
| SocketChannel | O(1) | O(1) | N/A | No |
| MappedByteBuffer | O(1) | O(1) | O(1) | Yes |
| HeapByteBuffer | O(1) | O(1) | O(1) | No |
| DirectByteBuffer | O(1) | O(1) | O(1) | Yes |
Key Features #
- Non-blocking I/O: Enable high-performance, scalable network applications
- Buffer-oriented: All I/O operations work with buffers for efficiency
- Selector-based multiplexing: Single thread can handle thousands of connections
- Memory mapping: Direct memory access to files for zero-copy I/O
- Character set support: Comprehensive encoding/decoding framework
- File system abstraction: Cross-platform file system operations
When to Use NIO #
Use NIO when:
- Building high-performance network servers
- Handling thousands of simultaneous connections
- Need non-blocking I/O operations
- Working with large files and memory mapping
- Requiring cross-platform file system operations
Use traditional I/O when:
- Simple file operations on small files
- Sequential processing of text files
- Blocking I/O is acceptable for your use case
- Working with character streams (Reader/Writer)
NIO vs Traditional I/O #
| Feature | Traditional I/O | NIO |
|---|---|---|
| Blocking | Yes | Optional |
| Buffering | Stream-based | Buffer-based |
| Performance | Good | Excellent for many connections |
| Scalability | Limited | High (thousands of connections) |
| API Complexity | Simple | More complex |
| Memory Mapping | No | Yes |
| Selector Support | No | Yes |
| Character Encoding | Limited | Comprehensive |