Bit I/O with Java


For my father, Ki-Won Kwon, the greatest man I’ve ever met.

Bit by bit, I’ve realized that’s when I need them, that’s when I need my father’s eyes.
– My Father’s Eyes – by Eric Clapton.

자바에서의 I/O를 통해 구성되는 숫자/문자의 기본 단위는 byte(8-bit octet)입니다. 오디오나 통신분야와 관련된 데이터를 읽기에는 무리가 있습니다. 그래서 library 하나 만들었습니다.

Maven Dependency

Maven Central Repo에서 최신버전을 확인하세요.

<dependency>
  <groupId>com.googlecode.jinahya</groupId>
  <artifactId>bit-io</artifactId>
  <version>1.0-alpha-11</version>
</dependency>

Javadocs

1.0-alpha-11
1.0-alpha-12-SNAPSHOT

일단 기본적인 모양세는 InputStream/OutputStream을 감싸는 작을 클래스가 두개 있습니다. 각 클래스에서 필요한 bit들을 읽거나 쓸 수 있습니다.

com.googlecode.jinahya.io.BitInput

final InputStream in; // your input stream
final BitInput input = new BitInput(in); // create
final boolean flag = input.readBoolean(); // only consumes 1 bit.
final int type = input.readUnsignedInt(3); // read 3-bit unsigned integer
final int padding = input.readInt(27); // read 27-bit signed integer
final long length = input.readUnsignedLong(40); // read 40-bit unsigned long
final long value = input.readLong(52); // read 52-bit long
input.align(1); // align octet; discard required bits for alignment

com.googlecode.jinahya.io.BitOutput

final OutputStream out; // your output stream
final BitOutput output= new BitOutput(out); // create
output.writeBoolean(false); // only consumes 1 bit.
output.writeUnsignedInt(3, 0x07); // write 3-bit unsigned integer
output.writeInt(27, -1000); // write 27-bit integer
output.writeUnsignedLong(40, 0x1FL); // write 40-bit unsigned long
output.writeLong(52, 0xFFFFL); // write 52-bit signed long
output.align(1); // align octet; write required zero bits for alignment
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s