mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-21 15:51:44 +02:00
java: fix direct conversion API
This commit is contained in:
@@ -22,16 +22,17 @@ import java.nio.ByteBuffer;
|
||||
//import java.math.BigInteger;
|
||||
|
||||
abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
int offset = 0;
|
||||
int filled = 0;
|
||||
byte[] buffer = null;
|
||||
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
|
||||
|
||||
abstract boolean fill() throws IOException;
|
||||
|
||||
final int next(int offset, UnpackResult result) throws IOException, UnpackException {
|
||||
final boolean next(UnpackResult result) throws IOException, UnpackException {
|
||||
if(filled == 0) {
|
||||
if(!fill()) {
|
||||
return offset;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,8 +40,9 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
int noffset = super.execute(buffer, offset, filled);
|
||||
if(noffset <= offset) {
|
||||
if(!fill()) {
|
||||
return offset;
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
offset = noffset;
|
||||
} while(!super.isFinished());
|
||||
@@ -49,10 +51,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
super.reset();
|
||||
result.done(obj);
|
||||
|
||||
return offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
private final void more(int offset, int require) throws IOException, UnpackException {
|
||||
private final void more(int require) throws IOException, UnpackException {
|
||||
while(filled - offset < require) {
|
||||
if(!fill()) {
|
||||
// FIXME
|
||||
@@ -61,41 +63,46 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
}
|
||||
}
|
||||
|
||||
final byte unpackByte(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
int o = unpackInt(c, offset);
|
||||
private final void advance(int length) {
|
||||
offset += length;
|
||||
}
|
||||
|
||||
final byte unpackByte() throws IOException, MessageTypeException {
|
||||
int o = unpackInt();
|
||||
if(0x7f < o || o < -0x80) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)o;
|
||||
}
|
||||
|
||||
final short unpackShort(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
int o = unpackInt(c, offset);
|
||||
final short unpackShort() throws IOException, MessageTypeException {
|
||||
int o = unpackInt();
|
||||
if(0x7fff < o || o < -0x8000) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (short)o;
|
||||
}
|
||||
|
||||
final int unpackInt(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final int unpackInt() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum
|
||||
advance(1);
|
||||
return (int)b;
|
||||
}
|
||||
switch(b & 0xff) {
|
||||
case 0xcc: // unsigned int 8
|
||||
more(offset, 2);
|
||||
c.advance(2);
|
||||
more(2);
|
||||
advance(2);
|
||||
return (int)((short)buffer[offset+1] & 0xff);
|
||||
case 0xcd: // unsigned int 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (int)((int)castBuffer.getShort(0) & 0xffff);
|
||||
case 0xce: // unsigned int 32
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
{
|
||||
@@ -103,11 +110,11 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
if(o < 0) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
return o;
|
||||
}
|
||||
case 0xcf: // unsigned int 64
|
||||
more(offset, 9);
|
||||
more(9);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 8);
|
||||
{
|
||||
@@ -115,27 +122,27 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
if(o < 0 || o > 0x7fffffffL) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
c.advance(9);
|
||||
advance(9);
|
||||
return (int)o;
|
||||
}
|
||||
case 0xd0: // signed int 8
|
||||
more(offset, 2);
|
||||
c.advance(2);
|
||||
more(2);
|
||||
advance(2);
|
||||
return (int)buffer[offset+1];
|
||||
case 0xd1: // signed int 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (int)castBuffer.getShort(0);
|
||||
case 0xd2: // signed int 32
|
||||
more(offset, 4);
|
||||
more(4);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(4);
|
||||
advance(4);
|
||||
return (int)castBuffer.getInt(0);
|
||||
case 0xd3: // signed int 64
|
||||
more(offset, 9);
|
||||
more(9);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 8);
|
||||
{
|
||||
@@ -143,7 +150,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
if(0x7fffffffL < o || o < -0x80000000L) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
c.advance(9);
|
||||
advance(9);
|
||||
return (int)o;
|
||||
}
|
||||
default:
|
||||
@@ -151,31 +158,32 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
}
|
||||
}
|
||||
|
||||
final long unpackLong(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final long unpackLong() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum
|
||||
advance(1);
|
||||
return (long)b;
|
||||
}
|
||||
switch(b & 0xff) {
|
||||
case 0xcc: // unsigned int 8
|
||||
more(offset, 2);
|
||||
c.advance(2);
|
||||
more(2);
|
||||
advance(2);
|
||||
return (long)((short)buffer[offset+1] & 0xff);
|
||||
case 0xcd: // unsigned int 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (long)((int)castBuffer.getShort(0) & 0xffff);
|
||||
case 0xce: // unsigned int 32
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
return ((long)castBuffer.getInt(0) & 0xffffffffL);
|
||||
case 0xcf: // unsigned int 64
|
||||
more(offset, 9);
|
||||
more(9);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 8);
|
||||
{
|
||||
@@ -184,51 +192,51 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
// FIXME
|
||||
throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported");
|
||||
}
|
||||
c.advance(9);
|
||||
advance(9);
|
||||
return o;
|
||||
}
|
||||
case 0xd0: // signed int 8
|
||||
more(offset, 2);
|
||||
c.advance(2);
|
||||
more(2);
|
||||
advance(2);
|
||||
return (long)buffer[offset+1];
|
||||
case 0xd1: // signed int 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (long)castBuffer.getShort(0);
|
||||
case 0xd2: // signed int 32
|
||||
more(offset, 4);
|
||||
more(4);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(4);
|
||||
advance(4);
|
||||
return (long)castBuffer.getInt(0);
|
||||
case 0xd3: // signed int 64
|
||||
more(offset, 9);
|
||||
more(9);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 8);
|
||||
c.advance(9);
|
||||
advance(9);
|
||||
return (long)castBuffer.getLong(0);
|
||||
default:
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
final float unpackFloat(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final float unpackFloat() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
switch(b & 0xff) {
|
||||
case 0xca: // float
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
return castBuffer.getFloat(0);
|
||||
case 0xcb: // double
|
||||
more(offset, 9);
|
||||
more(9);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 8);
|
||||
c.advance(9);
|
||||
advance(9);
|
||||
// FIXME overflow check
|
||||
return (float)castBuffer.getDouble(0);
|
||||
default:
|
||||
@@ -236,70 +244,70 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
}
|
||||
}
|
||||
|
||||
final double unpackDouble(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final double unpackDouble() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
switch(b & 0xff) {
|
||||
case 0xca: // float
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
return (double)castBuffer.getFloat(0);
|
||||
case 0xcb: // double
|
||||
more(offset, 9);
|
||||
more(9);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 8);
|
||||
c.advance(9);
|
||||
advance(9);
|
||||
return castBuffer.getDouble(0);
|
||||
default:
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
final Object unpackNull(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final Object unpackNull() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset] & 0xff;
|
||||
if(b != 0xc0) { // nil
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
c.advance(1);
|
||||
advance(1);
|
||||
return null;
|
||||
}
|
||||
|
||||
final boolean unpackBoolean(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final boolean unpackBoolean() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset] & 0xff;
|
||||
if(b == 0xc2) { // false
|
||||
c.advance(1);
|
||||
advance(1);
|
||||
return false;
|
||||
} else if(b == 0xc3) { // true
|
||||
c.advance(1);
|
||||
advance(1);
|
||||
return true;
|
||||
} else {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
final int unpackArray(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final int unpackArray() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
if((b & 0xf0) == 0x90) { // FixArray
|
||||
c.advance(1);
|
||||
advance(1);
|
||||
return (int)(b & 0x0f);
|
||||
}
|
||||
switch(b & 0xff) {
|
||||
case 0xdc: // array 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (int)castBuffer.getShort(0) & 0xffff;
|
||||
case 0xdd: // array 32
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
// FIXME overflow check
|
||||
return castBuffer.getInt(0) & 0x7fffffff;
|
||||
default:
|
||||
@@ -307,25 +315,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
}
|
||||
}
|
||||
|
||||
final int unpackMap(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final int unpackMap() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
if((b & 0xf0) == 0x80) { // FixMap
|
||||
c.advance(1);
|
||||
advance(1);
|
||||
return (int)(b & 0x0f);
|
||||
}
|
||||
switch(b & 0xff) {
|
||||
case 0xde: // map 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (int)castBuffer.getShort(0) & 0xffff;
|
||||
case 0xdf: // map 32
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
// FIXME overflow check
|
||||
return castBuffer.getInt(0) & 0x7fffffff;
|
||||
default:
|
||||
@@ -333,25 +341,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
}
|
||||
}
|
||||
|
||||
final int unpackRaw(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
more(offset, 1);
|
||||
final int unpackRaw() throws IOException, MessageTypeException {
|
||||
more(1);
|
||||
int b = buffer[offset];
|
||||
if((b & 0xe0) == 0xa0) { // FixRaw
|
||||
c.advance(1);
|
||||
return (int)(b & 0x0f);
|
||||
advance(1);
|
||||
return (int)(b & 0x1f);
|
||||
}
|
||||
switch(b & 0xff) {
|
||||
case 0xda: // raw 16
|
||||
more(offset, 3);
|
||||
more(3);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 2);
|
||||
c.advance(3);
|
||||
advance(3);
|
||||
return (int)castBuffer.getShort(0) & 0xffff;
|
||||
case 0xdb: // raw 32
|
||||
more(offset, 5);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
c.advance(5);
|
||||
advance(5);
|
||||
// FIXME overflow check
|
||||
return castBuffer.getInt(0) & 0x7fffffff;
|
||||
default:
|
||||
@@ -359,26 +367,35 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
}
|
||||
}
|
||||
|
||||
final byte[] unpackRawBody(UnpackCursor c, int offset, int length) throws IOException, MessageTypeException {
|
||||
more(offset, length);
|
||||
final byte[] unpackRawBody(int length) throws IOException, MessageTypeException {
|
||||
more(length);
|
||||
byte[] bytes = new byte[length];
|
||||
System.arraycopy(buffer, offset, bytes, 0, length);
|
||||
c.advance(length);
|
||||
advance(length);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
final String unpackString(UnpackCursor c, int offset) throws IOException, MessageTypeException {
|
||||
int length = unpackRaw(c, offset);
|
||||
offset = c.getOffset();
|
||||
more(offset, length);
|
||||
final String unpackString() throws IOException, MessageTypeException {
|
||||
int length = unpackRaw();
|
||||
more(length);
|
||||
String s;
|
||||
try {
|
||||
s = new String(buffer, offset, length, "UTF-8");
|
||||
} catch (Exception e) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
c.advance(length);
|
||||
advance(length);
|
||||
return s;
|
||||
}
|
||||
|
||||
final Object unpackObject() throws IOException, MessageTypeException {
|
||||
// FIXME save state, restore state
|
||||
UnpackResult result = new UnpackResult();
|
||||
if(!next(result)) {
|
||||
super.reset();
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return result.getData();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -20,6 +20,6 @@ package org.msgpack;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface MessageUnpackable {
|
||||
public void messageUnpack(Unpacker pk) throws IOException, MessageTypeException;
|
||||
public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException;
|
||||
}
|
||||
|
||||
|
@@ -1,96 +0,0 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class UnpackCursor {
|
||||
private Unpacker pac;
|
||||
private int offset;
|
||||
|
||||
UnpackCursor(Unpacker pac, int offset)
|
||||
{
|
||||
this.pac = pac;
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
final void advance(int length) {
|
||||
offset += length;
|
||||
}
|
||||
|
||||
final int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
public byte unpackByte() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackByte(this, offset);
|
||||
}
|
||||
|
||||
public short unpackShort() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackShort(this, offset);
|
||||
}
|
||||
|
||||
public int unpackInt() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackInt(this, offset);
|
||||
}
|
||||
|
||||
public long unpackLong() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackLong(this, offset);
|
||||
}
|
||||
|
||||
public float unpackFloat() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackFloat(this, offset);
|
||||
}
|
||||
|
||||
public double unpackDouble() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackDouble(this, offset);
|
||||
}
|
||||
|
||||
public Object unpackNull() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackNull(this, offset);
|
||||
}
|
||||
|
||||
public boolean unpackBoolean() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackBoolean(this, offset);
|
||||
}
|
||||
|
||||
public int unpackArray() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackArray(this, offset);
|
||||
}
|
||||
|
||||
public int unpackMap() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackMap(this, offset);
|
||||
}
|
||||
|
||||
public int unpackRaw() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackRaw(this, offset);
|
||||
}
|
||||
|
||||
public byte[] unpackRawBody(int length) throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackRawBody(this, offset, length);
|
||||
}
|
||||
|
||||
public String unpackString() throws IOException, MessageTypeException {
|
||||
return pac.impl.unpackString(this, offset);
|
||||
}
|
||||
|
||||
public void commit() {
|
||||
pac.setOffset(offset);
|
||||
}
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ public class UnpackIterator extends UnpackResult implements Iterator<Object> {
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
if(finished) { return true; }
|
||||
try {
|
||||
return pac.next(this);
|
||||
} catch (IOException e) {
|
||||
|
@@ -36,7 +36,6 @@ public class Unpacker implements Iterable<Object> {
|
||||
|
||||
private static final int DEFAULT_BUFFER_SIZE = 32*1024;
|
||||
|
||||
protected int offset;
|
||||
protected int parsed;
|
||||
protected int bufferReserveSize;
|
||||
protected InputStream stream;
|
||||
@@ -73,7 +72,6 @@ public class Unpacker implements Iterable<Object> {
|
||||
}
|
||||
|
||||
public Unpacker(InputStream stream, int bufferReserveSize) {
|
||||
this.offset = 0;
|
||||
this.parsed = 0;
|
||||
this.bufferReserveSize = bufferReserveSize/2;
|
||||
this.stream = stream;
|
||||
@@ -97,7 +95,7 @@ public class Unpacker implements Iterable<Object> {
|
||||
int length = buffer.remaining();
|
||||
if (length == 0) return;
|
||||
reserveBuffer(length);
|
||||
buffer.get(impl.buffer, this.offset, length);
|
||||
buffer.get(impl.buffer, impl.offset, length);
|
||||
bufferConsumed(length);
|
||||
}
|
||||
|
||||
@@ -107,7 +105,7 @@ public class Unpacker implements Iterable<Object> {
|
||||
|
||||
public void feed(byte[] buffer, int offset, int length) {
|
||||
reserveBuffer(length);
|
||||
System.arraycopy(buffer, offset, impl.buffer, this.offset, length);
|
||||
System.arraycopy(buffer, offset, impl.buffer, impl.offset, length);
|
||||
bufferConsumed(length);
|
||||
}
|
||||
|
||||
@@ -121,13 +119,12 @@ public class Unpacker implements Iterable<Object> {
|
||||
|
||||
public UnpackResult next() throws IOException, UnpackException {
|
||||
UnpackResult result = new UnpackResult();
|
||||
this.offset = impl.next(this.offset, result);
|
||||
impl.next(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean next(UnpackResult result) throws IOException, UnpackException {
|
||||
this.offset = impl.next(this.offset, result);
|
||||
return result.isFinished();
|
||||
return impl.next(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -143,17 +140,17 @@ public class Unpacker implements Iterable<Object> {
|
||||
}
|
||||
|
||||
int nextSize = impl.buffer.length * 2;
|
||||
int notParsed = impl.filled - this.offset;
|
||||
int notParsed = impl.filled - impl.offset;
|
||||
while(nextSize < require + notParsed) {
|
||||
nextSize *= 2;
|
||||
}
|
||||
|
||||
byte[] tmp = new byte[nextSize];
|
||||
System.arraycopy(impl.buffer, this.offset, tmp, 0, impl.filled - this.offset);
|
||||
System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset);
|
||||
|
||||
impl.buffer = tmp;
|
||||
impl.filled = notParsed;
|
||||
this.offset = 0;
|
||||
impl.offset = 0;
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
@@ -173,12 +170,12 @@ public class Unpacker implements Iterable<Object> {
|
||||
}
|
||||
|
||||
public boolean execute() throws UnpackException {
|
||||
int noffset = impl.execute(impl.buffer, offset, impl.filled);
|
||||
if(noffset <= offset) {
|
||||
int noffset = impl.execute(impl.buffer, impl.offset, impl.filled);
|
||||
if(noffset <= impl.offset) {
|
||||
return false;
|
||||
}
|
||||
parsed += noffset - offset;
|
||||
offset = noffset;
|
||||
parsed += noffset - impl.offset;
|
||||
impl.offset = noffset;
|
||||
return impl.isFinished();
|
||||
}
|
||||
|
||||
@@ -188,8 +185,8 @@ public class Unpacker implements Iterable<Object> {
|
||||
}
|
||||
|
||||
public int execute(byte[] buffer, int offset, int length) throws UnpackException {
|
||||
int noffset = impl.execute(buffer, offset + this.offset, length);
|
||||
this.offset = noffset - offset;
|
||||
int noffset = impl.execute(buffer, offset + impl.offset, length);
|
||||
impl.offset = noffset - offset;
|
||||
if(impl.isFinished()) {
|
||||
impl.resetState();
|
||||
}
|
||||
@@ -208,15 +205,8 @@ public class Unpacker implements Iterable<Object> {
|
||||
impl.reset();
|
||||
}
|
||||
|
||||
|
||||
public UnpackCursor begin()
|
||||
{
|
||||
return new UnpackCursor(this, offset);
|
||||
}
|
||||
|
||||
|
||||
public int getMessageSize() {
|
||||
return parsed - offset + impl.filled;
|
||||
return parsed - impl.offset + impl.filled;
|
||||
}
|
||||
|
||||
public int getParsedSize() {
|
||||
@@ -224,22 +214,72 @@ public class Unpacker implements Iterable<Object> {
|
||||
}
|
||||
|
||||
public int getNonParsedSize() {
|
||||
return impl.filled - offset;
|
||||
return impl.filled - impl.offset;
|
||||
}
|
||||
|
||||
public void skipNonparsedBuffer(int size) {
|
||||
offset += size;
|
||||
impl.offset += size;
|
||||
}
|
||||
|
||||
public void removeNonparsedBuffer() {
|
||||
impl.filled = offset;
|
||||
impl.filled = impl.offset;
|
||||
}
|
||||
|
||||
|
||||
void setOffset(int offset)
|
||||
{
|
||||
parsed += offset - this.offset;
|
||||
this.offset = offset;
|
||||
final public byte unpackByte() throws IOException, MessageTypeException {
|
||||
return impl.unpackByte();
|
||||
}
|
||||
|
||||
final public short unpackShort() throws IOException, MessageTypeException {
|
||||
return impl.unpackShort();
|
||||
}
|
||||
|
||||
final public int unpackInt() throws IOException, MessageTypeException {
|
||||
return impl.unpackInt();
|
||||
}
|
||||
|
||||
final public long unpackLong() throws IOException, MessageTypeException {
|
||||
return impl.unpackLong();
|
||||
}
|
||||
|
||||
final public float unpackFloat() throws IOException, MessageTypeException {
|
||||
return impl.unpackFloat();
|
||||
}
|
||||
|
||||
final public double unpackDouble() throws IOException, MessageTypeException {
|
||||
return impl.unpackDouble();
|
||||
}
|
||||
|
||||
final public Object unpackNull() throws IOException, MessageTypeException {
|
||||
return impl.unpackNull();
|
||||
}
|
||||
|
||||
final public boolean unpackBoolean() throws IOException, MessageTypeException {
|
||||
return impl.unpackBoolean();
|
||||
}
|
||||
|
||||
final public int unpackArray() throws IOException, MessageTypeException {
|
||||
return impl.unpackArray();
|
||||
}
|
||||
|
||||
final public int unpackMap() throws IOException, MessageTypeException {
|
||||
return impl.unpackMap();
|
||||
}
|
||||
|
||||
final public int unpackRaw() throws IOException, MessageTypeException {
|
||||
return impl.unpackRaw();
|
||||
}
|
||||
|
||||
final public byte[] unpackRawBody(int length) throws IOException, MessageTypeException {
|
||||
return impl.unpackRawBody(length);
|
||||
}
|
||||
|
||||
final public String unpackString() throws IOException, MessageTypeException {
|
||||
return impl.unpackString();
|
||||
}
|
||||
|
||||
final public Object unpackObject() throws IOException, MessageTypeException {
|
||||
return impl.unpackObject();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -105,7 +105,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeClass() throws IOException {
|
||||
line();
|
||||
line("public final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
|
||||
line("public final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
@@ -117,7 +117,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeSubclass() throws IOException {
|
||||
line();
|
||||
line("final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
|
||||
line("final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
@@ -150,7 +150,7 @@ public class ClassGenerator {
|
||||
writeConstructors();
|
||||
writeAccessors();
|
||||
writePackFunction();
|
||||
writeMergeFunction();
|
||||
writeConvertFunction();
|
||||
writeFactoryFunction();
|
||||
}
|
||||
|
||||
@@ -184,11 +184,11 @@ public class ClassGenerator {
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void writeMergeFunction() throws IOException {
|
||||
private void writeConvertFunction() throws IOException {
|
||||
line();
|
||||
line("@Override");
|
||||
line("@SuppressWarnings(\"unchecked\")");
|
||||
line("public void messageMerge(Object obj) throws MessageTypeException");
|
||||
line("public void messageConvert(Object obj) throws MessageTypeException");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line("Object[] _source = ((List)obj).toArray();");
|
||||
|
@@ -59,8 +59,8 @@ public class SpecificClassSchema extends ClassSchema {
|
||||
cacheConstructor();
|
||||
}
|
||||
try {
|
||||
MessageMergeable o = (MessageMergeable)constructorCache.newInstance((Object[])null);
|
||||
o.messageMerge(obj);
|
||||
MessageConvertable o = (MessageConvertable)constructorCache.newInstance((Object[])null);
|
||||
o.messageConvert(obj);
|
||||
return o;
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
|
||||
|
@@ -8,12 +8,6 @@ import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestDirectConversion {
|
||||
private UnpackCursor prepareCursor(ByteArrayOutputStream out) {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
return upk.begin();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInt() throws Exception {
|
||||
testInt(0);
|
||||
@@ -28,9 +22,9 @@ public class TestDirectConversion {
|
||||
public void testInt(int val) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(val);
|
||||
UnpackCursor c = prepareCursor(out);
|
||||
assertEquals(val, c.unpackInt());
|
||||
c.commit();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
assertEquals(val, upk.unpackInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -51,14 +45,14 @@ public class TestDirectConversion {
|
||||
public void testFloat(float val) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(val);
|
||||
UnpackCursor c = prepareCursor(out);
|
||||
float f = c.unpackFloat();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
float f = upk.unpackFloat();
|
||||
if(Float.isNaN(val)) {
|
||||
assertTrue(Float.isNaN(f));
|
||||
} else {
|
||||
assertEquals(val, f, 10e-10);
|
||||
}
|
||||
c.commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,23 +73,23 @@ public class TestDirectConversion {
|
||||
public void testDouble(double val) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(val);
|
||||
UnpackCursor c = prepareCursor(out);
|
||||
double f = c.unpackDouble();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
double f = upk.unpackDouble();
|
||||
if(Double.isNaN(val)) {
|
||||
assertTrue(Double.isNaN(f));
|
||||
} else {
|
||||
assertEquals(val, f, 10e-10);
|
||||
}
|
||||
c.commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNil() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).packNil();
|
||||
UnpackCursor c = prepareCursor(out);
|
||||
assertEquals(null, c.unpackNull());
|
||||
c.commit();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
assertEquals(null, upk.unpackNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -106,9 +100,9 @@ public class TestDirectConversion {
|
||||
public void testBoolean(boolean val) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(val);
|
||||
UnpackCursor c = prepareCursor(out);
|
||||
assertEquals(val, c.unpackBoolean());
|
||||
c.commit();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
assertEquals(val, upk.unpackBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,9 +139,9 @@ public class TestDirectConversion {
|
||||
public void testString(String val) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(val);
|
||||
UnpackCursor c = prepareCursor(out);
|
||||
assertEquals(val, c.unpackString());
|
||||
c.commit();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker upk = new Unpacker(in);
|
||||
assertEquals(val, upk.unpackString());
|
||||
}
|
||||
|
||||
// FIXME container types
|
||||
|
Reference in New Issue
Block a user