fix some bugs

This commit is contained in:
David Renshaw 2014-09-28 13:09:18 -04:00
parent 9f0c130aa5
commit 25c9e6af0d
3 changed files with 9 additions and 11 deletions

View file

@ -8,6 +8,7 @@ import org.capnproto.MessageBuilder;
import org.capnproto.MessageReader;
import org.capnproto.ByteChannelMessageReader;
import org.capnproto.Serialize;
import org.capnproto.SerializePacked;
import org.capnproto.StructList;
import org.capnproto.Text;
@ -43,8 +44,8 @@ public class AddressbookMain {
bobPhones.get(1).setType(Person.PhoneNumber.Type.WORK);
bob.getEmployment().setUnemployed(org.capnproto.Void.VOID);
Serialize.writeMessage((new FileOutputStream(FileDescriptor.out)).getChannel(),
message);
SerializePacked.writeMessageUnbuffered((new FileOutputStream(FileDescriptor.out)).getChannel(),
message);
}
public static void printAddressBook() throws java.io.IOException {

View file

@ -20,7 +20,6 @@ public final class PackedOutputStream implements WritableByteChannel {
int inPtr = inBuf.position();
int inEnd = inPtr + length;
while (inPtr < inEnd) {
if (out.remaining() < 10) {
//# Oops, we're out of space. We need at least 10
//# bytes for the fast path, since we don't
@ -100,19 +99,15 @@ public final class PackedOutputStream implements WritableByteChannel {
//# An all-zero word is followed by a count of
//# consecutive zero words (not including the first
//# one).
inBuf.position(inPtr);
long inWord = inBuf.getLong();
int runStart = inPtr;
int limit = inEnd;
if (limit - inPtr > 255 * 8) {
limit = inPtr + 255 * 8;
}
while(inBuf.position() < limit && inWord == 0) {
inWord = inBuf.getLong();
while(inPtr < limit && inBuf.getLong(inPtr) == 0){
inPtr += 8;
}
out.put((byte)((inBuf.position() - inPtr)/8 - 1));
inPtr = inBuf.position() - 8;
out.put((byte)((inPtr - runStart)/8));
} else if (tag == (byte)0xff) {
//# An all-nonzero word is followed by a count of
@ -184,6 +179,7 @@ public final class PackedOutputStream implements WritableByteChannel {
this.inner.write(out);
}
inBuf.position(inPtr);
return length;
}

View file

@ -36,6 +36,7 @@ class SerializePackedSuite extends FunSuite {
test("SimplePacking") {
expectPacksTo(Array(), Array());
expectPacksTo(Array(0,0,0,0,0,0,0,0), Array(0,0));
expectPacksTo(Array(0,0,12,0,0,34,0,0), Array(0x24,12,34));
expectPacksTo(Array(1,3,2,4,5,7,6,8), Array(0xff.toByte,1,3,2,4,5,7,6,8,0));
expectPacksTo(Array(0,0,0,0,0,0,0,0, 1,3,2,4,5,7,6,8),