์ด์ ์ ์ฐ๋ฆฌ๋ IO API (Input/Output Application Programming Interface)์ java.io ํจํค์ง์ ๋ํด ์๊ฒ ๋์์ต๋๋ค . ์ด ํจํค์ง์ ํด๋์ค๋ ์ฃผ๋ก Java์์ ์คํธ๋ฆผ ์์ ์ ์ํ ๊ฒ์ ๋๋ค. ์ฌ๊ธฐ์ ํต์ฌ์ ์คํธ๋ฆผ ์ ๊ฐ๋ ์ ๋๋ค .
์ค๋ ์ฐ๋ฆฌ๋ NIO API (New Input/Output) ๋ฅผ ๊ณ ๋ คํ๊ธฐ ์์ํ ๊ฒ์ ๋๋ค .
I/O์ ๋ํ ๋ ๊ฐ์ง ์ ๊ทผ ๋ฐฉ์์ ์ฃผ์ ์ฐจ์ด์ ์ IO API๋ ์คํธ๋ฆผ ์งํฅ์ ์ธ ๋ฐ๋ฉด NIO API๋ ๋ฒํผ ์งํฅ์ ์ด๋ผ๋ ๊ฒ์ ๋๋ค. ๋ฐ๋ผ์ ์ดํดํด์ผ ํ ์ฃผ์ ๊ฐ๋ ์ ๋ฒํผ ์ ์ฑ๋ ์ ๋๋ค .
๋ฒํผ๋ ๋ฌด์์ด๋ฉฐ ์ฑ๋์ ๋ฌด์์ ๋๊น?
์ฑ๋ ์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ค๊ณ ๋๊ฐ๋ ๋ ผ๋ฆฌ์ ํฌํธ์ธ ๋ฐ๋ฉด ๋ฒํผ๋ ์ด ์ ์ก๋ ๋ฐ์ดํฐ์ ์์ค ๋๋ ๋์์ ๋๋ค. ์ถ๋ ฅํ๋ ๋์ ๋ณด๋ด๋ ค๋ ๋ฐ์ดํฐ๋ ๋ฒํผ์ ๋ฃ๊ณ ๋ฒํผ๋ ๋ฐ์ดํฐ๋ฅผ ์ฑ๋๋ก ์ ๋ฌํฉ๋๋ค. ์ ๋ ฅํ๋ ๋์ ์ฑ๋์ ๋ฐ์ดํฐ๊ฐ ๋ฒํผ์ ๋ค์ด๊ฐ๋๋ค.
๋ค์ ๋งํด์:
- ๋ฒํผ ๋ ๋จ์ํ ์ ๋ณด๋ฅผ ์ฐ๊ณ ์ ๋ณด๋ฅผ ์ฝ์ ์ ์๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก์ ๋๋ค.
- ์ฑ๋ ์ ํ์ผ์ด๋ ์์ผ๊ณผ ๊ฐ์ I/O ์ฅ์น์ ๋ํ ์ก์ธ์ค๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ดํธ์จ์ด์ ๋๋ค.
์ฑ๋์ java.io ํจํค์ง์ ์คํธ๋ฆผ๊ณผ ๋งค์ฐ ์ ์ฌํฉ๋๋ค. ์ด๋๋ก๋ ๊ฐ๋(๋๋ ์ด๋์์ ์ค๋) ๋ชจ๋ ๋ฐ์ดํฐ๋ ์ฑ๋ ๊ฐ์ฒด๋ฅผ ํต๊ณผํด์ผ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก NIO ์์คํ ์ ์ฌ์ฉํ๋ ค๋ฉด I/O ์ํฐํฐ์ ๋ํ ์ฑ๋๊ณผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋ฒํผ๋ฅผ ์ป์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋ฒํผ๋ก ์์ ํ์ฌ ํ์์ ๋ฐ๋ผ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๊ฑฐ๋ ์ถ๋ ฅํฉ๋๋ค.
๋ฒํผ์์ ์๋ค๋ก ์ด๋ํ ์ ์์ต๋๋ค. ์ฆ, ์คํธ๋ฆผ์์ ํ ์ ์๋ ๋ฒํผ๋ฅผ "๊ฑท๊ธฐ"ํ ์ ์์ต๋๋ค. ์ด๋ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋ ๋ ๋ง์ ์ ์ฐ์ฑ์ ์ ๊ณตํฉ๋๋ค. ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ๋ฒํผ๋ ์ถ์ Buffer ํด๋์ค์ ์ฌ๋ฌ ํ์ ํญ๋ชฉ์ผ๋ก ํ์๋ฉ๋๋ค.
- ๋ฐ์ดํธ๋ฒํผ
- CharBuffer
- ShortBuffer
- IntBuffer
- ๋ถ๋ ๋ฒํผ
- ๋๋ธ๋ฒํผ
- ๋กฑ๋ฒํผ
ํ์ ํด๋์ค ๊ฐ์ ์ฃผ์ ์ฐจ์ด์ ์ ๊ทธ๋ค์ด ์ ์ฅํ๋ ๋ฐ์ดํฐ ์ ํ โ bytes , ints , longs ๋ฐ ๊ธฐํ ๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ์ ๋๋ค.
๋ฒํผ ์์ฑ
๋ฒํผ์๋ ๋ค ๊ฐ์ง ์ฃผ์ ์์ฑ์ด ์์ต๋๋ค. ์ฉ๋, ํ๊ณ, ์์น ๋ฐ ํ์์ ๋๋ค.
์ฉ๋์ ๋ฒํผ์ ์ ์ฅํ ์ ์๋ ์ต๋ ๋ฐ์ดํฐ/๋ฐ์ดํธ ์์ ๋๋ค. ๋ฒํผ์ ์ฉ๋์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค . ๋ฒํผ๊ฐ ๊ฐ๋ ์ฐจ๋ฉด ๋ ์ฐ๊ธฐ ์ ์ ์ง์์ผ ํฉ๋๋ค.
์ฐ๊ธฐ ๋ชจ๋์์ ๋ฒํผ์ ํ๊ณ๋ ์ฉ๋๊ณผ ๋์ผํ๋ฉฐ ๋ฒํผ์ ์ธ ์ ์๋ ์ต๋ ๋ฐ์ดํฐ ์์ ๋ํ๋ ๋๋ค. ์ฝ๊ธฐ ๋ชจ๋์์ ๋ฒํผ์ ํ๊ณ๋ ๋ฒํผ์์ ์ฝ์ ์ ์๋ ์ต๋ ๋ฐ์ดํฐ ์์ ๋ํ๋ ๋๋ค.
์์น ๋ ๋ฒํผ์์ ์ปค์์ ํ์ฌ ์์น๋ฅผ ๋ํ๋ ๋๋ค. ์ฒ์์๋ ๋ฒํผ๊ฐ ์์ฑ๋ ๋ 0์ผ๋ก ์ค์ ๋ฉ๋๋ค. ์ฆ, ์ฝ๊ฑฐ๋ ์ธ ๋ค์ ์์์ ์ธ๋ฑ์ค์ ๋๋ค.
๋งํฌ ๋ ์ปค์ ์์น๋ฅผ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ๋ฒํผ๋ฅผ ์กฐ์ํ ๋ ์ปค์ ์์น๋ ๊ณ์ ๋ณ๊ฒฝ๋์ง๋ง ํญ์ ์ด์ ์ ํ์๋ ์์น๋ก ๋๋๋ฆด ์ ์์ต๋๋ค.
๋ฒํผ ์์ ๋ฐฉ๋ฒ
์ด์ ์ฑ๋์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๊ธฐ ์ํด ๋ฒํผ(๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก)๋ก ์์ ํ ์ ์๊ฒ ํด์ฃผ๋ ์ฃผ์ ๋ฉ์๋ ์งํฉ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
-
allocate(int capacity) โ ์ด ๋ฉ์๋๋ ์ง์ ๋ ์ฉ๋์ผ๋ก ์ ๋ฒํผ๋ฅผ ํ ๋นํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ ๋ฌ๋ ์ฉ๋์ด ์์ ์ ์์ธ ๊ฒฝ์ฐ allocate () ๋ฉ์๋๋ IllegalArgumentException ์ ๋ฐ์์ํต๋๋ค .
-
capacity()๋ ํ์ฌ ๋ฒํผ์ ์ฉ๋์ ๋ฐํํฉ๋๋ค .
-
position()์ ํ์ฌ ์ปค์ ์์น๋ฅผ ๋ฐํํฉ๋๋ค. ์ฝ๊ธฐ ๋ฐ ์ฐ๊ธฐ ์์ ์ ์ปค์๋ฅผ ๋ฒํผ์ ๋์ผ๋ก ์ด๋ํฉ๋๋ค. ๋ฐํ ๊ฐ์ ํญ์ ์ ํ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ต๋๋ค.
-
limit()๋ ํ์ฌ ๋ฒํผ์ ํ๊ณ๋ฅผ ๋ฐํํฉ๋๋ค.
-
mark()๋ ํ์ฌ ์ปค์ ์์น๋ฅผ ํ์(์ ์ฅ)ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
-
reset()์ ์ปค์๋ฅผ ์ด์ ์ ํ์๋(์ ์ฅ๋) ์์น๋ก ๋๋๋ฆฝ๋๋ค.
-
clear()๋ ์์น๋ฅผ 0์ผ๋ก ์ค์ ํ๊ณ ์ ํ์ ์ฉ๋์ผ๋ก ์ค์ ํฉ๋๋ค. ์ด ๋ฉ์๋๋ ๋ฒํผ์ ๋ฐ์ดํฐ๋ฅผ ์ง์ฐ์ง ์์ต๋๋ค . ์์น, ํ๊ณ ๋ฐ ๋งํฌ๋ง ๋ค์ ์ด๊ธฐํํฉ๋๋ค.
-
flip()์ ๋ฒํผ๋ฅผ ์ฐ๊ธฐ ๋ชจ๋์์ ์ฝ๊ธฐ ๋ชจ๋๋ก ์ ํํฉ๋๋ค. ๋ํ ํ๊ณ๋ฅผ ํ์ฌ ์์น๋ก ์ค์ ํ ๋ค์ ์์น๋ฅผ ๋ค์ 0์ผ๋ก ์ค์ ํฉ๋๋ค.
-
read() โ ์ฑ๋์ read ๋ฉ์๋๋ ์ฑ๋์์ ๋ฒํผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ ๋ฐ ์ฌ์ฉ๋๋ ๋ฐ๋ฉด ๋ฒํผ์ put() ๋ฉ์๋๋ ๋ฐ์ดํฐ๋ฅผ ๋ฒํผ์ ์ฐ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
-
write() โ ์ฑ๋์ write ๋ฉ์๋๋ ๋ฒํผ์์ ์ฑ๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ ๋ฐ ์ฌ์ฉ๋๋ ๋ฐ๋ฉด ๋ฒํผ์ get() ๋ฉ์๋๋ ๋ฒํผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
-
rewind()๋ ๋ฒํผ๋ฅผ ๋๊ฐ์ต๋๋ค. ์ด ๋ฐฉ๋ฒ์ ๋ฒํผ๋ฅผ ๋ค์ ์ฝ์ด์ผ ํ ๋ ์ฌ์ฉ๋๋ฉฐ ์์น๋ฅผ 0์ผ๋ก ์ค์ ํ๊ณ ์ ํ์ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
์ด์ ์ฑ๋์ ๋ํ ๋ช ๋ง๋.
Java NIO์์ ๊ฐ์ฅ ์ค์ํ ์ฑ๋ ๊ตฌํ์ ๋ค์ ํด๋์ค์ ๋๋ค.
-
FileChannel โ ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๊ธฐ ์ํ ์ฑ๋์ ๋๋ค.
-
DatagramChannel โ ์ด ํด๋์ค๋ UDP(์ฌ์ฉ์ ๋ฐ์ดํฐ๊ทธ๋จ ํ๋กํ ์ฝ)๋ฅผ ํตํด ๋คํธ์ํฌ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์๋๋ค.
-
SocketChannel โ TCP(์ ์ก ์ ์ด ํ๋กํ ์ฝ)๋ฅผ ํตํด ๋คํธ์ํฌ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๊ธฐ ์ํ ์ฑ๋์ ๋๋ค.
-
ServerSocketChannel โ ์น ์๋ฒ์ ๋ง์ฐฌ๊ฐ์ง๋ก TCP ์ฐ๊ฒฐ์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๊ธฐ ์ํ ์ฑ๋์ ๋๋ค. ๋ค์ด์ค๋ ๊ฐ ์ฐ๊ฒฐ์ ๋ํด SocketChannel ์ด ์์ฑ๋ฉ๋๋ค.
๊ดํ
๋ช ์ค์ ์ฝ๋๋ฅผ ์์ฑํ ๋์ ๋๋ค. ๋จผ์ ํ์ผ์ ์ฝ๊ณ ์ฝ์์ ๋ด์ฉ์ ํ์ํ ๋ค์ ํ์ผ์ ๋ฌธ์์ด์ ์๋๋ค.
์ฝ๋์๋ ๋ง์ ์ฃผ์์ด ํฌํจ๋์ด ์์ต๋๋ค. ๋ชจ๋ ๊ฒ์ด ์ด๋ป๊ฒ ์๋ํ๋์ง ์ดํดํ๋ ๋ฐ ๋์์ด ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
// Create a RandomAccessFile object, passing in the file path
// and a string that says the file will be opened for reading and writing
try (RandomAccessFile randomAccessFile = new RandomAccessFile("text.txt", "rw");
// Get an instance of the FileChannel class
FileChannel channel = randomAccessFile.getChannel();
) {
// Our file is small, so we'll read it in one go
// Create a buffer of the required size based on the size of our channel
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
// Read data will be put into a StringBuilder
StringBuilder builder = new StringBuilder();
// Write data from the channel to the buffer
channel.read(byteBuffer);
// Switch the buffer from write mode to read mode
byteBuffer.flip();
// In a loop, write data from the buffer to the StringBuilder
while (byteBuffer.hasRemaining()) {
builder.append((char) byteBuffer.get());
}
// Display the contents of the StringBuilder on the console
System.out.println(builder);
// Now let's continue our program and write data from a string to the file
// Create a string with arbitrary text
String someText = "Hello, Amigo!!!!!";
// Create a new buffer for writing,
// but let the channel remain the same, because we're going to the same file
// In other words, we can use one channel for both reading and writing to a file
// Create a buffer specifically for our string โ convert the string into an array and get its length
ByteBuffer byteBuffer2 = ByteBuffer.allocate(someText.getBytes().length);
// Write our string to the buffer
byteBuffer2.put(someText.getBytes());
// Switch the buffer from write mode to read mode
// so that the channel can read from the buffer and write our string to the file
byteBuffer2.flip();
// The channel reads the information from the buffer and writes it to our file
channel.write(byteBuffer2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NIO API๋ฅผ ์ฌ์ฉํด ๋ณด์ธ์ . ๋ง์์ ๋์ค ๊ฒ๋๋ค!
GO TO FULL VERSION