capnproto-java-rpc/runtime/src/main/java/org/capnproto/Data.java

51 lines
1.3 KiB
Java
Raw Normal View History

2014-05-27 21:29:23 +00:00
package org.capnproto;
import java.nio.ByteBuffer;
public final class Data {
public static final class Reader {
2014-10-02 19:27:07 +00:00
public final ByteBuffer buffer;
public final int offset; // in bytes
public final int size; // in bytes
public Reader(ByteBuffer buffer, int offset, int size) {
this.buffer = buffer;
this.offset = offset * 8;
this.size = size;
}
public final int size() {
return this.size;
}
public ByteBuffer asByteBuffer() {
2014-10-03 14:40:00 +00:00
ByteBuffer dup = this.buffer.duplicate();
dup.position(this.offset);
ByteBuffer result = dup.slice();
2014-10-02 19:27:07 +00:00
result.limit(this.size);
return result;
}
public byte[] asArray() {
2014-10-03 14:40:00 +00:00
ByteBuffer dup = this.buffer.duplicate();
2014-10-02 19:27:07 +00:00
byte result[] = new byte[this.size];
2014-10-03 14:40:00 +00:00
dup.position(this.offset);
dup.get(result, 0, this.size);
2014-10-02 19:27:07 +00:00
return result;
}
2014-05-27 21:29:23 +00:00
}
public static final class Builder {
2014-10-02 19:27:07 +00:00
public final ByteBuffer buffer;
public final int offset; // in bytes
public final int size; // in bytes
2014-05-27 21:29:23 +00:00
2014-10-02 19:27:07 +00:00
public Builder(ByteBuffer buffer, int offset, int size) {
this.buffer = buffer;
this.offset = offset;
this.size = size;
}
}
2014-05-27 21:29:23 +00:00
}