NIO Buffers, Channels, Selectors Java In Hindi
जावा प्रोग्रामिंग लैंग्वेज में, NIO (New I/O) न्यू इनपुट आउटपुट मेथड को ट्रेडिशनल Java I/O इनपुट आउटपुट मेथड के कम्पयेर में इनपुट/आउटपुट डाटा प्रोग्राम ऑपरेशन को हैंडल या मैनेज करने में ज़्यादा एफिशिएंट और फ्लेक्सिबल कांसेप्ट मेथड प्रोवाइड करने के लिए इंट्रोड्यूस किया गया था।

There are three essential concepts in Java NIO (New I/O).
- Buffers — यह जावा प्रोग्राम के डेटा को टेम्पररी आर्डर में होल्ड करते हैं।
- Channels — यह जावा प्रोग्राम के डेटा को रीड/राइट करने के लिए एक कनेक्शन मेथड प्रोवाइड करते हैं।
- Selectors — यह जावा प्रोग्राम में एक थ्रेड को कई चैनल्स को मॉनिटर करने में हेल्प करते हैं।
Buffers Concept in Java.
जावा प्रोग्राम में बफ़र एक कंटेनर वैल्यू कंटेनर हॉल्डर या मेथड है, जिसका यूज़ किसी चैनल से रीड या राइट किए जा रहे प्रोग्राम डेटा को होल्ड करने में किया जाता है।
Common buffer types used in Java.
- ByteBuffer method.
- CharBuffer method.
- IntBuffer method.
- LongBuffer method.
- FloatBuffer method.
- DoubleBuffer method.
- ShortBuffer method.
जावा प्रोग्रामिंग लैंग्वेज में सबसे अधिक यूज़ होने वाला ByteBuffer मेथड है।
Creating a Buffer in Java.
ByteBuffer buffer = ByteBuffer.allocate(1024);
यह मौजूदा जावा प्रोगाम में 1024 बाइट्स की कैपेसिटी वाला एक नया बफ़र क्रिएट करता है।
Essential properties of a Java buffer.
जावा प्रोग्राम में यूजर क्रिएटेड एक बफ़र में मुख्य रूप से ये कॉमन एलिमेंट होती हैं.
- Buffer capacity.
- Buffer position.
- Buffer limit.
Buffer capacity.
एक यूजर डिफाइन कस्टम प्रोग्राम बफ़र में ज़्यादा से ज़्यादा कितने एलिमेंट वैल्यू या बाइट स्टोर हो सकते हैं।
ByteBuffer buffer = ByteBuffer.allocate(17);
Here in this description.
इसमें बफर की कैपेसिटी = 17 डिफाइन होती है
Buffer position.
यह मौजूदा जावा प्रोग्राम में उस यूजर डिफाइन बफर की स्टोरेज लोकेशन को इंडीकेट करता है, जहाँ बफर प्रोग्राम में डेटा रीड या राइट किया जाएगा।
Buffer limit.
यह मौजूदा जावा प्रोग्राम में यूजर डिफाइन बफर की उस लिमिट को इंडीकेट करता है, जहाँ तक बफर प्रोग्राम में डेटा रीड या राइट किया जा सकता है।
put() Buffer method in Java.
इसका यूज़ मौजूदा जावा प्रोग्राम में बफ़र डेटा क्रिएट या राइट करने में होता है।
ByteBuffer buffer = ByteBuffer.allocate(17);
buffer.put((byte) 17);
buffer.put((byte) 23);
buffer.put((byte) 33);
ये स्टेटमेंट लिखने के बाद, बफर की पोज़िशन आगे बढ़ जाती है।
Flip() concept in buffer.
यह मेथड जावा प्रोग्राम में फ्लिप() फंक्शन क्रिएट करने के बाद बफ़र को पढ़ने के लिए तैयार करता है।
buffer.flip();
इस कॉन्सेप्ट के हिसाब से.
Writing to a Java buffer.
बफर की पोजीशन → आगे बढ़ता है
flip()
↓
Reading the buffer value.
- Buffer position → इसमें बफर शुरू से शुरू होता है
- Buffer limit → इसमें लिखे गए डेटा का अमाउंट बन जाता है
get() concept in buffer.
जावा प्रोग्राम में get() फंक्शन मेथड का यूज़ बफर से डेटा इन्फो को रीड करने में होता है।
byte data = buffer.get();
System.out.println(data);
clear() concept in buffer.
जावा प्रोग्राम में clear() फंक्शन मेथड एक नया डेटा लिखने के लिए एक बफर तैयार करता है।
buffer.clear();
इसके साथ ही clear() फंक्शन दूसरे राइटिंग साइकिल के लिए पोजीशन और लिमिट को रीसेट करता है।
Example of the buffer method in Java.
ByteBuffer buffer =
import java.nio.ByteBuffer;
public class ByteBufferTesting {
public static void main(String[] args) {
// here we Allocate a ByteBuffer with capacity 17 bytes in java program
ByteBuffer buffer = ByteBuffer.allocate(17);
// here we Put new values into the buffer
buffer.put((byte) 17);
buffer.put((byte) 23);
buffer.put((byte) 33);
buffer.put((byte) 43);
// here we use Flip the buffer to switch from writing mode to reading mode
buffer.flip();
// here we Read and print values from the buffer element
System.out.println(“The Buffer elements is -:”);
while (buffer.hasRemaining()) {
System.out.println(buffer.get());
}
}
}
Its output.
The Buffer elements is -:
17
23
33
43
Common Java Used Popular Buffer Methods
| # | Buffer Methods | Purpose of Each Buffer Methods |
| 1. | allocate() function | allocate() function used to Creates a buffer element |
| 2. | put()function | put()function used to Writes buffer data |
| 3. | get()function | get()function used to Reads data or buffer element |
| 4. | flip()function | flip()function used to Switches writing mode to reading mode file data |
| 5. | clear()function | clear()function used to Prepares buffer for new writing element |
| 6. | rewind()function | rewind()function method used to Moves position back to beginning |
| 7. | hasRemaining()function | hasRemaining()function used to Checks whether data remains or not |
Channel Concept in Java.
जावा बफर प्रोग्राम में चैनल एक कनेक्शन को रिप्रेजेंट करता है, चैनल कनेक्शन के माध्यम से डेटा को रीड या राइट किया जा सकता है।
जावा में ट्रेडिशनल स्ट्रीम मेथड के अपोजिट, चैनल टाइप के आधार पर चैनल से डाटा रीड और राइट दोनों ऑपरेशन को सपोर्ट कर सकते हैं।
Common channels in Java include.
- FileChannel.
- SocketChannel.
- ServerSocketChannel.
- DatagramChannel.
- FileChannel Concept in Java.
जावा प्रोग्राम में FileChannel मेथड कांसेप्ट का यूज़ जावा डाटा फ़ाइलों को रीड और राइट करने में किया जाता है।
Example of the FileChannel Concept.
FileChannel channel =
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class FileChannelIllustatiion {
public static void main(String[] args) {
try {
// here we Open the file in READ mode data operation
FileChannel channel = FileChannel.open(
Paths.get(“samplefile.txt”),
StandardOpenOption.READ
);
// here we Allocate a ByteBuffer of 1024 bytes disk space allocation
ByteBuffer buffer = ByteBuffer.allocate(1024);
// here we Read file data into the buffer mode
channel.read(buffer);
// here we Switch buffer from writing mode to reading mode task
buffer.flip();
// here we Read and display file contents elements
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
// here we Close the channel
channel.close();
} catch (IOException ex) {
System.out.println(“Display Error while reading file data -: ” + ex.getMessage());
}
}
}
A basic Java channel flow concept.
डाटा फ़ाइल
↓
फ़ाइलचैनल मेथड
↓
बाइटबफ़र स्टोरेज एलिमेंट
↓
एप्लिकेशन
Writing using a FileChannel in Java.
FileChannel channel =
String text = “Welcome to Java NIO features”;
ByteBuffer buffer =
ByteBuffer.wrap(text.getBytes());
Example of Writing using a FileChannel in Java.
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class FileChannelWriteIllustation {
public static void main(String[] args) {
try {
// here we create data to write into the file
String info = “Welcome to Java NIO features. \n Welcome to FileChannel concept.”;
// here we Convert string into bytes and store in ByteBuffer method
ByteBuffer buffer = ByteBuffer.wrap(info.getBytes());
// here we Open file in CREATE and WRITE mode file operation
FileChannel channel = FileChannel.open(
Paths.get(“samplefile.txt”),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE
);
// here we Write buffer data into the file
while (buffer.hasRemaining()) {
channel.write(buffer);
}
// here we Close the channel
channel.close();
System.out.println(“Sample file Data written successfully”);
} catch (IOException ex) {
System.out.println(“Display Error writing into the file -: ” + ex.getMessage());
}
}
}
Socket channel concept in Java.
जावा में सॉकेट चैनल NIO नेटवर्क कम्युनिकेशन के लिए भी चैनल प्रोवाइड करता है।
ServerSocketChannel
जावा में सर्वर सॉकेट चैनल क्रिएट करने के लिए सर्वर सॉकेट चैनल मेथड का यूज़ होता है।
ServerSocketChannel server =
ServerSocketChannel.open();
server.bind(
new InetSocketAddress(8080)
);
SocketChannel.
जावा प्रोग्राम में क्लाइंट और सर्वर के बीच कम्युनिकेशन के लिए सॉकेट चैनल मेथड यूज़ होता है।
SocketChannel client =
SocketChannel.open(
new InetSocketAddress(“localhost”, 8080)
);
यह जावा में चैनल ब्लॉकिंग या नॉन-ब्लॉकिंग मोड में परफॉर्म कर सकते हैं।
For example.
server.configureBlocking(false);
यह जावा प्रोग्राम में नॉन-ब्लॉकिंग चैनल सिलेक्टर के साथ स्पेशल आर्डर में यूज़फुल होते हैं।
Selector Methods in Java.
जावा प्रोग्राम में एक सिलेक्टर एक सिंगल थ्रेड को कई चैनल मॉनिटर करने में हैल्प करता है।
यह स्पेशल रूप से नेटवर्क एप्लिकेशन के लिए यूज़फुल होते है। बिना सिलेक्टर मेथड के:
- प्रोग्राम थ्रेड 1 → चैनल 1 मेथड
- प्रोग्राम थ्रेड 2 → चैनल 2 मेथड
- प्रोग्राम थ्रेड 3 → चैनल 3 मेथड
- प्रोग्राम थ्रेड 4 → चैनल 4 मेथड
Channel method with a selector.
┌→ चैनल 1 मेथड
│
सिंगल प्रोग्राम थ्रेड → सिलेक्टर
│
├→ चैनल 2 मेथड
│
└→ चैनल 3 मेथड
यहाँ हमें हर कनेक्शन के लिए एक थ्रेड की ज़रूरत कम हो सकती है।
Creating a selector in Java.
Selector selector =
selector.open(); function.
इसमें थ्रेड चैनल को सिलेक्टर के साथ रजिस्टर करने से पहले उसे नॉन-ब्लॉकिंग के रूप में कॉन्फ़िगर करना होगा।
channel.configureBlocking(false);
इसके बाद इसे रजिस्टर करें।
channel.register(
selector,
SelectionKey.OP_READ
);
Selector Selection Operations in java.
जावा प्रोग्रामिंग लैंग्वेज में सबसे अधिक यूज़ होने वाले कुछ पॉपुलर सिलेक्शन ऑपरेशन हैं.
| # | Selector Operation | Meaning of Selector Operations |
| 1. | OP_READ operation | OP_READ operation used to check outchannel is ready for reading or not |
| 2. | OP_WRITE operation | OP_WRITE operation used to check out channel is ready for writing |
| 3. | OP_CONNECT operation | OP_CONNECT operation used to check out connection can be established or not |
| 4. | OP_ACCEPT operation | OP_ACCEPT operation used to check out the server can accept a connection or not |
Example of a selection operation.
channel.register(
selector,
SelectionKey.OP_READ
);
Using Selector Methods in Java.
जावा प्रोग्रामिंग लैंग्वेज में, एक आम सिलेक्टर लूप कुछ ऐसा रिप्रेजेंट होता है,
while (true) {
selector.select();
Set<SelectionKey> keys =
selector.selectedKeys();
for (SelectionKey key : keys) {
if (key.isReadable()) {
// here it Read data
}
if (key.isWritable()) {
// here it Write data
}
}
keys.clear();
}
इस प्रोसेस में सिलेक्टर तब तक वेट करता है, जब तक एक या ज़्यादा रजिस्टर्ड चैनल ऑपरेशन के लिए तैयार न हो जाएं।
Relationship between buffer, channel, and selector in Java.
एक बेसिक जावा प्रोग्राम में ये तीनों कॉन्सेप्ट एक साथ काम करते हैं.
सिलेक्टर मेथड
↓
चैनल इसे मॉनिटर करता है
↓
चैनल कांसेप्ट
↓
इसमें डेटा पढ़ता और लिखता है
↓
बफ़र मेमोरी
↓
फाइनल एप्लिकेशन
For example, in a network application.
क्लाइंट रिक्वेस्ट
↓
सॉकेटचैनल मेथड
↓
सिलेक्टर प्रोसेस
↓
पढ़ने लायक?
↓
बाइटबफ़र
↓
प्रोसेस डेटा एलिमेंट
Difference Between Buffer, Channel, and Selector.
| # | Each Feature | Buffer method | Channel method | Selector method |
| 1. | Unique purpose | It Holds data or value element | It Transfers data element | It Monitors all channels |
| 2. | What Used for | It provides Temporary storage hold | Provide readymade I/O communication method | It provides Multiplexing concept |
| 3. | Example with | Used with ByteBuffer method | Used with FileChannel method | Used with Selector method |
| 4. | Implement with | Buffer implement with Data | Chanel implements with Files/network | Selector implement with Multiple channels |
| 5. | Popular methods | Popular buffer method or function put(), get(), flip() | Popular channel method or function read(), write() | Popular selector method or function select(), selectedKeys() |
Conclusion of NIO Buffers, Channels, Selectors.
याद रखने का आसान तरीका
- Buffer Method → बफर में प्रोसेस होने वाला डेटा कहाँ रखा जाता है.
- Channel Method → बफर चैनल में डेटा सोर्स टू डेस्टिनेशन कैसे मूव होता है.
- Selector Method → इसमें कौन सा चैनल बफर ऑपरेशन के लिए तैयार है
In short, Java प्रोग्राम में NIO I/O के लिए चैनल, I/O इनपुट आउटपुट ऑपरेशन के दौरान डेटा होल्ड करने के लिए बफ़र और कम थ्रेड वाले कई नॉन-ब्लॉकिंग चैनल को अच्छे से मॉनिटर करने के लिए सिलेक्टर मेथड का यूज़ करता है।

