write some bytes to stdout. the bytes are wrong

This commit is contained in:
David Renshaw 2014-05-24 14:33:25 -04:00
parent 57c47cff4b
commit 22a54fceaf
5 changed files with 29 additions and 7 deletions

View file

@ -1,17 +1,22 @@
package org.capnproto.examples;
import java.io.FileOutputStream;
import java.io.FileDescriptor;
import org.capnproto.MessageBuilder;
import org.capnproto.MessageReader;
import org.capnproto.StructList;
import org.capnproto.InputStreamMessageReader;
import org.capnproto.Serialize;
import org.capnproto.StructList;
import org.capnproto.Text;
import org.capnproto.examples.Addressbook.*;
public class AddressbookMain {
public static void writeAddressBook() {
System.out.println("WARNING: writing is not yet fully implemented");
public static void writeAddressBook() throws java.io.IOException {
System.err.println("WARNING: writing is not yet fully implemented");
MessageBuilder message = new MessageBuilder();
AddressBook.Builder addressbook = message.initRoot(AddressBook.Builder.factory);
StructList.Builder<Person.Builder> people = addressbook.initPeople(2);
@ -36,6 +41,9 @@ public class AddressbookMain {
bobPhones.get(1).setNumber(new Text.Reader("555-7654"));
bobPhones.get(1).setType(Person.PhoneNumber.Type.WORK);
bob.getEmployment().setUnemployed();
Serialize.writeMessage((new FileOutputStream(FileDescriptor.out)).getChannel(),
message);
}
public static void printAddressBook() throws java.io.IOException {

View file

@ -5,6 +5,8 @@ import java.nio.ByteOrder;
import java.util.Vector;
public final class BuilderArena implements Arena {
// Maybe this should be ArrayList?
public final Vector<SegmentBuilder> segments;
public BuilderArena() {
@ -34,7 +36,15 @@ public final class BuilderArena implements Arena {
throw new Error("unimplemented");
}
public final Vector<ByteBuffer> getSegmentsForOutput() {
throw new Error();
public final ByteBuffer[] getSegmentsForOutput() {
ByteBuffer[] result = new ByteBuffer[this.segments.size()];
for (int ii = 0; ii < this.segments.size(); ++ii) {
SegmentBuilder segment = segments.get(ii);
segment.buffer.reset();
ByteBuffer slice = segment.buffer.slice();
slice.limit(segment.currentSize() * 8);
result[ii] = slice;
}
return result;
}
}

View file

@ -23,5 +23,7 @@ public final class MessageBuilder {
return ptr.initAsStruct(factory);
}
//public final getSegmentsForOutput()
public final java.nio.ByteBuffer[] getSegmentsForOutput() {
return this.arena.getSegmentsForOutput();
}
}

View file

@ -18,7 +18,7 @@ public final class SegmentBuilder extends SegmentReader {
}
// return how many words have already been allocated
private final int currentSize() {
public final int currentSize() {
return this.pos;
}

View file

@ -3,6 +3,8 @@ package org.capnproto;
import java.nio.ByteBuffer;
public class SegmentReader {
// invariant: buffer's mark is at its beginning.
final ByteBuffer buffer;
public SegmentReader(ByteBuffer buffer) {