-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNIOExample.java
More file actions
116 lines (97 loc) · 3.71 KB
/
Copy pathNIOExample.java
File metadata and controls
116 lines (97 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package java1_4.nio;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.io.IOException;
/**
* Java 1.4 NIO (New Input/Output) Example Demonstrates non-blocking I/O and buffers
*/
public class NIOExample
{
public static void main(String[] args)
{
System.out.println("=== Java 1.4 NIO (New Input/Output) ===\n");
System.out.println("1. Buffer Operations");
System.out.println("-------------------");
ByteBuffer buffer = ByteBuffer.allocate(1024);
System.out.println("Buffer capacity: " + buffer.capacity());
System.out.println("Buffer position: " + buffer.position());
System.out.println("Buffer limit: " + buffer.limit());
// Write to buffer
buffer.put("Hello NIO".getBytes());
System.out.println("After put - position: " + buffer.position());
// Flip for reading
buffer.flip();
System.out.println("After flip - position: " + buffer.position() + ", limit: " + buffer.limit());
// Read from buffer
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
System.out.println("Read data: " + new String(data));
System.out.println("\n2. FileChannel Example");
System.out.println("---------------------");
System.out.println("""
try {
// Write to file
FileChannel writeChannel = FileChannel.open(
Paths.get("output.txt"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE
);
ByteBuffer writeBuffer = ByteBuffer.wrap("Hello NIO".getBytes());
writeChannel.write(writeBuffer);
writeChannel.close();
// Read from file
FileChannel readChannel = FileChannel.open(
Paths.get("output.txt"),
StandardOpenOption.READ
);
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
readChannel.read(readBuffer);
readBuffer.flip();
byte[] data = new byte[readBuffer.remaining()];
readBuffer.get(data);
System.out.println(new String(data));
readChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
""");
System.out.println("\n3. Selector for Non-blocking I/O");
System.out.println("-------------------------------");
System.out.println("""
Selector selector = Selector.open();
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.bind(new InetSocketAddress(8080));
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
for (SelectionKey key : keys) {
if (key.isAcceptable()) {
// Accept connection
} else if (key.isReadable()) {
// Read data
}
}
}
""");
System.out.println("\nKey Features:");
System.out.println("- Non-blocking I/O");
System.out.println("- Buffers for efficient I/O");
System.out.println("- Channels for I/O operations");
System.out.println("- Selectors for multiplexing");
System.out.println("- Better performance than traditional I/O");
System.out.println("\nNIO Components:");
System.out.println("- Buffer: Container for data");
System.out.println("- Channel: Connection to I/O source");
System.out.println("- Selector: Multiplexer for channels");
System.out.println("- SelectionKey: Represents channel registration");
System.out.println("\nBenefits:");
System.out.println("- Better performance");
System.out.println("- Non-blocking operations");
System.out.println("- Scalable I/O");
System.out.println("- Efficient memory usage");
}
}