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[], int ๊บผ์ง, int len) ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์—์„œ ์—ฌ๋Ÿฌ ๋ฐ”์ดํŠธ๋ฅผ ์ฝ๊ณ  ๋ฒ„ํผ ๋ฐฐ์—ด b ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค .
off ๋Š” ๋Œ€์ƒ ๋ฐฐ์—ด b ์— ๋Œ€ํ•œ ์˜คํ”„์…‹์ž…๋‹ˆ๋‹ค .
len ์€ ์ฝ์„ ์ตœ๋Œ€ ๋ฐ”์ดํŠธ ์ˆ˜์ž…๋‹ˆ๋‹ค.
๊ธด ์Šคํ‚ต(๊ธด n) ์ด ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์—์„œ n ๋ฐ”์ดํŠธ์˜ ์ž…๋ ฅ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋›ด ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค(์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด n๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์žˆ์Œ).
์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ •์ˆ˜() ์ด ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ๋Š”(๋˜๋Š” ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ๋Š”) ๋‚จ์€ ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๋ฌดํšจ ๋ฆฌ์…‹() ๋ฒ„ํผ๋ฅผ ํ‘œ์‹œ๋œ ์œ„์น˜๋กœ ์žฌ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์œ„์น˜๊ฐ€ ํ‘œ์‹œ๋˜๊ฑฐ๋‚˜ ์ƒ์„ฑ์ž์— ๋‹ค๋ฅธ ์˜คํ”„์…‹์ด ์ง€์ •๋˜์ง€ ์•Š๋Š” ํ•œ ํ‘œ์‹œ๋œ ์œ„์น˜๋Š” 0์ž…๋‹ˆ๋‹ค.
๋ถˆ๋ฆฌ์–ธ ๋งˆํฌSupported() ์ด InputStream ์ด ํ‘œ์‹œ/์žฌ์„ค์ •์„ ์ง€์›ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ByteArrayInputStream ์— ๋Œ€ํ•ด true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค .
๋ฌดํšจ ๋‹ซ๊ธฐ() ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋ฌดํšจ ํ‘œ์‹œ(int readAheadLimit) ์„ค์ •ํ‘œ์‹œํ˜„์žฌ ์œ„์น˜์™€ ๊ฐ™์€ ํ•„๋“œ. reset ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ ๋˜๋ฉด ์ดํ›„ ์ฝ๊ธฐ๋Š” ํ•ด๋‹น ์œ„์น˜์—์„œ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. 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();
   }
}
    

์‚ฌ์šฉ ๊ฐ€๋Šฅ()

๋ฒ„ํผ์— ๋ฌด์–ธ๊ฐ€๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด available() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


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

reset() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ŠคํŠธ๋ฆผ์˜ ์‹œ์ž‘์ ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

์‚ฐ์ถœ:

์ฝ๊ธฐ: 65
์ฝ๊ธฐ: 66
์ฝ๊ธฐ: 67
์ฝ๊ธฐ: 68
reset() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
์ฝ๊ธฐ: 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;
}
    

๋‹ค์Œ์€ mark() ๋ฐ reset() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ByteArrayInputStream ์—์„œ ํ‘œ์‹œ๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค . ์ด์ „ ์˜ˆ์ œ์— mark() ๋ฉ”์„œ๋“œ ์— ๋Œ€ํ•œ ํ˜ธ์ถœ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค .


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
reset() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
์ฝ๊ธฐ: 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;
}
    

๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ 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();
   }
}
    

mark() ๋ฐ reset() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ›„ ์ŠคํŠธ๋ฆผ์€ ํ•ญ์ƒ ์ค€๋น„ ์ƒํƒœ๊ฐ€ ๋˜๋ฉฐ ํ‘œ์‹œ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

์‚ฐ์ถœ:

isMarkSupported: ์ฐธ
์ฝ๊ธฐ: 65
์ฝ๊ธฐ: 66
์ฝ๊ธฐ: 67
isMarkSupported: ์ฐธ
isMarkSupported: ์ฐธ

๋‹ซ๋‹ค()

๊ทธ๋ฆฌ๊ณ  close ๋ฉ”์„œ๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


/**
* 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 {
}
    

close ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ๋ฌธ์„œ๋Š” ByteArrayInputStream ์„ ๋‹ซ์•„๋„ ์•„๋ฌด ํšจ๊ณผ๊ฐ€ ์—†๋‹ค๊ณ  ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ByteArrayInputStream ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋Š” IOException์„ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ณ  ์ŠคํŠธ๋ฆผ์ด ๋‹ซํžŒ ํ›„์— ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

์–ด๋–ค ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

๋ฐ”์ดํŠธ ๋ฐฐ์—ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์œผ๋ ค๋ฉด ByteArrayInputStream ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค . ์ผ๋ฐ˜์ ์œผ๋กœ ์ด ํด๋ž˜์Šค๋ฅผ ๋‹จ๋…์œผ๋กœ๋ณด๋‹ค๋Š” InputStreams ๋กœ ์ž‘์—…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค .