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 वर्गाच्या पद्धती

पद्धत कृती
पूर्ण वाचा() या इनपुट प्रवाहातील डेटाचा पुढील बाइट वाचतो.
इंट रीड (बाइट बी[], इंट ऑफ, इंट लेन) इनपुट प्रवाहातील अनेक बाइट्स वाचते आणि त्यांना बफर अॅरे b मध्ये संग्रहित करते .
off हे लक्ष्य अॅरे b मध्ये ऑफसेट आहे .
len वाचण्यासाठी बाइट्सची कमाल संख्या आहे.
लांब वगळा (लांब n) या इनपुट प्रवाहातून इनपुटचे n बाइट्स वगळते. वगळलेल्या बाइट्सची संख्या परत करते (आम्ही इनपुट प्रवाहाच्या शेवटी पोहोचलो तर ते n पेक्षा कमी असू शकते).
इंट उपलब्ध() या इनपुट प्रवाहातून वाचल्या जाऊ शकणार्‍या (किंवा वगळल्या जाणार्‍या) उर्वरित बाइट्सची संख्या मिळवते.
निरर्थक रीसेट() चिन्हांकित स्थितीत बफर रीसेट करते. चिन्हांकित स्थान 0 आहे जोपर्यंत दुसरी स्थिती चिन्हांकित केली जात नाही किंवा कन्स्ट्रक्टरमध्ये भिन्न ऑफसेट निर्दिष्ट केला जात नाही.
बुलियन मार्क सपोर्टेड() हे इनपुटस्ट्रीम मार्किंग/रीसेटिंगला सपोर्ट करते की नाही ते तपासते. ByteArrayInputStream साठी खरे मिळवते .
शून्य बंद () काहीही करत नाही.
शून्य खूण (int readAheadLimit) सेट करतेचिन्हवर्तमान स्थितीच्या समान फील्ड. रीसेट पद्धत कॉल केल्यास , त्यानंतरचे वाचन त्या स्थितीपासून सुरू होईल. readAheadLimit पॅरामीटर वापरले जात नाही आणि ते पद्धतीच्या वर्तनावर परिणाम करत नाही .

चला या पद्धतींचा जवळून विचार करूया आणि ते सराव मध्ये कसे कार्य करतात ते पाहू या.

वाचा()

जेव्हा तुम्हाला सामान्य इनपुटस्ट्रीम प्रमाणेच ByteArrayInputStream मधून बाइट्स वाचायचे असतील , तेव्हा तुम्ही read() पद्धत वापरू शकता .

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)

तुम्ही skip() पद्धतीचा वापर करून ठराविक बाइट्स वगळू शकता आणि ते वाचू नका.

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();
   }
}

आउटपुट:

३, ४,

रीसेट()

ही पद्धत बफर केलेल्या प्रवाहाची स्थिती शेवटच्या चिन्हांकित स्थितीवर रीसेट करते. भिन्न चिन्ह सेट केल्याशिवाय ते स्थान 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();
   }
}

आपण पाहू की reset() पद्धत कॉल केल्याने आपण आपल्या प्रवाहाच्या प्रारंभ बिंदूवर पोहोचतो.

आउटपुट:

वाचा: 65
वाचा: 66
वाचा: 67
वाचा: 68
कॉलिंग रीसेट() पद्धत
वाचा: 65
वाचा: 66

चिन्ह (int readAheadLimit)

ByteArrayInputStream क्लासची mark () पद्धत सध्याच्या बाइट स्थानावर अंतर्गत चिन्ह सेट करते, म्हणजेच पूर्वी वाचलेल्या बाइटच्या लगेच नंतर. ही पद्धत एक पॅरामीटर घेते जे सूचित करते की प्रवाह अवैध होण्यापूर्वी चिन्हानंतर किती बाइट वाचले जाऊ शकतात. डीफॉल्टनुसार, खूण स्पष्टपणे सेट न केल्यास, ByteArrayInputStream स्थिती 0 किंवा ऑफसेटची स्थिती त्याच्या कन्स्ट्रक्टरला दिली जाते. हे लक्षात घेणे महत्त्वाचे आहे की readAheadLimit चिन्ह या वर्गासाठी अप्रासंगिक आहे.

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

ByteArrayInputStream मध्ये मार्क() आणि reset() पद्धती वापरून मार्क सेट करण्याचे येथे उदाहरण आहे . आम्ही मागील उदाहरणामध्ये मार्क() पद्धतीवर कॉल जोडू :

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
Read: 65
Read: 66
Read: 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 ची आवश्यकता असते. सामान्यतः हा वर्ग इतर कोडच्या संयोजनात वापरणे अर्थपूर्ण आहे ज्यांना स्वतःऐवजी इनपुटस्ट्रीमसह कसे कार्य करावे हे माहित आहे.