Java.io पैकेज में ByteArrayInputStream क्लास का उपयोग इनपुट ऐरे (बाइट्स) को पढ़ने के लिए किया जा सकता है

बाइट सरणी इनपुट स्ट्रीम बनाने के लिए, हमें पहले java.io.ByteArrayInputStream पैकेज आयात करना होगा। पैकेज आयात करने के बाद, हमारे पास इनपुट स्ट्रीम बनाने के लिए दो कंस्ट्रक्टर उपलब्ध हैं:

ByteArrayInputStream input = new ByteArrayInputStream(arr);
ByteArrayInputStream input = new ByteArrayInputStream(arr, 2, 2);

कक्षा के अंदर 4 फ़ील्ड हैं:

// Byte array provided by the creator of the stream
protected byte buf[];

// Index of the next character to read from the input stream's buffer
protected int pos;

// Current marked position in the stream
protected int mark = 0;

// Index is one greater than the last valid character in the input stream's buffer
protected int count;

और यहाँ हमारे निर्माता हैं:

public ByteArrayInputStream(byte buf[]) {
    this.buf = buf;
    this.pos = 0;
    this.count = buf.length;
}

public ByteArrayInputStream(byte buf[], int offset, int length) {
    this.buf = buf;
    this.pos = offset;
    this.count = Math.min(offset + length, buf.length);
    this.mark = offset;
}

ByteArrayInputStream वर्ग के तरीके

तरीका कार्य
int पढ़ा () इस इनपुट स्ट्रीम से डेटा की अगली बाइट पढ़ता है।
इंट रीड (बाइट बी [], इंट ऑफ, इंट लेन) इनपुट स्ट्रीम से कई बाइट पढ़ता है और उन्हें बफर सरणी b में संग्रहीत करता है ।
off लक्ष्य सरणी b में ऑफ़सेट है ।
लेन पढ़ने के लिए बाइट्स की अधिकतम संख्या है।
लंबी छोड़ें (लंबी एन) इस इनपुट स्ट्रीम से इनपुट के एन बाइट्स को छोड़ देता है। छोड़े गए बाइट्स की संख्या लौटाता है (यदि हम इनपुट स्ट्रीम के अंत तक पहुँचते हैं तो यह n से कम हो सकता है)।
int उपलब्ध () इस इनपुट स्ट्रीम से पढ़ी (या छोड़ी गई) शेष बाइट्स की संख्या लौटाता है।
शून्य रीसेट () बफ़र को चिह्नित स्थिति पर रीसेट करता है। चिह्नित स्थिति 0 है जब तक कि किसी अन्य स्थिति को चिह्नित नहीं किया जाता है या कन्स्ट्रक्टर में एक अलग ऑफ़सेट निर्दिष्ट नहीं किया जाता है।
बूलियन मार्क समर्थित () जाँचता है कि क्या यह InputStream मार्किंग/रीसेटिंग का समर्थन करता है। ByteArrayInputStream के लिए सही लौटाता है
शून्य बंद () कुछ नहीं करता।
शून्य चिह्न (int readAheadLimit) सेट करता हैनिशानवर्तमान स्थिति के बराबर क्षेत्र। यदि रीसेट विधि कहा जाता है, तो बाद की रीडिंग उस स्थिति से शुरू होगी। ReadAheadLimit पैरामीटर का उपयोग नहीं किया जाता है और यह विधि के व्यवहार को प्रभावित नहीं करता है

आइए इन तरीकों पर करीब से नज़र डालें और देखें कि ये व्यवहार में कैसे काम करते हैं।

पढ़ना()

जब आप ByteArrayInputStream से बाइट पढ़ना चाहते हैं, जैसे आप एक साधारण InputStream से करते हैं , तो आप रीड () विधि का उपयोग कर सकते हैं।

public static void main(String[] args) {
   byte[] array = {1, 2, 3, 4};

   try (ByteArrayInputStream input = new ByteArrayInputStream(array)) {
       for (int i = 0; i < array.length; i++) {
           int data = input.read();
           System.out.print(data + ", ");
       }
   } catch (IOException e) {
       e.printStackTrace();
   }
}

उपलब्ध()

यदि आप यह जांचना चाहते हैं कि आपके बफर में कुछ है या नहीं, तो आप उपलब्ध () विधि को कॉल कर सकते हैं।

public static void main(String[] args) {
   byte[] array = {1, 2, 3, 4};

   try (ByteArrayInputStream input = new ByteArrayInputStream(array)) {
       System.out.println("Bytes available for reading: " + input.available());

       input.read();
       System.out.println("Bytes available for reading " + input.available());

       input.read();
       System.out.println("Bytes available for reading " + input.available());
   } catch (IOException e) {
       e.printStackTrace();
   }
}

हम देखेंगे कि बफ़र से प्रत्येक पढ़ने के बाद पढ़ने के लिए उपलब्ध बाइट्स की संख्या बदल जाती है।

आउटपुट:

पढ़ने के लिए उपलब्ध बाइट्स: पढ़ने के लिए उपलब्ध 4
बाइट्स:
पढ़ने के लिए उपलब्ध 3 बाइट्स: 2

छोड़ें (लंबा n)

आप बाइट्स की एक निश्चित संख्या को छोड़ने और उन्हें पढ़ने के लिए स्किप () विधि का उपयोग कर सकते हैं।

public static void main(String[] args) {
   byte[] array = {1, 2, 3, 4};

   try (ByteArrayInputStream input = new ByteArrayInputStream(array)) {
       input.skip(2);

       while (input.available() != 0) {
           int data = input.read();
           System.out.print(data + ", ");
       }
   } catch (IOException e) {
       e.printStackTrace();
   }
}

आउटपुट:

3, 4,

रीसेट()

यह विधि बफ़र्ड स्ट्रीम की स्थिति को अंतिम चिह्नित स्थिति पर रीसेट करती है। यह स्थिति 0 है जब तक कि एक अलग चिह्न सेट नहीं किया जाता है।

public static void main(String[] args) {
   byte[] buf = {65, 66, 67, 68, 69};
   try (ByteArrayInputStream input = new ByteArrayInputStream(buf)) {
       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());

       System.out.println("Calling reset() method");
       input.reset();
       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());
   } catch (IOException e) {
       e.printStackTrace();
   }
}

हम देखेंगे कि रीसेट () विधि को कॉल करना हमें हमारी स्ट्रीम के शुरुआती बिंदु पर ले जाता है।

आउटपुट:

पढ़ें: 65
पढ़ें: 66
पढ़ें: 67
पढ़ें: 68
कॉलिंग रीसेट () विधि
पढ़ें: 65
पढ़ें: 66

चिह्न (int readAheadLimit)

ByteArrayInputStream क्लास का मार्क () तरीका मौजूदा बाइट पोजीशन पर इंटरनल मार्क सेट करता है, यानी पहले पढ़े गए बाइट के तुरंत बाद। यह विधि एक पैरामीटर लेती है जो इंगित करती है कि धारा के अमान्य होने से पहले निशान के बाद कितने बाइट पढ़े जा सकते हैं। डिफ़ॉल्ट रूप से, यदि चिह्न स्पष्ट रूप से सेट नहीं किया गया है, तो ByteArrayInputStream स्थिति 0 या ऑफ़सेट की स्थिति को इसके कन्स्ट्रक्टर को पास कर देता है। यह ध्यान रखना महत्वपूर्ण है कि इस वर्ग के लिए रीडअहेडलिमिट मार्क अप्रासंगिक है।

/* Note: For this class, {@code readAheadLimit}
*  has no meaning.
*
* @since   1.1
*/
public void mark(int readAheadLimit) {
   mark = pos;
}

यहाँ एक ByteArrayInputStream में चिह्न () और रीसेट () विधियों का उपयोग करके एक चिह्न सेट करने का एक उदाहरण दिया गया है । हम पिछले उदाहरण में मार्क () विधि में एक कॉल जोड़ेंगे :

public static void main(String[] args) {
   byte[] buf = {65, 66, 67, 68, 69};
   try (ByteArrayInputStream input = new ByteArrayInputStream(buf)) {
       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());
       input.mark(5);

       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());

       System.out.println("Calling reset() method");
       input.reset();

       System.out.println("Read: " + input.read());
       System.out.println("Read: " + input.read());

   } catch (IOException e) {
       e.printStackTrace();
   }
}

हम देख सकते हैं कि वर्तमान धारा की स्थिति बदल गई है।

आउटपुट:

पढ़ें: 65
पढ़ें: 66
पढ़ें: 67
पढ़ें: 68
पढ़ें: 69
कॉलिंग रीसेट () विधि
पढ़ें: 68
पढ़ें: 69

चिह्न समर्थित ()

MarkSupported () विधि आपको यह जांचने देती है कि कोई चिह्न सेट किया जा सकता है या नहीं। यह समझने के लिए कि वापसी मूल्य कहाँ से आता है, आइए विधि के कोड पर जाएँ:

/**
* Tests if this {@code InputStream} supports mark/reset. The
* {@code markSupported} method of {@code ByteArrayInputStream}
* always returns {@code true}.
*
* @since   1.1
*/
public boolean markSupported() {
   return true;
}

विधि हमेशा सत्य लौटाती है । आइए व्यवहार में इसका परीक्षण करें।

public static void main(String[] args) {
   byte[] buf = {65, 66, 67, 68, 69};
   try (ByteArrayInputStream bais = new ByteArrayInputStream(buf)) {
       boolean isMarkSupported = bais.markSupported();

       System.out.println("isMarkSupported: " + isMarkSupported);
       System.out.println("Read: " + bais.read());
       System.out.println("Read: " + bais.read());

       bais.mark(1);
       System.out.println("Read: " + bais.read());
       isMarkSupported = bais.markSupported();
       System.out.println("isMarkSupported: " + isMarkSupported);

       bais.reset();
       isMarkSupported = bais.markSupported();
       System.out.println("isMarkSupported: " + isMarkSupported);
   } catch (IOException e) {
       e.printStackTrace();
   }
}

मार्क () और रीसेट () विधियों को निष्पादित करने के बाद , हमारी धारा हमेशा तैयार रहती है और निशान का समर्थन करती है:

आउटपुट:

isMarkSupported: true
पढ़ें: 65
पढ़ें: 66
पढ़ें: 67
isMarkSupported: true
isMarkSupported: true

बंद करना()

और क्लोज मेथड को समझने के लिए आइए इसके अंदर भी झांकते हैं:

/**
* Closing a {@code ByteArrayInputStream} has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an {@code IOException}.
*/
public void close() throws IOException {
}

क्लोज मेथड के लिए प्रलेखन हमें बताता है कि ByteArrayInputStream को बंद करने का कोई प्रभाव नहीं पड़ता है। IOException फेंके बिना स्ट्रीम बंद होने के बाद ByteArrayInputStream क्लास विधियों को कॉल किया जा सकता है ।

हम क्या निष्कर्ष निकाल सकते हैं?

जब हम बाइट सरणी से डेटा पढ़ना चाहते हैं तो हमें ByteArrayInputStream की आवश्यकता होती है। यह आमतौर पर इस वर्ग को अन्य कोड के संयोजन में उपयोग करने के लिए समझ में आता है जो जानता है कि इनपुटस्ट्रीम के साथ कैसे काम करना है , इसके बजाय।