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();
}
}
๋ฒํผ์์ ์ฝ์ ๋๋ง๋ค ์ฝ๊ธฐ์ ์ฌ์ฉํ ์ ์๋ ๋ฐ์ดํธ ์๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
์ฐ์ถ:
์ฝ๊ธฐ ๊ฐ๋ฅ ๋ฐ์ดํธ: 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() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ์คํธ๋ฆผ์ ์์์ ์ผ๋ก ์ด๋ํ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค .
์ฐ์ถ:
์ฝ๊ธฐ: 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();
}
}
ํ์ฌ ์คํธ๋ฆผ์ ์์น๊ฐ ๋ณ๊ฒฝ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
์ฐ์ถ:
์ฝ๊ธฐ: 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() ๋ฉ์๋๋ฅผ ์คํํ ํ ์คํธ๋ฆผ์ ํญ์ ์ค๋น ์ํ๊ฐ ๋๋ฉฐ ํ์๋ฅผ ์ง์ํฉ๋๋ค.
์ฐ์ถ:
์ฝ๊ธฐ: 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 ๋ก ์์ ํ๋ ๋ฐฉ๋ฒ์ ์๊ณ ์๋ ๋ค๋ฅธ ์ฝ๋์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค .
GO TO FULL VERSION