diff --git a/src/main/java/org/nustaq/serialization/coders/FSTJsonDecoder.java b/src/main/java/org/nustaq/serialization/coders/FSTJsonDecoder.java index 9562a85d..15b9a508 100644 --- a/src/main/java/org/nustaq/serialization/coders/FSTJsonDecoder.java +++ b/src/main/java/org/nustaq/serialization/coders/FSTJsonDecoder.java @@ -228,7 +228,10 @@ public void resetToCopyOf(byte[] bytes, int off, int len) { throw new RuntimeException("not supported"); byte b[] = new byte[len]; System.arraycopy(bytes,off,b,0,len); - fstInput = new FSTInputStream(b); + if ( fstInput == null ) + fstInput = new FSTInputStream(b); + else + fstInput.resetForReuse(bytes,len); try { createParser(); } catch (IOException e) { @@ -244,7 +247,12 @@ public void createParser() throws IOException { @Override public void resetWith(byte[] bytes, int len) { - fstInput = new FSTInputStream(bytes,0,len); + if ( fstInput == null ) { + fstInput = new FSTInputStream(bytes,0,len); + } + else { + fstInput.resetForReuse(bytes,len); + } try { createParser(); } catch (IOException e) { diff --git a/src/main/java/org/nustaq/serialization/coders/FSTJsonEncoder.java b/src/main/java/org/nustaq/serialization/coders/FSTJsonEncoder.java index 66b24846..0e8d7e77 100644 --- a/src/main/java/org/nustaq/serialization/coders/FSTJsonEncoder.java +++ b/src/main/java/org/nustaq/serialization/coders/FSTJsonEncoder.java @@ -354,14 +354,15 @@ protected String classToString(Class clz) { @Override public void writeAttributeName(FSTClazzInfo.FSTFieldInfo subInfo) { try { - if ( gen.getOutputContext().inArray() ) - gen.writeString(subInfo.getName()); + SerializedString bufferedName = (SerializedString) subInfo.getBufferedName(); + if ( bufferedName == null ) { + bufferedName = new SerializedString(subInfo.getName()); + subInfo.setBufferedName(bufferedName); + } + if ( gen.getOutputContext().inArray() ) { + gen.writeString(bufferedName); + } else { - SerializedString bufferedName = (SerializedString) subInfo.getBufferedName(); - if ( bufferedName == null ) { - bufferedName = new SerializedString(subInfo.getName()); - subInfo.setBufferedName(bufferedName); - } gen.writeFieldName(bufferedName); } } catch (IOException e) { diff --git a/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java b/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java index 6e06b2ce..56579525 100644 --- a/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java +++ b/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java @@ -448,11 +448,12 @@ public void resetToCopyOf(byte[] bytes, int off, int len) { @Override public void resetWith(byte[] bytes, int len) { clnames.clear(); - input.reset(); - input.count = len; - input.buf = bytes; - input.pos = 0; - input.byteBacked = true; + input.resetForReuse(bytes,len); +// input.reset(); +// input.count = len; +// input.buf = bytes; +// input.pos = 0; +// input.byteBacked = true; } @Override diff --git a/src/main/java/org/nustaq/serialization/util/FSTInputStream.java b/src/main/java/org/nustaq/serialization/util/FSTInputStream.java index 9c296aa4..485db944 100644 --- a/src/main/java/org/nustaq/serialization/util/FSTInputStream.java +++ b/src/main/java/org/nustaq/serialization/util/FSTInputStream.java @@ -33,13 +33,22 @@ public final class FSTInputStream extends InputStream { public int pos; public int count; // avaiable valid read bytes InputStream in; - boolean fullyRead = false; + boolean fullyRead = false; // true if input source has been read til end public boolean byteBacked = false; public FSTInputStream(InputStream in) { initFromStream(in); } + public void resetForReuse( byte b[], int length ) { + reset(); + count = length; + buf = b; + pos = 0; + byteBacked = true; + fullyRead = true; + } + public void initFromStream(InputStream in) { fullyRead = false; byteBacked = false; @@ -77,7 +86,7 @@ public void readNextChunk(InputStream in) { } public void ensureCapacity(int siz) { - if (buf.length < siz) { + if (buf.length < siz && ! fullyRead) { byte newBuf[] = new byte[siz]; System.arraycopy(buf, 0, newBuf, 0, buf.length); buf = newBuf; @@ -110,12 +119,10 @@ public int read() { } public int read(byte b[], int off, int len) { - if (fullyRead) + if (isFullyRead()) return -1; - while (pos + len >= count) { + while (! fullyRead && pos + len >= count) { readNextChunk(in); - if (fullyRead) - break; } int avail = count - pos; if (len > avail) { diff --git a/src/test/ser/Play.java b/src/test/ser/Play.java index 35be437e..d4e05f42 100644 --- a/src/test/ser/Play.java +++ b/src/test/ser/Play.java @@ -56,8 +56,8 @@ public static void main(String[] args) { byte[] bytes = conf.asByteArray(p); Object deser = conf.asObject(bytes); System.out.println(DeepEquals.deepEquals(p,deser)); -// while( true ) -// sb(conf); + while( true ) + sb(conf); } protected static void sb(FSTConfiguration conf) {