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 read() ఈ ఇన్‌పుట్ స్ట్రీమ్ నుండి డేటా యొక్క తదుపరి బైట్‌ను చదువుతుంది.
int రీడ్(బైట్ b[], int off, int len) ఇన్‌పుట్ స్ట్రీమ్ నుండి అనేక బైట్‌లను చదువుతుంది మరియు వాటిని బఫర్ అర్రే బిలో నిల్వ చేస్తుంది .
ఆఫ్ అనేది లక్ష్య శ్రేణికి ఆఫ్‌సెట్ b .
len అనేది చదవాల్సిన బైట్‌ల గరిష్ట సంఖ్య.
లాంగ్ స్కిప్ (లాంగ్ n) ఈ ఇన్‌పుట్ స్ట్రీమ్ నుండి ఇన్‌పుట్ యొక్క n బైట్‌లను దాటవేస్తుంది. దాటవేయబడిన బైట్‌ల సంఖ్యను చూపుతుంది (మనం ఇన్‌పుట్ స్ట్రీమ్ ముగింపుకు చేరుకున్నట్లయితే అది n కంటే తక్కువగా ఉండవచ్చు).
Int అందుబాటులో () ఈ ఇన్‌పుట్ స్ట్రీమ్ నుండి చదవగలిగే (లేదా దాటవేయబడిన) మిగిలిన బైట్‌ల సంఖ్యను అందిస్తుంది.
శూన్య రీసెట్ () గుర్తించబడిన స్థానానికి బఫర్‌ని రీసెట్ చేస్తుంది. కన్స్ట్రక్టర్‌లో మరొక స్థానం గుర్తించబడితే లేదా వేరే ఆఫ్‌సెట్ పేర్కొనబడితే తప్ప గుర్తించబడిన స్థానం 0 అవుతుంది.
బూలియన్ మార్క్ సపోర్టెడ్() ఇన్‌పుట్‌స్ట్రీమ్ మార్కింగ్/రీసెట్ చేయడానికి మద్దతిస్తుందో లేదో తనిఖీ చేస్తుంది. ByteArrayInputStream కోసం నిజమని చూపుతుంది .
శూన్యం దగ్గరగా () ఏమీ చేయదు.
శూన్యం గుర్తు (int readAheadLimit) సెట్ చేస్తుందిగుర్తుఫీల్డ్ ప్రస్తుత స్థానానికి సమానం. రీసెట్ పద్ధతిని పిలిచినట్లయితే, ఆ స్థానం నుండి తదుపరి పఠనం ప్రారంభమవుతుంది. readAheadLimit పరామితి ఉపయోగించబడదు మరియు పద్ధతి యొక్క ప్రవర్తనను ప్రభావితం చేయదు .

ఈ పద్ధతులను నిశితంగా పరిశీలిద్దాం మరియు అవి ఆచరణలో ఎలా పనిచేస్తాయో చూద్దాం.

చదవండి()

మీరు ఒక సాధారణ ఇన్‌పుట్ స్ట్రీమ్ నుండి బైట్‌అర్రేఇన్‌పుట్ స్ట్రీమ్ నుండి బైట్‌లను చదవాలనుకున్నప్పుడు , మీరు రీడ్() పద్ధతిని ఉపయోగించవచ్చు .


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

అవుట్‌పుట్:

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 లేదా ఆఫ్‌సెట్ స్థానాన్ని సూచిస్తుంది. ఈ తరగతికి readAheadLimit గుర్తు అసంబద్ధం కాదని గమనించడం ముఖ్యం .


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

బైట్‌అరేఇన్‌పుట్‌స్ట్రీమ్‌లో మార్క్() మరియు రీసెట్() పద్ధతులను ఉపయోగించి గుర్తును సెట్ చేయడానికి ఇక్కడ ఒక ఉదాహరణ ఉంది . మేము మునుపటి ఉదాహరణకి మార్క్() పద్ధతికి కాల్‌ని జోడిస్తాము :


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 అవసరం . ఇన్‌పుట్‌స్ట్రీమ్‌లతో ఎలా పని చేయాలో తెలిసిన ఇతర కోడ్‌తో కలిపి ఈ తరగతిని ఉపయోగించడం సాధారణంగా అర్ధమే .