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 read(byte b[], int off, int len) உள்ளீட்டு ஸ்ட்ரீமில் இருந்து பல பைட்டுகளைப் படித்து, இடையக வரிசையில் b இல் சேமிக்கிறது .
ஆஃப் என்பது இலக்கு வரிசை b க்கு ஆஃப்செட் ஆகும் .
len என்பது படிக்க வேண்டிய பைட்டுகளின் அதிகபட்ச எண்ணிக்கை.
நீண்ட ஸ்கிப் (நீண்ட n) இந்த உள்ளீட்டு ஸ்ட்ரீமில் இருந்து உள்ளீட்டின் n பைட்டுகளைத் தவிர்க்கிறது. தவிர்க்கப்பட்ட பைட்டுகளின் எண்ணிக்கையை வழங்குகிறது (உள்ளீட்டு ஸ்ட்ரீமின் முடிவை நாம் அடைந்தால் அது n ஐ விடக் குறைவாக இருக்கலாம்).
int கிடைக்கிறது() இந்த உள்ளீட்டு ஸ்ட்ரீமில் இருந்து படிக்கக்கூடிய (அல்லது தவிர்க்கப்பட்ட) மீதமுள்ள பைட்டுகளின் எண்ணிக்கையை வழங்குகிறது.
வெற்றிட மீட்டமைப்பு() குறிக்கப்பட்ட நிலைக்கு இடையகத்தை மீட்டமைக்கிறது. கன்ஸ்ட்ரக்டரில் வேறொரு நிலை குறிக்கப்பட்டாலோ அல்லது வேறு ஆஃப்செட் குறிப்பிடப்பட்டாலோ குறிக்கப்பட்ட நிலை 0 ஆகும்.
பூலியன் மார்க் ஆதரிக்கப்படுகிறது() இந்த InputStream குறிக்கும்/மீட்டமைப்பை ஆதரிக்கிறதா என்பதைச் சரிபார்க்கிறது. ByteArrayInputStream க்கு உண்மை என்று திரும்பும் .
வெற்றிட மூட () எதையும் செய்வதில்லை.
வெற்றிட குறி (int readAheadLimit) அமைக்கிறதுகுறிதற்போதைய நிலைக்கு சமமான புலம். மீட்டமைப்பு முறை என்று அழைக்கப்பட்டால் , அந்த நிலையில் இருந்து அடுத்தடுத்த வாசிப்பு தொடங்கும். readAheadLimit அளவுரு பயன்படுத்தப்படவில்லை மற்றும் முறையின் நடத்தையை பாதிக்காது .

இந்த முறைகளை இன்னும் விரிவாகப் பார்ப்போம் மற்றும் அவை நடைமுறையில் எவ்வாறு செயல்படுகின்றன என்பதைப் பார்ப்போம்.

படி()

நீங்கள் ஒரு சாதாரண InputStream இலிருந்து பைட்களைப் படிக்க விரும்புவதைப் போலவே ஒரு 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)

குறிப்பிட்ட எண்ணிக்கையிலான பைட்டுகளைத் தவிர்க்கவும், அவற்றைப் படிக்காமல் இருக்கவும் ஸ்கிப்() முறையைப் பயன்படுத்தலாம் .

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 ஐ மூடுவது எந்த விளைவையும் ஏற்படுத்தாது என்பதை மூடும் முறைக்கான ஆவணங்கள் கூறுகின்றன . ByteArrayInputStream வகுப்பு முறைகள் IOException ஐ விடாமல் ஸ்ட்ரீம் மூடப்பட்ட பிறகு அழைக்கப்படலாம் .

நாம் என்ன முடிவுக்கு வரலாம்?

பைட் வரிசையில் இருந்து தரவைப் படிக்க வேண்டும் என்றால் நமக்கு ஒரு ByteArrayInputStream தேவை . இந்த வகுப்பை இன்புட்ஸ்ட்ரீம்களுடன் எவ்வாறு வேலை செய்வது என்பதைத் தெரிந்துகொள்ளும் பிற குறியீடுகளுடன் இணைந்து பயன்படுத்துவது பொதுவாக அர்த்தமுள்ளதாக இருக்கும் .