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.MessageReader;
import org.capnproto.ByteChannelMessageReader; import org.capnproto.ByteChannelMessageReader;
import org.capnproto.Serialize; import org.capnproto.Serialize;
import org.capnproto.SerializePacked;
import org.capnproto.StructList; import org.capnproto.StructList;
import org.capnproto.Text; import org.capnproto.Text;
@ -43,8 +44,8 @@ public class AddressbookMain {
bobPhones.get(1).setType(Person.PhoneNumber.Type.WORK); bobPhones.get(1).setType(Person.PhoneNumber.Type.WORK);
bob.getEmployment().setUnemployed(org.capnproto.Void.VOID); bob.getEmployment().setUnemployed(org.capnproto.Void.VOID);
Serialize.writeMessage((new FileOutputStream(FileDescriptor.out)).getChannel(), SerializePacked.writeMessageUnbuffered((new FileOutputStream(FileDescriptor.out)).getChannel(),
message); message);
} }
public static void printAddressBook() throws java.io.IOException { 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 inPtr = inBuf.position();
int inEnd = inPtr + length; int inEnd = inPtr + length;
while (inPtr < inEnd) { while (inPtr < inEnd) {
if (out.remaining() < 10) { if (out.remaining() < 10) {
//# Oops, we're out of space. We need at least 10 //# Oops, we're out of space. We need at least 10
//# bytes for the fast path, since we don't //# 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 //# An all-zero word is followed by a count of
//# consecutive zero words (not including the first //# consecutive zero words (not including the first
//# one). //# one).
int runStart = inPtr;
inBuf.position(inPtr);
long inWord = inBuf.getLong();
int limit = inEnd; int limit = inEnd;
if (limit - inPtr > 255 * 8) { if (limit - inPtr > 255 * 8) {
limit = inPtr + 255 * 8; limit = inPtr + 255 * 8;
} }
while(inBuf.position() < limit && inWord == 0) { while(inPtr < limit && inBuf.getLong(inPtr) == 0){
inWord = inBuf.getLong(); inPtr += 8;
} }
out.put((byte)((inBuf.position() - inPtr)/8 - 1)); out.put((byte)((inPtr - runStart)/8));
inPtr = inBuf.position() - 8;
} else if (tag == (byte)0xff) { } else if (tag == (byte)0xff) {
//# An all-nonzero word is followed by a count of //# An all-nonzero word is followed by a count of
@ -184,6 +179,7 @@ public final class PackedOutputStream implements WritableByteChannel {
this.inner.write(out); this.inner.write(out);
} }
inBuf.position(inPtr);
return length; return length;
} }

View file

@ -36,6 +36,7 @@ class SerializePackedSuite extends FunSuite {
test("SimplePacking") { test("SimplePacking") {
expectPacksTo(Array(), Array()); 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(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(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), expectPacksTo(Array(0,0,0,0,0,0,0,0, 1,3,2,4,5,7,6,8),