forked from anirudhagaikwad/JavaFullStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNIOExample1.java
More file actions
198 lines (170 loc) · 7.68 KB
/
Copy pathNIOExample1.java
File metadata and controls
198 lines (170 loc) · 7.68 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package examples;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.nio.charset.StandardCharsets;
public class NIOExample1 {
public static void main(String[] args) {
try {
NIOExamples.extractPalindromes("input.txt", "palindromes.txt");
NIOExamples.maskPassword();
NIOExamples.interactiveChat();
NIOExamples.logUserInput("user_log.txt");
NIOExamples.multiThreadedChat();
NIOExamples.realTimeDataStreaming("data_log.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
class NIOExamples {
/**
* 50) Extract Palindromes from a File (NIO-based version)
* Reads a file using FileChannel and ByteBuffer, extracts all palindrome words,
* and writes them to another file using FileChannel and ByteBuffer.
*/
public static void extractPalindromes(String inputFile, String outputFile) throws IOException {
try (FileChannel inChannel = FileChannel.open(Paths.get(inputFile), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get(outputFile), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
StringBuilder content = new StringBuilder();
while (inChannel.read(buffer) > 0) {
buffer.flip();
content.append(StandardCharsets.UTF_8.decode(buffer));
buffer.clear();
}
List<String> palindromes = Arrays.stream(content.toString().split("\\s+"))
.filter(word -> word.length() > 1 && word.equalsIgnoreCase(new StringBuilder(word).reverse().toString()))
.collect(Collectors.toList());
ByteBuffer outBuffer = ByteBuffer.wrap(String.join("\n", palindromes).getBytes(StandardCharsets.UTF_8));
outChannel.write(outBuffer);
}
}
/**
* 51) Password Masking on Console (NIO-based version)
* Reads a password from System.in via Channel-based NIO input stream,
* masks input, and securely stores it.
*/
public static void maskPassword() throws IOException {
System.out.print("Enter Password: ");
ReadableByteChannel inChannel = Channels.newChannel(System.in);
ByteBuffer buffer = ByteBuffer.allocate(50);
inChannel.read(buffer);
buffer.flip();
char[] passwordChars = new char[buffer.remaining()];
for (int i = 0; i < passwordChars.length; i++) {
passwordChars[i] = (char) buffer.get();
System.out.print("*");
}
System.out.println("\nPassword securely stored.");
}
/**
* 52) Interactive Command-line Chat (NIO-based version)
* Simulates a chat where user inputs via non-blocking NIO and receives responses.
*/
public static void interactiveChat() throws IOException {
ReadableByteChannel inChannel = Channels.newChannel(System.in);
ByteBuffer buffer = ByteBuffer.allocate(256);
System.out.println("Start chatting (type 'exit' to quit):");
while (true) {
buffer.clear();
inChannel.read(buffer);
buffer.flip();
String input = StandardCharsets.UTF_8.decode(buffer).toString().trim();
if ("exit".equalsIgnoreCase(input)) break;
System.out.println("Bot: " + input.toUpperCase()); // Simple response logic
}
}
/**
* 53) Logging User Input to a File (NIO-based version)
* Captures user input using NIO Channels and logs them with timestamps.
*/
public static void logUserInput(String logFile) throws IOException {
FileChannel outChannel = FileChannel.open(Paths.get(logFile), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
ReadableByteChannel inChannel = Channels.newChannel(System.in);
ByteBuffer buffer = ByteBuffer.allocate(256);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("Start typing (type 'exit' to quit):");
while (true) {
buffer.clear();
inChannel.read(buffer);
buffer.flip();
String input = StandardCharsets.UTF_8.decode(buffer).toString().trim();
if ("exit".equalsIgnoreCase(input)) break;
String logEntry = dtf.format(LocalDateTime.now()) + " - " + input + "\n";
ByteBuffer logBuffer = ByteBuffer.wrap(logEntry.getBytes(StandardCharsets.UTF_8));
outChannel.write(logBuffer);
}
}
/**
* 54) Multi-Threaded Console Chat (NIO-based version)
* Uses two threads: one for reading user input and another for responding asynchronously.
*/
public static void multiThreadedChat() {
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> {
try {
ReadableByteChannel inChannel = Channels.newChannel(System.in);
ByteBuffer buffer = ByteBuffer.allocate(256);
while (true) {
buffer.clear();
inChannel.read(buffer);
buffer.flip();
String input = StandardCharsets.UTF_8.decode(buffer).toString().trim();
if ("exit".equalsIgnoreCase(input)) break;
System.out.println("You: " + input);
}
} catch (IOException e) {
e.printStackTrace();
}
});
executor.submit(() -> {
while (true) {
try {
Thread.sleep(3000);
System.out.println("Chatbot: I'm here to chat!");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
});
executor.shutdown();
}
/**
* 55) Real-time Data Streaming (NIO-based version)
* Reads numeric inputs, computes a moving average, and writes results to a file.
*/
public static void realTimeDataStreaming(String outputFile) throws IOException {
FileChannel outChannel = FileChannel.open(Paths.get(outputFile), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
ReadableByteChannel inChannel = Channels.newChannel(System.in);
ByteBuffer buffer = ByteBuffer.allocate(256);
List<Double> numbers = new ArrayList<>();
System.out.println("Enter numbers (type 'exit' to stop):");
while (true) {
buffer.clear();
inChannel.read(buffer);
buffer.flip();
String input = StandardCharsets.UTF_8.decode(buffer).toString().trim();
if ("exit".equalsIgnoreCase(input)) break;
try {
double num = Double.parseDouble(input);
numbers.add(num);
if (numbers.size() > 5) numbers.remove(0); // Keep last 5 entries for moving average
double avg = numbers.stream().mapToDouble(Double::doubleValue).average().orElse(0);
String logEntry = "Number: " + num + ", Moving Average: " + avg + "\n";
ByteBuffer logBuffer = ByteBuffer.wrap(logEntry.getBytes(StandardCharsets.UTF_8));
outChannel.write(logBuffer);
System.out.println("Current Moving Average: " + avg);
} catch (NumberFormatException e) {
System.out.println("Invalid number, try again.");
}
}
}
}