'Networking'에 해당되는 글 6건

  1. 2012.10.02 [JAVA][예제] InputBufferTest 예제 - Beginning Java Networking
[IT]/컴퓨터2012.10.02 13:00

 

 

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



저작자 표시
신고
Posted by 제임스구 jamesku

티스토리 툴바