티스토리 뷰

 

 

Beginning Java Networking

Chad Darby, www.wrox.com



I/O 처리시 리소스의 효율성을 높이기 위해 버퍼 필터 스트림을 사용하는 예제
// InputBufferTest.java

import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.BufferedInputStream;

public class InputBufferTest {

  public InputBufferTest() {
  }

  public static void main(String args[]) {
    try {
      byte[] b = new byte[]
      {
        (byte)-8, (byte)-4, (byte)-2,
        (byte)0,  (byte)2,  (byte)4
      };
      ByteArrayInputStream byteIn = new ByteArrayInputStream(b);
      BufferedInputStream bis = new BufferedInputStream(byteIn);

      int readByte = bis.read();
      System.out.println("Reading the first byte: " +
          ((readByte <= 127) ? readByte :(readByte - 256)));

      bis.mark(16);

      readByte = bis.read();
      System.out.println("Reading the second byte: " +
          ((readByte <= 127) ? readByte :(readByte - 256)));

      readByte = bis.read();
      System.out.println("Reading the third byte: " +
          ((readByte <= 127) ? readByte :(readByte - 256)));

      readByte = bis.read();
      System.out.println("Reading the fourth byte: " +
          ((readByte <= 127) ? readByte :(readByte - 256)));

      bis.reset();

      readByte = bis.read();
      System.out.println("Reading the second byte again: " +
          ((readByte <= 127) ? readByte :(readByte - 256)));
      } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함