write some bytes to stdout. the bytes are wrong
This commit is contained in:
parent
57c47cff4b
commit
22a54fceaf
5 changed files with 29 additions and 7 deletions
|
@ -1,17 +1,22 @@
|
||||||
package org.capnproto.examples;
|
package org.capnproto.examples;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FileDescriptor;
|
||||||
|
|
||||||
import org.capnproto.MessageBuilder;
|
import org.capnproto.MessageBuilder;
|
||||||
import org.capnproto.MessageReader;
|
import org.capnproto.MessageReader;
|
||||||
import org.capnproto.StructList;
|
|
||||||
import org.capnproto.InputStreamMessageReader;
|
import org.capnproto.InputStreamMessageReader;
|
||||||
|
import org.capnproto.Serialize;
|
||||||
|
import org.capnproto.StructList;
|
||||||
import org.capnproto.Text;
|
import org.capnproto.Text;
|
||||||
|
|
||||||
import org.capnproto.examples.Addressbook.*;
|
import org.capnproto.examples.Addressbook.*;
|
||||||
|
|
||||||
public class AddressbookMain {
|
public class AddressbookMain {
|
||||||
|
|
||||||
public static void writeAddressBook() {
|
public static void writeAddressBook() throws java.io.IOException {
|
||||||
System.out.println("WARNING: writing is not yet fully implemented");
|
System.err.println("WARNING: writing is not yet fully implemented");
|
||||||
|
|
||||||
MessageBuilder message = new MessageBuilder();
|
MessageBuilder message = new MessageBuilder();
|
||||||
AddressBook.Builder addressbook = message.initRoot(AddressBook.Builder.factory);
|
AddressBook.Builder addressbook = message.initRoot(AddressBook.Builder.factory);
|
||||||
StructList.Builder<Person.Builder> people = addressbook.initPeople(2);
|
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).setNumber(new Text.Reader("555-7654"));
|
||||||
bobPhones.get(1).setType(Person.PhoneNumber.Type.WORK);
|
bobPhones.get(1).setType(Person.PhoneNumber.Type.WORK);
|
||||||
bob.getEmployment().setUnemployed();
|
bob.getEmployment().setUnemployed();
|
||||||
|
|
||||||
|
Serialize.writeMessage((new FileOutputStream(FileDescriptor.out)).getChannel(),
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printAddressBook() throws java.io.IOException {
|
public static void printAddressBook() throws java.io.IOException {
|
||||||
|
|
|
@ -5,6 +5,8 @@ import java.nio.ByteOrder;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
public final class BuilderArena implements Arena {
|
public final class BuilderArena implements Arena {
|
||||||
|
|
||||||
|
// Maybe this should be ArrayList?
|
||||||
public final Vector<SegmentBuilder> segments;
|
public final Vector<SegmentBuilder> segments;
|
||||||
|
|
||||||
public BuilderArena() {
|
public BuilderArena() {
|
||||||
|
@ -34,7 +36,15 @@ public final class BuilderArena implements Arena {
|
||||||
throw new Error("unimplemented");
|
throw new Error("unimplemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Vector<ByteBuffer> getSegmentsForOutput() {
|
public final ByteBuffer[] getSegmentsForOutput() {
|
||||||
throw new Error();
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,5 +23,7 @@ public final class MessageBuilder {
|
||||||
return ptr.initAsStruct(factory);
|
return ptr.initAsStruct(factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
//public final getSegmentsForOutput()
|
public final java.nio.ByteBuffer[] getSegmentsForOutput() {
|
||||||
|
return this.arena.getSegmentsForOutput();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ public final class SegmentBuilder extends SegmentReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// return how many words have already been allocated
|
// return how many words have already been allocated
|
||||||
private final int currentSize() {
|
public final int currentSize() {
|
||||||
return this.pos;
|
return this.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class SegmentReader {
|
public class SegmentReader {
|
||||||
|
|
||||||
|
// invariant: buffer's mark is at its beginning.
|
||||||
final ByteBuffer buffer;
|
final ByteBuffer buffer;
|
||||||
|
|
||||||
public SegmentReader(ByteBuffer buffer) {
|
public SegmentReader(ByteBuffer buffer) {
|
||||||
|
|
Loading…
Reference in a new issue