java: add Unpacker.unpack(MessageUnpackable) and Unpacker.tryUnpackNil()

This commit is contained in:
frsyuki 2010-05-22 03:34:43 +09:00
parent 1fe35d7efe
commit b9cb270b8f
2 changed files with 29 additions and 1 deletions

View File

@ -63,6 +63,15 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
} }
} }
private final boolean tryMore(int require) throws IOException, UnpackException {
while(filled - offset < require) {
if(!fill()) {
return false;
}
}
return true;
}
private final void advance(int length) { private final void advance(int length) {
offset += length; offset += length;
} }
@ -275,6 +284,18 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
return null; return null;
} }
final boolean tryUnpackNull() throws IOException {
if(!tryMore(1)) {
return false;
}
int b = buffer[offset] & 0xff;
if(b != 0xc0) { // nil
return false;
}
advance(1);
return 1;
}
final boolean unpackBoolean() throws IOException, MessageTypeException { final boolean unpackBoolean() throws IOException, MessageTypeException {
more(1); more(1);
int b = buffer[offset] & 0xff; int b = buffer[offset] & 0xff;
@ -389,7 +410,6 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
} }
final Object unpackObject() throws IOException { final Object unpackObject() throws IOException {
// FIXME save state, restore state
UnpackResult result = new UnpackResult(); UnpackResult result = new UnpackResult();
if(!next(result)) { if(!next(result)) {
super.reset(); super.reset();

View File

@ -544,5 +544,13 @@ public class Unpacker implements Iterable<Object> {
final public Object unpackObject() throws IOException { final public Object unpackObject() throws IOException {
return impl.unpackObject(); return impl.unpackObject();
} }
final void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
obj.unpackMessage(this);
}
final boolean tryUnpackNull() throws IOException {
return impl.tryUnpackNull();
}
} }