Tagged: i/o

Asymmetries between FilterInputStream and FilterOutputStream


Costructors

FilterInputStream(InputStream) is protected and FilterOutputStream(OutputStream) is `public.

protected FilterInputStream(InputStream in) {
    this.in = in;
}
public FilterOutputStream(OutputStream out) {
    this.out = out;
}

Methods

FilterInputStream#read(b, off, len) performs in.read(b, off, len) and returns the result and FilterOutputStream#write(b, off, len), on the other hand, calls the write(int) method on each byte to output.

public int read(byte b[], int off, int len) throws IOException {
    return in.read(b, off, len);
}
public void write(byte b[], int off, int len) throws IOException {
    if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
        throw new IndexOutOfBoundsException();

    for (int i = 0 ; i < len ; i++) {
       write(b[off + i]);
    }
}

required standard open options for creating or replacing a file


I just found a bug on my own code.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE, // create if not exist
    StandardOpenOption.WRITE // i'm gonna write on
);

Above code seemed working, but when I tried to replace a bigger file with a smaller one, the file size remains and the only leading(?) portion of file contents replaced.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE,
    StandardOpenOption.TRUNCATE_EXISTING, // truncate the file!!!
    StandardOpenOption.WRITE
);

reading/writing bits in java


I’ve written a small yet handy library for reading/writing arbitrary length of bits in java.

Apache Subversion

svn co http://jinahya.googlecode.com/svn/trunk/com.googlecode.jinahya/bit-io/

Apache Maven

<dependency>
  <!-- resides in maven central repo -->
  <groupId>com.googlecode.jinahya</groupId>
  <artifactId>bit-io</artifactId>
  <version>1.0-alpha-15</version>
</dependency>

Reading from an InputStream

final InputStream stream; // given

final BitInput input = new BitInput(new BitInput.StreamInput(stream));

final boolean b = input.readBoolean(); // reads a 1-bit boolean
final int i = input.readUnsignedInt(27); // reads a 27-bit unsigned int
final long l = input.readLong(48); // reads a 48-bit signed long

input.align(1); // align to 8-bit byte; padding

Reading from a ByteBuffer

final ByteBuffer buffer; // given

final BitInput input = new BitInput(new BitInput.BufferInput(buffer));

// goes same

Writing to an OutputStream

final OutputStream stream; // given

final BitOutput output = new BitOutput(new BitOutput.StreamOutput(stream));

output.writeBoolean(true); // writes a 1-bit boolean
output.writeInt(17, 0x00); // writes a 17-bit signed int
output.writeUnsignedLong(54, 0x00L); // writes a 54-bit unsigned long

output.align(4); // align to 32-bit byte; discarding

Writing to a ByteBuffer

final ByteBuffer buffer; // given

final BitOutput output = new BitOutput(new BitOutput.BufferOutput(buffer));

// goes same

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