├── .gitignore ├── .gradle └── 1.6 │ └── taskArtifacts │ ├── cache.properties │ ├── cache.properties.lock │ ├── fileHashes.bin │ ├── fileSnapshots.bin │ ├── outputFileStates.bin │ └── taskArtifacts.bin ├── .travis.yml ├── LICENSE.md ├── README.md ├── fst.png ├── img ├── array.PNG ├── chunked.PNG ├── comlexheapobject.PNG ├── singlebyte.PNG ├── structiter.html └── structlayout.PNG ├── lib ├── jackson-core-2.5.3.jar ├── java-util-1.13.3.jar ├── javassist-3.19.0-GA.jar ├── junit-4.7.jar └── objenesis-2.1.jar ├── pom.xml ├── release.sh ├── src ├── main │ ├── java │ │ └── org │ │ │ └── nustaq │ │ │ ├── kson │ │ │ ├── ArgTypes.java │ │ │ ├── JSonSerializer.java │ │ │ ├── Kson.java │ │ │ ├── KsonArgTypesResolver.java │ │ │ ├── KsonCharInput.java │ │ │ ├── KsonCharOutput.java │ │ │ ├── KsonDeserializer.java │ │ │ ├── KsonParseException.java │ │ │ ├── KsonSerializer.java │ │ │ ├── KsonStringCharInput.java │ │ │ ├── KsonStringOutput.java │ │ │ └── KsonTypeMapper.java │ │ │ ├── logging │ │ │ └── FSTLogger.java │ │ │ ├── net │ │ │ ├── TCPObjectServer.java │ │ │ └── TCPObjectSocket.java │ │ │ ├── offheap │ │ │ ├── BinaryQueue.java │ │ │ ├── FSTAsciiStringOffheapMap.java │ │ │ ├── FSTBinaryOffheapMap.java │ │ │ ├── FSTCodedOffheapMap.java │ │ │ ├── FSTLongOffheapMap.java │ │ │ ├── FSTSerializedOffheapMap.java │ │ │ ├── FSTUTFStringOffheapMap.java │ │ │ ├── FreeList.java │ │ │ ├── OffHeapByteTree.java │ │ │ └── bytez │ │ │ │ ├── BasicBytez.java │ │ │ │ ├── ByteSink.java │ │ │ │ ├── ByteSource.java │ │ │ │ ├── Bytez.java │ │ │ │ ├── BytezAllocator.java │ │ │ │ ├── VolatileByteAccess.java │ │ │ │ ├── bytesource │ │ │ │ ├── AsciiStringByteSource.java │ │ │ │ ├── ByteArrayByteSource.java │ │ │ │ ├── BytezByteSource.java │ │ │ │ ├── CutAsciiStringByteSource.java │ │ │ │ ├── LeftCutStringByteSource.java │ │ │ │ └── UTFStringByteSource.java │ │ │ │ ├── malloc │ │ │ │ ├── MMFBytez.java │ │ │ │ ├── MallocBytez.java │ │ │ │ ├── MallocBytezAllocator.java │ │ │ │ └── MemoryBytez.java │ │ │ │ ├── niobuffers │ │ │ │ └── ByteBufferBasicBytez.java │ │ │ │ └── onheap │ │ │ │ ├── HeapBytez.java │ │ │ │ └── HeapBytezAllocator.java │ │ │ └── serialization │ │ │ ├── FSTBasicObjectSerializer.java │ │ │ ├── FSTClassInstantiator.java │ │ │ ├── FSTClazzInfo.java │ │ │ ├── FSTClazzInfoRegistry.java │ │ │ ├── FSTClazzLineageInfo.java │ │ │ ├── FSTClazzNameRegistry.java │ │ │ ├── FSTConfiguration.java │ │ │ ├── FSTCrossPlatformSerialzer.java │ │ │ ├── FSTDecoder.java │ │ │ ├── FSTDefaultClassInstantiator.java │ │ │ ├── FSTEncoder.java │ │ │ ├── FSTObjectInput.java │ │ │ ├── FSTObjectInputNoShared.java │ │ │ ├── FSTObjectOutput.java │ │ │ ├── FSTObjectOutputNoShared.java │ │ │ ├── FSTObjectRegistry.java │ │ │ ├── FSTObjectSerializer.java │ │ │ ├── FSTObjenesisInstantiator.java │ │ │ ├── FSTSerialisationListener.java │ │ │ ├── FSTSerializerRegistry.java │ │ │ ├── FSTSerializerRegistryDelegate.java │ │ │ ├── VersionConflictListener.java │ │ │ ├── annotations │ │ │ ├── AnonymousTransient.java │ │ │ ├── Conditional.java │ │ │ ├── Flat.java │ │ │ ├── OneOf.java │ │ │ ├── Predict.java │ │ │ ├── Serialize.java │ │ │ ├── Transient.java │ │ │ └── Version.java │ │ │ ├── coders │ │ │ ├── FSTBytezDecoder.java │ │ │ ├── FSTBytezEncoder.java │ │ │ ├── FSTJsonDecoder.java │ │ │ ├── FSTJsonEncoder.java │ │ │ ├── FSTJsonFieldNames.java │ │ │ ├── FSTMinBinDecoder.java │ │ │ ├── FSTMinBinEncoder.java │ │ │ ├── FSTStreamDecoder.java │ │ │ ├── FSTStreamEncoder.java │ │ │ ├── JSONAsString.java │ │ │ └── Unknown.java │ │ │ ├── minbin │ │ │ ├── GenMeta.java │ │ │ ├── MBIn.java │ │ │ ├── MBObject.java │ │ │ ├── MBOut.java │ │ │ ├── MBPrinter.java │ │ │ ├── MBRef.java │ │ │ ├── MBSequence.java │ │ │ ├── MBTags.java │ │ │ └── MinBin.java │ │ │ ├── serializers │ │ │ ├── FSTArrayListSerializer.java │ │ │ ├── FSTBigIntegerSerializer.java │ │ │ ├── FSTBigNumberSerializers.java │ │ │ ├── FSTBitSetSerializer.java │ │ │ ├── FSTCPEnumSetSerializer.java │ │ │ ├── FSTCPThrowableSerializer.java │ │ │ ├── FSTClassSerializer.java │ │ │ ├── FSTCollectionSerializer.java │ │ │ ├── FSTDateSerializer.java │ │ │ ├── FSTEnumSetSerializer.java │ │ │ ├── FSTJSonSerializers.java │ │ │ ├── FSTJSonUnmodifiableCollectionSerializer.java │ │ │ ├── FSTJSonUnmodifiableMapSerializer.java │ │ │ ├── FSTMapSerializer.java │ │ │ ├── FSTProxySerializer.java │ │ │ ├── FSTStringBufferSerializer.java │ │ │ ├── FSTStringBuilderSerializer.java │ │ │ ├── FSTStringSerializer.java │ │ │ ├── FSTThrowableSerializer.java │ │ │ └── FSTTimestampSerializer.java │ │ │ ├── simpleapi │ │ │ ├── DefaultCoder.java │ │ │ ├── FSTBufferTooSmallException.java │ │ │ ├── FSTCoder.java │ │ │ ├── MinBinCoder.java │ │ │ ├── OffHeapCoder.java │ │ │ └── OnHeapCoder.java │ │ │ └── util │ │ │ ├── DefaultFSTInt2ObjectMap.java │ │ │ ├── DefaultFSTInt2ObjectMapFactory.java │ │ │ ├── FSTIdentity2IdMap.java │ │ │ ├── FSTInputStream.java │ │ │ ├── FSTInt2IntMap.java │ │ │ ├── FSTInt2ObjectMap.java │ │ │ ├── FSTInt2ObjectMapFactory.java │ │ │ ├── FSTMap.java │ │ │ ├── FSTObject2IntMap.java │ │ │ ├── FSTOrderedConcurrentJobExecutor.java │ │ │ ├── FSTOutputStream.java │ │ │ └── FSTUtil.java │ └── web │ │ ├── index.html │ │ └── minbin │ │ ├── minbin-2.19.js │ │ └── minbin-2.20.js ├── test │ ├── jdk_bug │ │ └── foo │ │ │ ├── Test.java │ │ │ └── bean │ │ │ ├── AnImpl.java │ │ │ ├── SomeAbstract.java │ │ │ ├── TestBean.java │ │ │ ├── TestBeanAncestor.java │ │ │ ├── UtilFactory.java │ │ │ └── UtilFactoryAncestor.java │ ├── json │ │ ├── JsonTypOrder.java │ │ ├── StringEnc.java │ │ ├── UnknownTest.java │ │ ├── testjson.json │ │ └── unknowntest.json │ ├── kson │ │ ├── KsonCustomer.java │ │ ├── KsonPhoneNumber.java │ │ ├── KsonTest.java │ │ ├── test.json │ │ └── test.kson │ ├── net │ │ └── TestObjectServer.java │ ├── offheap │ │ ├── BinaryQueueTest.java │ │ └── LongMapTest.java │ └── ser │ │ ├── BasicAndroidTest.java │ │ ├── BasicBinaryUnsafeTest.java │ │ ├── BasicFSTTest.java │ │ ├── BasicJsonTest.java │ │ ├── BasicMinBinTest.java │ │ ├── BasicReuseTest.java │ │ ├── Basics.java │ │ ├── Bug34.java │ │ ├── CarBench.java │ │ ├── CarBenchBinarized.java │ │ ├── FSTJavaAssistTest.java │ │ ├── FloatBench.java │ │ ├── ForceSerIssue89.java │ │ ├── Git64.java │ │ ├── Git67.java │ │ ├── Git80.java │ │ ├── GitHub159.java │ │ ├── GitHub39.java │ │ ├── Github61.java │ │ ├── I177.java │ │ ├── I178.java │ │ ├── I79.java │ │ ├── Issue91.java │ │ ├── JSonUnmodifiableTest.java │ │ ├── LineageTest.java │ │ ├── Map1Test.java │ │ ├── MapTest.java │ │ ├── MinBinTest.java │ │ ├── OverflowTest.java │ │ ├── Play.java │ │ ├── RawMemTest.java │ │ ├── ReadResolve.java │ │ ├── SetupAlloc.java │ │ ├── SpecialsTest.java │ │ ├── TestFastSerialization.java │ │ ├── TestQ.java │ │ ├── TestWriteReplaceInList.java │ │ ├── UmodifiableTest.java │ │ ├── WeirdArrays.java │ │ ├── androidbitset │ │ ├── AndroidBItSet.java │ │ ├── AndroidBigInt.java │ │ ├── AndroidBitSetTest.java │ │ ├── JDKBigInt.java │ │ └── JDKBitSet.java │ │ ├── bighm │ │ └── BigObjTest.java │ │ ├── externalizable │ │ └── ExternalizableTestClass.java │ │ ├── hzcastblog │ │ ├── Order.java │ │ ├── OrderLine.java │ │ └── RunTest.java │ │ ├── issue52 │ │ └── I52.java │ │ ├── jsonbench │ │ └── JSonBench.java │ │ ├── offheaplatency │ │ ├── MktDataDispatcher.java │ │ ├── MktDataEvent.java │ │ ├── MyFSTSerializer.java │ │ └── TestFSTSerializer.java │ │ ├── security │ │ └── NestedHS.java │ │ ├── serializerdelegate │ │ ├── CondTest.java │ │ └── SerializerDelegateTest.java │ │ ├── serializers │ │ ├── FSTEnumSetSerializerTest.java │ │ ├── FSTJSonUnmodifiableCollectionSerializerTest.java │ │ ├── FSTJSonUnmodifiableMapSerializerTest.java │ │ ├── FSTStreamEncoderTest.java │ │ └── Github204.java │ │ └── unit │ │ └── tests │ │ ├── TestFastSerializationBean1.java │ │ ├── TestFastSerializationBean2.java │ │ ├── TestFastSerializationList.java │ │ └── externalizable │ │ ├── BeanTestClass1.java │ │ ├── BeanTestClass2.java │ │ └── ExternalizableTestClass.java └── test_nojunit │ └── java │ └── de │ └── ruedigermoeller │ └── heapofftest │ ├── AnotherOffheapThingy.java │ ├── FSTGCMark.java │ ├── SimpleHistogram.java │ └── structs │ ├── LargeIntArray.java │ ├── TestData.java │ ├── TestDate.java │ ├── TestInstrument.java │ ├── TestInstrumentLeg.java │ ├── TestMarket.java │ ├── TestTimeZone.java │ └── single │ ├── BasicArrays.java │ └── BasicPrimitives.java └── test.json /.gradle/1.6/taskArtifacts/cache.properties: -------------------------------------------------------------------------------- 1 | #Tue Mar 04 19:21:57 CET 2014 2 | -------------------------------------------------------------------------------- /.gradle/1.6/taskArtifacts/cache.properties.lock: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /.gradle/1.6/taskArtifacts/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/.gradle/1.6/taskArtifacts/fileHashes.bin -------------------------------------------------------------------------------- /.gradle/1.6/taskArtifacts/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/.gradle/1.6/taskArtifacts/fileSnapshots.bin -------------------------------------------------------------------------------- /.gradle/1.6/taskArtifacts/outputFileStates.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/.gradle/1.6/taskArtifacts/outputFileStates.bin -------------------------------------------------------------------------------- /.gradle/1.6/taskArtifacts/taskArtifacts.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/.gradle/1.6/taskArtifacts/taskArtifacts.bin -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | - oraclejdk7 5 | - openjdk7 6 | -------------------------------------------------------------------------------- /fst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/fst.png -------------------------------------------------------------------------------- /img/array.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/img/array.PNG -------------------------------------------------------------------------------- /img/chunked.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/img/chunked.PNG -------------------------------------------------------------------------------- /img/comlexheapobject.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/img/comlexheapobject.PNG -------------------------------------------------------------------------------- /img/singlebyte.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/img/singlebyte.PNG -------------------------------------------------------------------------------- /img/structlayout.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/img/structlayout.PNG -------------------------------------------------------------------------------- /lib/jackson-core-2.5.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/lib/jackson-core-2.5.3.jar -------------------------------------------------------------------------------- /lib/java-util-1.13.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/lib/java-util-1.13.3.jar -------------------------------------------------------------------------------- /lib/javassist-3.19.0-GA.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/lib/javassist-3.19.0-GA.jar -------------------------------------------------------------------------------- /lib/junit-4.7.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/lib/junit-4.7.jar -------------------------------------------------------------------------------- /lib/objenesis-2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/lib/objenesis-2.1.jar -------------------------------------------------------------------------------- /release.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | export PW=$1 4 | export GPG_TTY=$(tty) 5 | 6 | echo $1 7 | 8 | mvn clean package -Dmaven.test.skip=true gpg:sign -Dgpg.passphrase=$PW 9 | cd target 10 | rm *-jar-with* 11 | rm *stale-data.txt 12 | rm -r */ 13 | jar -cf bundle.jar * 14 | cd .. 15 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/ArgTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.kson; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * tagging to advise kson/json serializer to guess types from a method signature 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.FIELD) 29 | 30 | public @interface ArgTypes { 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/KsonArgTypesResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.kson; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | /** 23 | * Created by ruedi on 14.08.2014. 24 | */ 25 | public interface KsonArgTypesResolver { 26 | Class[] getArgTypes(Class outerClass, List currentParse); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/KsonCharInput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.kson; 18 | 19 | public interface KsonCharInput { 20 | /** 21 | * @return char or -1 for eof 22 | */ 23 | int readChar(); 24 | int peekChar(); 25 | int position(); 26 | int back(int num); 27 | boolean isEof(); 28 | 29 | String getString(int pos, int length); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/KsonCharOutput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.kson; 18 | 19 | public interface KsonCharOutput { 20 | 21 | public void writeChar( char c ); 22 | public void writeString( String s ); 23 | 24 | char lastChar(); 25 | 26 | void back(int i); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/KsonParseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.kson; 18 | 19 | import java.util.Stack; 20 | 21 | /** 22 | * Created by ruedi on 26.12.13. 23 | */ 24 | public class KsonParseException extends RuntimeException { 25 | 26 | public KsonParseException(String ms, KsonCharInput in) { 27 | super(ms+":"+in.getString(in.position()-20,20)+getStackString(in)); 28 | } 29 | 30 | private static String getStackString(KsonCharInput in) { 31 | if ( in instanceof KsonStringCharInput && ((KsonStringCharInput) in).stack != null) { 32 | final Stack stack = ((KsonStringCharInput) in).stack; 33 | StringBuilder res = new StringBuilder("\n\n"); 34 | for (int i = stack.size()-1; i >= 0; i--) { 35 | KsonDeserializer.ParseStep parseStep = stack.get(i); 36 | res.append(" ").append(parseStep).append("\n"); 37 | } 38 | return res.toString(); 39 | } 40 | return null; 41 | } 42 | 43 | public KsonParseException(String s, KsonCharInput in, Throwable ex) { 44 | super(s+":"+in.getString(in.position()-20,20)+getStackString(in),ex); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/KsonStringCharInput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.kson; 17 | 18 | import java.util.Stack; 19 | 20 | /** 21 | * implementation of char input on top of a String 22 | */ 23 | public class KsonStringCharInput implements KsonCharInput { 24 | CharSequence s; 25 | int pos; 26 | int end; 27 | Stack stack; 28 | 29 | public KsonStringCharInput(CharSequence s) { 30 | this.s = s; 31 | pos = 0; 32 | end = s.length(); 33 | } 34 | 35 | public KsonStringCharInput(String s, int pos, int len) { 36 | this.s = s; 37 | this.pos = pos; 38 | this.end = pos+len; 39 | } 40 | 41 | /** 42 | * @return char or -1 for eof 43 | */ 44 | @Override 45 | public int readChar() { 46 | if ( pos >= end ) 47 | return -1; 48 | return s.charAt(pos++); 49 | } 50 | 51 | @Override 52 | public int peekChar() { 53 | if ( pos >= end ) 54 | return -1; 55 | return s.charAt(pos); 56 | } 57 | 58 | @Override 59 | public int position() { 60 | return pos; 61 | } 62 | 63 | @Override 64 | public int back(int num) { 65 | pos -= num; return pos; 66 | } 67 | 68 | @Override 69 | public boolean isEof() { 70 | return pos >= s.length(); 71 | } 72 | 73 | @Override 74 | public String getString(int pos, int length) { 75 | return s.subSequence(Math.max(0,pos),Math.min(s.length(),pos+length)).toString(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/kson/KsonStringOutput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.kson; 18 | 19 | /** 20 | * 21 | * Created by ruedi on 07.08.2014. 22 | */ 23 | public class KsonStringOutput implements KsonCharOutput { 24 | 25 | StringBuilder builder = new StringBuilder(); 26 | 27 | @Override 28 | public void writeChar(char c) { 29 | builder.append(c); 30 | } 31 | 32 | @Override 33 | public void writeString(String s) { 34 | builder.append(s); 35 | } 36 | 37 | @Override 38 | public char lastChar() { 39 | if ( builder.length() > 0 ) { 40 | return builder.charAt(builder.length()-1); 41 | } 42 | return 0; 43 | } 44 | 45 | @Override 46 | public void back(int i) { 47 | builder.setLength(builder.length()-i); 48 | } 49 | 50 | public StringBuilder getBuilder() { 51 | return builder; 52 | } 53 | 54 | public void setBuilder(StringBuilder builder) { 55 | this.builder = builder; 56 | } 57 | 58 | public String toString() { 59 | return builder.toString(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/logging/FSTLogger.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.logging; 2 | 3 | public final class FSTLogger { 4 | 5 | private static Log binding; 6 | 7 | /** 8 | * Initialize FST logging with a {@link Log} binding. 9 | * @param log 10 | */ 11 | public static void setBinding(Log log) { 12 | binding = log; 13 | } 14 | 15 | public static FSTLogger getLogger(Class clazz) { 16 | return new FSTLogger(clazz.getName()); 17 | } 18 | 19 | private final String loggerName; 20 | private FSTLogger(String loggerName) { 21 | this.loggerName = loggerName; 22 | } 23 | 24 | public void log(Level level, String message, /* nullable */ Throwable ex) { 25 | if (binding != null) { 26 | binding.log(loggerName, level, message, ex); 27 | } 28 | } 29 | 30 | public enum Level { 31 | TRACE, 32 | DEBUG, 33 | INFO, 34 | WARN, 35 | ERROR 36 | } 37 | 38 | public interface Log { 39 | 40 | void log(String name, Level level, String message, /* nullable */ Throwable ex); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/FSTLongOffheapMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap; 17 | 18 | import org.nustaq.offheap.bytez.ByteSource; 19 | import org.nustaq.offheap.bytez.bytesource.ByteArrayByteSource; 20 | import org.nustaq.serialization.simpleapi.DefaultCoder; 21 | import org.nustaq.serialization.simpleapi.FSTCoder; 22 | 23 | /** 24 | * Created by ruedi on 15.11.14. 25 | * 26 | * Same as FSTAsciiStringOffheapMap, but uses Long as key 27 | * see FSTAsciiStringOffheapMap for doc. 28 | */ 29 | public class FSTLongOffheapMap extends FSTSerializedOffheapMap { 30 | 31 | public FSTLongOffheapMap(long sizeBytes, int numberOfElems, FSTCoder coder) { 32 | super(8, sizeBytes, numberOfElems, coder); 33 | } 34 | 35 | public FSTLongOffheapMap(String mappedFile, long sizeMemBytes, int numberOfElems, FSTCoder coder) throws Exception { 36 | super(mappedFile, 8, sizeMemBytes, numberOfElems, coder); 37 | } 38 | 39 | public FSTLongOffheapMap(long sizeBytes, int numberOfElems) { 40 | super(8, sizeBytes, numberOfElems, new DefaultCoder()); 41 | } 42 | 43 | public FSTLongOffheapMap(String mappedFile, long sizeMemBytes, int numberOfElems) throws Exception { 44 | super(mappedFile, 8, sizeMemBytes, numberOfElems, new DefaultCoder()); 45 | } 46 | 47 | byte longbyte[] = new byte[8]; 48 | ByteSource tmpKey = new ByteArrayByteSource(longbyte); 49 | 50 | @Override 51 | public ByteSource encodeKey(Long key) { 52 | int count = 0; 53 | long v = key; 54 | longbyte[count++] = (byte) (v >>> 0); 55 | longbyte[count++] = (byte) (v >>> 8); 56 | longbyte[count++] = (byte) (v >>> 16); 57 | longbyte[count++] = (byte) (v >>> 24); 58 | longbyte[count++] = (byte) (v >>> 32); 59 | longbyte[count++] = (byte) (v >>> 40); 60 | longbyte[count++] = (byte) (v >>> 48); 61 | longbyte[count++] = (byte) (v >>> 56); 62 | return tmpKey; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/FSTUTFStringOffheapMap.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.offheap; 2 | 3 | import org.nustaq.offheap.bytez.ByteSource; 4 | import org.nustaq.offheap.bytez.bytesource.UTFStringByteSource; 5 | import org.nustaq.serialization.simpleapi.DefaultCoder; 6 | import org.nustaq.serialization.simpleapi.FSTCoder; 7 | 8 | /** 9 | * Created by ruedi on 05/08/15. 10 | */ 11 | public class FSTUTFStringOffheapMap extends FSTSerializedOffheapMap { 12 | 13 | /** 14 | * see other mem constructor, additional allows to insert a coder. 15 | * Useful to preregister classes (speed+size gain, no classnames written!). 16 | * E.g. new DefaultCoder(MyValue.class, MyOtherValue.class, .. ) 17 | */ 18 | public FSTUTFStringOffheapMap(int keyLen, long sizeMemBytes, int numberOfEleems, FSTCoder coder) { 19 | super(keyLen, sizeMemBytes, numberOfEleems, coder); 20 | } 21 | 22 | /** 23 | * 24 | * @param keyLen - maximum len of a key 25 | * @param sizeMemBytes - size of memory (not bigger than OS heap 26 | * @param numberOfEleems - estimation on number of key-value pairs (autogrow, just educated guess) 27 | */ 28 | public FSTUTFStringOffheapMap(int keyLen, long sizeMemBytes, int numberOfEleems) { 29 | super(keyLen, sizeMemBytes, numberOfEleems, new DefaultCoder()); 30 | } 31 | 32 | /** 33 | * see other mem mapped file constructor + additional allows to insert a coder. 34 | * Useful to preregister classes (speed+size gain, no classnames written!). 35 | * E.g. new DefaultCoder(MyValue.class, MyOtherValue.class, .. ) 36 | */ 37 | public FSTUTFStringOffheapMap(String mappedFile, int keyLen, long sizeMemBytes, int numberOfElems,FSTCoder coder) throws Exception { 38 | super(mappedFile, keyLen, sizeMemBytes, numberOfElems, coder); 39 | } 40 | 41 | /** 42 | * create a memory mapped offheap hashmap 43 | * @param file - the file location. If file exists, it will be loaded 44 | * Attention: in case classes changed you need to delete files as 45 | * objects won't be compatible (except you made use of fst versioning features) 46 | * @param keylen - the maximum len of a key 47 | * @param size - max size of file (can be GB, check OS settings to avoid eager write-to-disk) 48 | * @param numelem - estimated number of key-value pairs (will auto-grow, just educated guess) 49 | * @throws Exception 50 | */ 51 | public FSTUTFStringOffheapMap(String file, int keylen, long size, int numelem) throws Exception { 52 | this(file,keylen,size,numelem,new DefaultCoder()); 53 | } 54 | 55 | public ByteSource encodeKey(String key) { 56 | if ( key.length() > keyLen ) 57 | throw new RuntimeException("key too long: '"+key+"' maxlen:"+keyLen); 58 | return new UTFStringByteSource(key).padLeft(keyLen); 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/FreeList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap; 17 | 18 | /** 19 | * Created by ruedi on 04.07.14. 20 | */ 21 | public class FreeList { 22 | 23 | long flists[][] = new long[32][]; // 16, 32, 64, 128, 256, 512, .. 24 | int flIndex[] = new int[32]; 25 | 26 | // get freelist index for given length 27 | int computeListIndex( int len ) { 28 | int powIndex = 32-Integer.numberOfLeadingZeros(len-1); 29 | return powIndex; 30 | } 31 | 32 | int computeLen( int len ) { 33 | int powIndex = 32-Integer.numberOfLeadingZeros(len-1); 34 | return 1< 0) { 40 | return flists[index][--flIndex[index]]; 41 | } 42 | return 0; 43 | } 44 | 45 | public void addToFree(long offset, int len) { 46 | int index = computeListIndex(len); 47 | if ( flists[index] == null ) 48 | flists[index] = new long[500]; 49 | if ( flIndex[index] >= flists[index].length ) { 50 | long newFree[] = new long[Math.min(flists[index].length * 2, Integer.MAX_VALUE - 1)]; 51 | System.arraycopy(flists[index], 0, newFree, 0, flIndex[index]); 52 | flists[index] = newFree; 53 | } 54 | flists[index][flIndex[index]++] = offset; 55 | } 56 | 57 | public static void main(String arg[]) { 58 | FreeList li = new FreeList(); 59 | li.computeListIndex(13); 60 | li.computeListIndex(16); 61 | li.computeListIndex(15); 62 | li.computeListIndex(17); 63 | li.computeListIndex(99); 64 | li.computeListIndex(777); 65 | li.computeListIndex(127); 66 | System.out.println((li.computeListIndex(777000))); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/ByteSink.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.offheap.bytez; 2 | 3 | /** 4 | * Created by moelrue on 5/5/15. 5 | */ 6 | public interface ByteSink { 7 | 8 | public void put(long byteIndex, byte value); 9 | public long length(); 10 | public void copyTo(BasicBytez other, long otherByteIndex, long myByteIndex, long lenBytes); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/ByteSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap.bytez; 17 | 18 | /** 19 | * Created by ruedi on 27.06.14. 20 | */ 21 | public interface ByteSource { 22 | public byte get(long index); 23 | public long length(); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/Bytez.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.offheap.bytez; 18 | 19 | /** 20 | * abstraction of byte arrays similar to ByteBuffer without the need to create temp objects in order to get long,int,.. views 21 | * additionally supports volatile read/write (for byte[] based backing buffers only !) 22 | */ 23 | public interface Bytez extends BasicBytez { 24 | 25 | public Bytez slice(long off, int len); 26 | 27 | public boolean compareAndSwapInt( long offset, int expect, int newVal); 28 | public boolean compareAndSwapLong( long offset, long expect, long newVal); 29 | 30 | public byte[] toBytes(long startIndex, int len); 31 | /** 32 | * @return return the underlying byte array, not supported by MallocBytez !. Use getArr to extract data by copy instead. 33 | */ 34 | public byte[] asByteArray(); 35 | 36 | /** 37 | * @return the start index inside the byte array returned by asByteArray, not supported by MallocBytez 38 | */ 39 | public int getBAOffsetIndex(); 40 | /** 41 | * @return the length inside the byte array returned by asByteArray, not supported by MallocBytez 42 | */ 43 | public int getBALength(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/BytezAllocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.offheap.bytez; 18 | 19 | /** 20 | * Date: 16.11.13 21 | * Time: 12:37 22 | * 23 | * An Allocator instantiates byte sources and can free them again (if necessary) 24 | */ 25 | public interface BytezAllocator { 26 | 27 | public Bytez alloc(long len); 28 | public void free( Bytez bytes ); 29 | public void freeAll(); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/VolatileByteAccess.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.offheap.bytez; 2 | 3 | public interface VolatileByteAccess { 4 | 5 | public boolean getBoolVolatile(long byteIndex); 6 | public byte getVolatile(long byteIndex); 7 | public char getCharVolatile(long byteIndex); 8 | public short getShortVolatile(long byteIndex); 9 | public int getIntVolatile(long byteIndex); 10 | public long getLongVolatile(long byteIndex); 11 | public float getFloatVolatile(long byteIndex); 12 | public double getDoubleVolatile(long byteIndex); 13 | 14 | public void putBoolVolatile(long byteIndex, boolean value); 15 | public void putVolatile(long byteIndex, byte value); 16 | public void putCharVolatile(long byteIndex, char c); 17 | public void putShortVolatile(long byteIndex, short s); 18 | public void putIntVolatile(long byteIndex, int i); 19 | public void putLongVolatile(long byteIndex, long l); 20 | public void putFloatVolatile(long byteIndex, float f); 21 | public void putDoubleVolatile(long byteIndex, double d); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/bytesource/AsciiStringByteSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap.bytez.bytesource; 17 | 18 | import org.nustaq.offheap.bytez.ByteSource; 19 | 20 | /** 21 | * Created by ruedi on 27.06.14. 22 | */ 23 | public class AsciiStringByteSource implements ByteSource { 24 | 25 | protected String string; 26 | protected int off; 27 | protected int len; 28 | 29 | public AsciiStringByteSource(String arr) { 30 | this.string = arr; 31 | off = 0; 32 | len = arr.length(); 33 | } 34 | 35 | public AsciiStringByteSource(String arr, int off) { 36 | this.string = arr; 37 | this.off = off; 38 | len = string.length()-off; 39 | } 40 | 41 | public AsciiStringByteSource(String arr, int off, int len) { 42 | this.string = arr; 43 | this.off = off; 44 | this.len = len; 45 | } 46 | 47 | @Override 48 | public byte get(long index) { 49 | return (byte) string.charAt(((int) (index + off))); 50 | } 51 | 52 | @Override 53 | public long length() { 54 | return len; 55 | } 56 | 57 | public String getString() { 58 | return string; 59 | } 60 | 61 | public void setString(String string) { 62 | if ( string.length() > len ) 63 | throw new RuntimeException("key value too long"); 64 | this.string = string; 65 | } 66 | 67 | public int getLen() { 68 | return len; 69 | } 70 | 71 | public void setLen(int len) { 72 | this.len = len; 73 | } 74 | 75 | public int getOff() { 76 | return off; 77 | } 78 | 79 | public void setOff(int off) { 80 | this.off = off; 81 | } 82 | 83 | public String toString() { 84 | return string.substring(off,off+len); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/bytesource/ByteArrayByteSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap.bytez.bytesource; 17 | 18 | import org.nustaq.offheap.bytez.ByteSource; 19 | 20 | /** 21 | * Created by ruedi on 27.06.14. 22 | */ 23 | public class ByteArrayByteSource implements ByteSource 24 | { 25 | 26 | byte arr[]; 27 | int off; 28 | int len; 29 | 30 | public ByteArrayByteSource(byte[] arr) { 31 | this.arr = arr; 32 | off = 0; 33 | len = arr.length; 34 | } 35 | 36 | public ByteArrayByteSource(byte[] arr, int off) { 37 | this.arr = arr; 38 | this.off = off; 39 | len = arr.length-off; 40 | } 41 | 42 | public ByteArrayByteSource(byte[] arr, int off, int len) { 43 | this.arr = arr; 44 | this.off = off; 45 | this.len = len; 46 | } 47 | 48 | @Override 49 | public byte get(long index) { 50 | return arr[((int) (index + off))]; 51 | } 52 | 53 | @Override 54 | public long length() { 55 | return len; 56 | } 57 | 58 | public byte[] getArr() { 59 | return arr; 60 | } 61 | 62 | public void setArr(byte[] arr) { 63 | this.arr = arr; 64 | } 65 | 66 | public int getOff() { 67 | return off; 68 | } 69 | 70 | public void setOff(int off) { 71 | this.off = off; 72 | } 73 | 74 | public void setLen(int len) { 75 | this.len = len; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/bytesource/BytezByteSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap.bytez.bytesource; 17 | 18 | import org.nustaq.offheap.bytez.ByteSource; 19 | import org.nustaq.offheap.bytez.Bytez; 20 | 21 | /** 22 | * Created by ruedi on 27.06.14. 23 | */ 24 | public class BytezByteSource implements ByteSource { 25 | 26 | Bytez bytes; 27 | long off; 28 | int len; 29 | 30 | public BytezByteSource(Bytez bytes, long off, int len) { 31 | this.bytes = bytes; 32 | this.off = off; 33 | this.len = len; 34 | } 35 | 36 | @Override 37 | public byte get(long index) { 38 | return bytes.get(index+off); 39 | } 40 | 41 | @Override 42 | public long length() { 43 | return len; 44 | } 45 | 46 | public Bytez getBytes() { 47 | return bytes; 48 | } 49 | 50 | public void setBytes(Bytez bytes) { 51 | this.bytes = bytes; 52 | } 53 | 54 | public long getOff() { 55 | return off; 56 | } 57 | 58 | public void setOff(long off) { 59 | this.off = off; 60 | } 61 | 62 | public int getLen() { 63 | return len; 64 | } 65 | 66 | public void setLen(int len) { 67 | this.len = len; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/bytesource/CutAsciiStringByteSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.offheap.bytez.bytesource; 18 | 19 | /** 20 | * Created by ruedi on 27.06.14. 21 | */ 22 | 23 | /** 24 | * returns 0 instead of throwing index exception 25 | */ 26 | public class CutAsciiStringByteSource extends AsciiStringByteSource { 27 | public CutAsciiStringByteSource(String arr) { 28 | super(arr); 29 | } 30 | 31 | public CutAsciiStringByteSource(String arr, int off) { 32 | super(arr, off); 33 | } 34 | 35 | public CutAsciiStringByteSource(String arr, int off, int len) { 36 | super(arr, off, len); 37 | } 38 | 39 | @Override 40 | public byte get(long index) { 41 | if ( index + off <= string.length() ) 42 | return super.get(index); 43 | return 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/bytesource/LeftCutStringByteSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.offheap.bytez.bytesource; 18 | 19 | /** 20 | * implement bytesource on top a string, the string is right aligned inside the byte source 21 | */ 22 | public class LeftCutStringByteSource extends AsciiStringByteSource { 23 | 24 | 25 | public LeftCutStringByteSource(String arr) { 26 | super(arr); 27 | } 28 | 29 | public LeftCutStringByteSource(String arr, int off) { 30 | super(arr, off); 31 | } 32 | 33 | public LeftCutStringByteSource(String arr, int off, int len) { 34 | super(arr, off, len); 35 | } 36 | 37 | @Override 38 | public byte get(long index) { 39 | int shift = len - string.length(); 40 | if ( index < shift ) { 41 | return 0; 42 | } 43 | return super.get(index-shift); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/bytesource/UTFStringByteSource.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.offheap.bytez.bytesource; 2 | 3 | import org.nustaq.offheap.bytez.ByteSource; 4 | import org.nustaq.serialization.util.FSTUtil; 5 | 6 | import java.io.UnsupportedEncodingException; 7 | 8 | /** 9 | * Created by ruedi on 05/08/15. 10 | */ 11 | public class UTFStringByteSource implements ByteSource { 12 | 13 | byte bytes[]; 14 | 15 | public UTFStringByteSource(String key) { 16 | try { 17 | bytes = key.getBytes("UTF-8"); 18 | } catch (UnsupportedEncodingException e) { 19 | FSTUtil.rethrow(e); 20 | } 21 | } 22 | 23 | @Override 24 | public byte get(long index) { 25 | return bytes[((int) index)]; 26 | } 27 | 28 | @Override 29 | public long length() { 30 | return bytes.length; 31 | } 32 | 33 | public UTFStringByteSource padLeft(int keyLen) { 34 | if ( bytes.length < keyLen ) { 35 | byte newBytes[] = new byte[keyLen]; 36 | System.arraycopy(bytes,0,newBytes,keyLen-bytes.length,bytes.length); 37 | bytes = newBytes; 38 | } 39 | return this; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/malloc/MMFBytez.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap.bytez.malloc; 17 | 18 | 19 | import jdk.incubator.foreign.MemorySegment; 20 | 21 | import java.io.File; 22 | import java.io.RandomAccessFile; 23 | import java.lang.reflect.InvocationTargetException; 24 | import java.lang.reflect.Method; 25 | import java.nio.channels.FileChannel; 26 | 27 | /** 28 | * Bytez allocated inside a memory mapped file. Some Mmap file stuff handling is copied from OpenHFT library (too big to depend on for fst), 29 | * a great tool for all kind of binary/low level java stuff. Check it out at github. 30 | */ 31 | public class MMFBytez extends MemoryBytez { 32 | private File file; 33 | 34 | public MMFBytez(String filePath, long length, boolean clearFile) throws Exception { 35 | init(filePath, length, clearFile); 36 | } 37 | 38 | protected void init(String file, long length, boolean clearFile) throws Exception { 39 | File f = new File(file); 40 | if (f.exists() && clearFile) { 41 | f.delete(); 42 | } 43 | if ( ! f.exists() ) { 44 | f.getParentFile().mkdirs(); 45 | f.createNewFile(); 46 | } 47 | memseg = MemorySegment.mapFromPath(f.toPath(),length, FileChannel.MapMode.READ_WRITE); 48 | this.file = f; 49 | } 50 | 51 | public void freeAndClose() { 52 | memseg.close(); 53 | } 54 | 55 | public File getFile() { 56 | return file; 57 | } 58 | 59 | // public static void main(String[] args) throws Exception { 60 | // MMFBytez mmfBytez = new MMFBytez("/tmp/mmf", 2000, false); 61 | // mmfBytez.put(1999, (byte) 1999); 62 | // for ( int i = 0; i < mmfBytez.length(); i++ ) { 63 | // mmfBytez.put(i,(byte)i); 64 | // System.out.println(mmfBytez.get(i)); 65 | // } 66 | // } 67 | 68 | } -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/malloc/MallocBytezAllocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.offheap.bytez.malloc; 18 | 19 | import org.nustaq.offheap.bytez.Bytez; 20 | import org.nustaq.offheap.bytez.BytezAllocator; 21 | 22 | import java.util.ArrayList; 23 | import java.util.concurrent.atomic.AtomicLong; 24 | 25 | /** 26 | * Date: 17.11.13 27 | * Time: 01:16 28 | * To change this template use File | Settings | File Templates. 29 | */ 30 | public class MallocBytezAllocator implements BytezAllocator { 31 | 32 | public static AtomicLong alloced = new AtomicLong(0); 33 | 34 | ArrayList allocated = new ArrayList(); 35 | 36 | @Override 37 | public Bytez alloc(long len) { 38 | MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len); 39 | mallocBytez.clear(); 40 | allocated.add(mallocBytez); 41 | alloced.getAndAdd(len); 42 | return mallocBytez; 43 | } 44 | 45 | @Override 46 | public void free(Bytez bytes) { 47 | if ( bytes instanceof MallocBytez && allocated.contains(bytes) ) { 48 | allocated.remove(bytes); 49 | alloced.getAndAdd(-bytes.length()); 50 | ((MallocBytez) bytes).free(); 51 | } 52 | } 53 | 54 | @Override 55 | public void freeAll() { 56 | for (int i = 0; i < allocated.size(); i++) { 57 | MallocBytez mallocBytez = allocated.get(i); 58 | mallocBytez.free(); 59 | } 60 | allocated.clear(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/offheap/bytez/onheap/HeapBytezAllocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.offheap.bytez.onheap; 17 | 18 | import org.nustaq.offheap.bytez.Bytez; 19 | import org.nustaq.offheap.bytez.BytezAllocator; 20 | 21 | /** 22 | * Date: 16.11.13 23 | * Time: 14:23 24 | * To change this template use File | Settings | File Templates. 25 | */ 26 | public class HeapBytezAllocator implements BytezAllocator { 27 | @Override 28 | public Bytez alloc(long len) { 29 | return new HeapBytez(new byte[(int) len]); 30 | } 31 | 32 | @Override 33 | public void free(Bytez bytes) { 34 | 35 | } 36 | 37 | @Override 38 | public void freeAll() { 39 | // nothing to do, GC 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTBasicObjectSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | /** 19 | * Created with IntelliJ IDEA. 20 | * User: ruedi 21 | * Date: 11.11.12 22 | * Time: 12:09 23 | * To change this template use File | Settings | File Templates. 24 | */ 25 | public abstract class FSTBasicObjectSerializer implements FSTCrossPlatformSerialzer { 26 | 27 | protected FSTBasicObjectSerializer() { 28 | } 29 | 30 | @Override 31 | public boolean willHandleClass(Class cl) { 32 | return true; 33 | } 34 | 35 | @Override 36 | public void readObject(FSTObjectInput in, Object toRead, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy) throws Exception { 37 | } 38 | 39 | /** 40 | * @return true if FST can skip a search for same instances in the serialized ObjectGraph. This speeds up reading and writing and makes 41 | * sense for short immutable such as Integer, Short, Character, Date, .. . For those classes it is more expensive (CPU, size) to do a lookup than to just 42 | * write the Object twice in case. 43 | */ 44 | @Override 45 | public boolean alwaysCopy() { 46 | return false; 47 | } 48 | 49 | @Override 50 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 51 | return null; 52 | } 53 | 54 | public boolean writeTupleEnd() { 55 | return true; 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTClassInstantiator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | import java.lang.reflect.Constructor; 19 | 20 | /** 21 | * Created by ruedi on 12.12.14. 22 | */ 23 | public interface FSTClassInstantiator { 24 | 25 | public Object newInstance(Class clazz, Constructor cons, boolean doesRequireInit, boolean unsafeAsLastResort ); 26 | public Constructor findConstructorForExternalize(Class clazz); 27 | public Constructor findConstructorForSerializable(Class clazz); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTCrossPlatformSerialzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | /** 19 | * Created by ruedi on 31.03.14. 20 | */ 21 | public interface FSTCrossPlatformSerialzer extends FSTObjectSerializer { 22 | 23 | boolean writeTupleEnd(); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTObjectSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | import java.io.IOException; 19 | 20 | /** 21 | * Created with IntelliJ IDEA. 22 | * User: ruedi 23 | * Date: 10.11.12 24 | * Time: 12:33 25 | * To change this template use File | Settings | File Templates. 26 | */ 27 | 28 | /** 29 | * @see FSTBasicObjectSerializer 30 | */ 31 | public interface FSTObjectSerializer { 32 | 33 | public static final String REALLY_NULL = "REALLY_NULL"; 34 | /** 35 | * write the contents of a given object 36 | */ 37 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) 38 | throws IOException; 39 | 40 | /** 41 | * read the content to an already instantiated object 42 | */ 43 | public void readObject(FSTObjectInput in, Object toRead, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy) 44 | throws Exception; 45 | 46 | /** 47 | * useful if you register for a class and its subclasses, but want to exclude a specific subclass 48 | */ 49 | public boolean willHandleClass(Class cl); 50 | 51 | /** 52 | * @return true if FST can skip a search for same instances in the serialized ObjectGraph. This speeds up reading and writing and makes 53 | * sense for short immutable such as Integer, Short, Character, Date, .. . For those classes it is more expensive (CPU, size) to do a lookup than to just 54 | * write the Object twice in case. 55 | */ 56 | public boolean alwaysCopy(); 57 | 58 | /** 59 | * return null to delegate object instantiation to FST. If you want to implement object instantiation yourself, usually you leave the readObject method empty 60 | * and handle instantiation and reading the object here. You must call registerObjectForWrite immediately after creating it on the FSTObjectInput 61 | */ 62 | public Object instantiate(Class objectClass, FSTObjectInput fstObjectInput, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) 63 | throws Exception; 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTObjenesisInstantiator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | import org.nustaq.serialization.util.FSTUtil; 19 | import org.objenesis.Objenesis; 20 | import org.objenesis.instantiator.ObjectInstantiator; 21 | 22 | import java.lang.reflect.Constructor; 23 | import java.lang.reflect.InvocationTargetException; 24 | import java.lang.reflect.Modifier; 25 | 26 | /** 27 | * Created by ruedi on 12.12.14. 28 | * 29 | * Used for Android as does not require sun.* or Unsafe to instantiate classes. 30 | * Might be slower for some cases (probably executes field init) 31 | * 32 | */ 33 | public class FSTObjenesisInstantiator implements FSTClassInstantiator { 34 | 35 | ObjectInstantiator objInstantiator; 36 | 37 | public FSTObjenesisInstantiator( Objenesis objenesis, Class clazz ) { 38 | objInstantiator = objenesis.getInstantiatorOf( clazz ); 39 | } 40 | 41 | static Object[] empty = new Object[0]; 42 | @Override 43 | public Object newInstance(Class clazz, Constructor cons, boolean doesRequireInit, boolean unsafeAsLastResort) { 44 | if ( cons != null ) 45 | try { 46 | return cons.newInstance(empty); 47 | } catch (Exception e) { 48 | FSTUtil.rethrow(e); 49 | } 50 | return objInstantiator.newInstance(); 51 | } 52 | 53 | @Override 54 | public Constructor findConstructorForExternalize(Class clazz) { 55 | try { 56 | Constructor c = clazz.getDeclaredConstructor((Class[]) null); 57 | if ( c == null ) 58 | return null; 59 | c.setAccessible(true); 60 | if ((c.getModifiers() & Modifier.PUBLIC) != 0) { 61 | return c; 62 | } else { 63 | return null; 64 | } 65 | } catch (NoSuchMethodException ex) { 66 | return null; 67 | } 68 | } 69 | 70 | @Override 71 | public Constructor findConstructorForSerializable(Class clazz) { 72 | return findConstructorForExternalize(clazz); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTSerialisationListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | /** 19 | * Date: 21.10.13 20 | * Time: 18:28 21 | * 22 | * can be used for stats or other weird things (FSTObjectOutput). Note this will slow down serialization significantly 23 | */ 24 | public interface FSTSerialisationListener { 25 | 26 | void objectWillBeWritten( Object obj, int streamPosition ); 27 | void objectHasBeenWritten( Object obj, int oldStreamPosition, int streamPosition ); 28 | 29 | } -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/FSTSerializerRegistryDelegate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | /** 19 | * interface to patch default serializer lookup. Can be set at FSTConfiguration 20 | */ 21 | public interface FSTSerializerRegistryDelegate { 22 | /** 23 | * @param cl 24 | * @return null for default lookup or a serializer for given class 25 | */ 26 | public FSTObjectSerializer getSerializer(Class cl); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/VersionConflictListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization; 17 | 18 | /** 19 | * Created by ruedi on 06.08.14. 20 | */ 21 | public interface VersionConflictListener { 22 | public void onOldVersionRead(Object newReadFromOldSerialized); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/AnonymousTransient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.*; 19 | 20 | /** 21 | * Date: 13.11.13 22 | * Time: 20:35 23 | * To change this template use File | Settings | File Templates. 24 | */ 25 | @Retention(RetentionPolicy.RUNTIME) 26 | @Target({ElementType.TYPE}) 27 | 28 | /** 29 | * specifies all this$ fields should be handled as transient 30 | * useful when serializing anonymous classes which should get executed remotely 31 | */ 32 | public @interface AnonymousTransient { 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/Conditional.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * applicable to non native fields. if a conditional callback is set on the FSTObjectInput, the callback 25 | * will be called before the object referenced by the annotated field is decoded. Allows to skip decoding of 26 | * objects partially to save CPU. 27 | * 28 | * WARNING: can be used for unshared (cycle free) Objects only. In addition all classes contained in the 29 | * skipped subgraph need to be preregistered. So applicable rarely. See git issue #75 30 | * 31 | */ 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target(ElementType.FIELD) 34 | 35 | public @interface Conditional { 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/Flat.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | @Retention(RetentionPolicy.RUNTIME) 24 | @Target({ElementType.FIELD,ElementType.TYPE}) 25 | 26 | /** 27 | * defines the reference should not be looked up for identical objects in the object graph. May speed up 28 | * encoding, however in case there is another link to this object, performance and size are worse then 29 | */ 30 | public @interface Flat { 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/OneOf.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | @Retention(RetentionPolicy.RUNTIME) 24 | @Target(ElementType.FIELD) 25 | 26 | /** 27 | * save bandwidth + performance in case a String reference frequently contains the same values. 28 | */ 29 | public @interface OneOf { 30 | String[] value(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/Predict.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.serialization.annotations; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target({ElementType.FIELD,ElementType.TYPE}) 26 | 27 | public @interface Predict { 28 | Class[] value(); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/Serialize.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Date: 13.11.13 25 | * Time: 20:45 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target({ElementType.FIELD,ElementType.TYPE}) 29 | /** 30 | * see @Transient 31 | */ 32 | public @interface Serialize { 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/Transient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.*; 19 | 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Target({ElementType.TYPE}) 22 | 23 | /** 24 | * specifies all fields in this class are by default transient. Only fields marked @Serialize are written on serialization. 25 | * This is useful when serializing e.g. anonymous classes, and want to avoid serializing the outer class. Warning this annotation is inherited !! 26 | */ 27 | public @interface Transient { 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/annotations/Version.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.annotations; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | @Retention(RetentionPolicy.RUNTIME) 24 | @Target({ElementType.FIELD}) 25 | 26 | /** 27 | * support for adding fields without breaking compatibility to old streams. 28 | * For each release of your app increment the version value. No Version annotation means version=0. 29 | * Note that each added field needs to be annotated. 30 | * 31 | * e.g. 32 | * 33 | * class MyClass implements Serializable { 34 | * 35 | * // fields on initial release 1.0 36 | * int x; 37 | * String y; 38 | * 39 | * // fields added with release 1.5 40 | * @Version(1) String added; 41 | * @Version(1) String alsoAdded; 42 | * 43 | * // fields added with release 2.0 44 | * @Version(2) String addedv2; 45 | * @Version(2) String alsoAddedv2; 46 | * 47 | * } 48 | * 49 | * If an old class is read, new fields will be set to default values. You can register a VersionConflictListener 50 | * at FSTObjectInput in order to fill in defaults for new fields. 51 | * 52 | * Notes/Limits: 53 | * - Removing fields will break backward compatibility. You can only Add new fields. 54 | * - Can slow down serialization over time (if many versions) 55 | * - does not work for Externalizable or Classes which make use of JDK-special features such as readObject/writeObject 56 | * (AKA does not work if fst has to fall back to 'compatible mode' for an object). 57 | * - in case you use custom serializers, your custom serializer has to handle versioning 58 | * 59 | */ 60 | public @interface Version { 61 | byte value(); 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/coders/JSONAsString.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.coders; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Created by ruedi on 21.03.17. 10 | * 11 | * advice json serializer to convert annotated field to a string (e.g. byte or char arrays) 12 | * 13 | * !!!!!!!! supports byte[] only currently !!!!!!!!!!!!!!! 14 | */ 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Target(ElementType.FIELD) 17 | 18 | public @interface JSONAsString { 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/minbin/GenMeta.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.minbin; 17 | 18 | import java.io.Serializable; 19 | import java.util.List; 20 | 21 | /** 22 | * Created by ruedi on 29.05.2014. 23 | */ 24 | public interface GenMeta extends Serializable { 25 | public List getClasses(); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/minbin/MBObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.minbin; 17 | 18 | import java.io.Serializable; 19 | import java.util.HashMap; 20 | import java.util.Iterator; 21 | 22 | /** 23 | * Date: 13.04.2014 24 | * Time: 15:36 25 | * 26 | * Generic object used in case a reading application does not know about the classes 27 | * used by the writing application 28 | */ 29 | public class MBObject implements Serializable { 30 | static Iterator emptyIter = new Iterator() { 31 | @Override 32 | public boolean hasNext() { 33 | return false; 34 | } 35 | 36 | @Override 37 | public String next() { 38 | return null; 39 | } 40 | 41 | @Override 42 | public void remove() { 43 | 44 | } 45 | }; 46 | 47 | HashMap values; 48 | Object typeInfo; 49 | 50 | public MBObject(Object typeInfo) { 51 | this.typeInfo = typeInfo; 52 | } 53 | 54 | public Object getTypeInfo() { 55 | return typeInfo; 56 | } 57 | 58 | public void setTypeInfo(Object typeInfo) { 59 | this.typeInfo = typeInfo; 60 | } 61 | 62 | public Object get(String key) { 63 | if ( values != null ) 64 | return values.get(key); 65 | return null; 66 | } 67 | 68 | public MBObject put(String key, Object val) { 69 | if ( values == null ) 70 | values = new HashMap<>(); 71 | values.put(key,val); 72 | return this; 73 | } 74 | 75 | public int size() { 76 | return values == null ? 0 : values.size(); 77 | } 78 | 79 | public Iterator keyIterator() { 80 | if ( values == null ) { 81 | return emptyIter; 82 | } 83 | return values.keySet().iterator(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/minbin/MBRef.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.minbin; 17 | 18 | import java.io.Serializable; 19 | 20 | /** 21 | * Created by ruedi on 02.05.14. 22 | */ 23 | public class MBRef implements Serializable { 24 | int streamPosition; 25 | 26 | public MBRef(int streamPosition) { 27 | this.streamPosition = streamPosition; 28 | } 29 | 30 | public int getStreamPosition() { 31 | return streamPosition; 32 | } 33 | 34 | public void setStreamPosition(int streamPosition) { 35 | this.streamPosition = streamPosition; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "MBRef("+streamPosition +')'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/minbin/MBSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.minbin; 17 | 18 | import java.io.Serializable; 19 | import java.util.ArrayList; 20 | 21 | /** 22 | * Date: 13.04.2014 23 | * Time: 15:36 24 | * same as MBObject but for sequences of objects. 25 | */ 26 | public class MBSequence implements Serializable { 27 | ArrayList content; 28 | Object typeInfo; 29 | 30 | public MBSequence(Object typeInfo) { 31 | this.typeInfo = typeInfo; 32 | } 33 | 34 | public int size() { 35 | return content == null ? 0 : content.size(); 36 | } 37 | 38 | public Object get(int index) { 39 | if ( content == null ) 40 | throw new IndexOutOfBoundsException("size "+size()+" index:"+index); 41 | return content.get(index); 42 | } 43 | 44 | public void set( int index, Object value ) { 45 | if ( content == null ) 46 | throw new IndexOutOfBoundsException("size "+size()+" index:"+index); 47 | content.set(index,value); 48 | } 49 | 50 | public MBSequence add( Object ... o ) { 51 | if (content == null) { 52 | content = new ArrayList(); 53 | } 54 | if ( o == null ) { 55 | o = new Object[] { null }; 56 | } 57 | for (int i = 0; i < o.length; i++) { 58 | content.add(o[i]); 59 | } 60 | return this; 61 | } 62 | 63 | public Object getTypeInfo() { 64 | return typeInfo; 65 | } 66 | 67 | public void setTypeInfo(Object typeInfo) { 68 | this.typeInfo = typeInfo; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTArrayListSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.serializers; 17 | 18 | /** 19 | * Created by ruedi on 07.03.14. 20 | */ 21 | import org.nustaq.serialization.FSTBasicObjectSerializer; 22 | import org.nustaq.serialization.FSTClazzInfo; 23 | import org.nustaq.serialization.FSTObjectInput; 24 | import org.nustaq.serialization.FSTObjectOutput; 25 | import org.nustaq.serialization.util.FSTUtil; 26 | 27 | import java.io.IOException; 28 | import java.util.*; 29 | 30 | /** 31 | * Created with IntelliJ IDEA. 32 | * User: ruedi 33 | * Date: 10.11.12 34 | * Time: 15:55 35 | * To change this template use File | Settings | File Templates. 36 | */ 37 | public class FSTArrayListSerializer extends FSTBasicObjectSerializer { 38 | 39 | @Override 40 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 41 | ArrayList col = (ArrayList)toWrite; 42 | int size = col.size(); 43 | out.writeInt(size); 44 | Class lastClz = null; 45 | FSTClazzInfo lastInfo = null; 46 | for (int i = 0; i < size; i++) { 47 | Object o = col.get(i); 48 | if ( o != null ) { 49 | lastInfo = out.writeObjectInternal(o, o.getClass() == lastClz ? lastInfo : null, null); 50 | lastClz = o.getClass(); 51 | } else 52 | out.writeObjectInternal(o, null, null); 53 | } 54 | } 55 | 56 | @Override 57 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 58 | try { 59 | int len = in.readInt(); 60 | ArrayList res = new ArrayList(len); 61 | in.registerObject(res, streamPosition,serializationInfo, referencee); 62 | for ( int i = 0; i < len; i++ ) { 63 | final Object o = in.readObjectInternal(null); 64 | res.add(o); 65 | } 66 | return res; 67 | } catch (Throwable th) { 68 | FSTUtil.rethrow(th); 69 | } 70 | return null; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTBigIntegerSerializer.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.serializers; 2 | 3 | import org.nustaq.serialization.FSTBasicObjectSerializer; 4 | import org.nustaq.serialization.FSTClazzInfo; 5 | import org.nustaq.serialization.FSTObjectInput; 6 | import org.nustaq.serialization.FSTObjectOutput; 7 | 8 | import java.io.IOException; 9 | import java.math.BigInteger; 10 | 11 | /** 12 | * author: nicoruti 13 | * submitted via #53 14 | * 15 | */ 16 | public class FSTBigIntegerSerializer extends FSTBasicObjectSerializer { 17 | 18 | @Override 19 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, 20 | int streamPosition) throws IOException { 21 | byte[] value = ((BigInteger) toWrite).toByteArray(); 22 | out.writeInt(value.length); 23 | out.write(value); 24 | } 25 | 26 | @Override 27 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, 28 | int streamPosition) throws Exception { 29 | int len = in.readInt(); 30 | byte[] buf = new byte[len]; 31 | in.read(buf); 32 | BigInteger bigInteger = new BigInteger(buf); 33 | in.registerObject(bigInteger,streamPosition,serializationInfo,referencee); 34 | return bigInteger; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTBitSetSerializer.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.serializers; 2 | 3 | import org.nustaq.serialization.FSTBasicObjectSerializer; 4 | import org.nustaq.serialization.FSTClazzInfo; 5 | import org.nustaq.serialization.FSTObjectInput; 6 | import org.nustaq.serialization.FSTObjectOutput; 7 | 8 | import java.io.IOException; 9 | import java.util.BitSet; 10 | 11 | public class FSTBitSetSerializer extends FSTBasicObjectSerializer { 12 | @Override 13 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, 14 | FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 15 | out.writeObject(((BitSet)toWrite).toLongArray()); 16 | } 17 | 18 | @Override 19 | public boolean alwaysCopy(){ 20 | return true; 21 | } 22 | 23 | @Override 24 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, 25 | FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 26 | long[] l = (long[])in.readObject(); 27 | Object res = BitSet.valueOf(l); 28 | return res; 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTCPThrowableSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.serializers; 17 | 18 | import org.nustaq.serialization.FSTBasicObjectSerializer; 19 | import org.nustaq.serialization.FSTClazzInfo; 20 | import org.nustaq.serialization.FSTObjectInput; 21 | import org.nustaq.serialization.FSTObjectOutput; 22 | 23 | import java.io.IOException; 24 | import java.lang.reflect.Constructor; 25 | 26 | /** 27 | * Created by ruedi on 02.05.14. 28 | * enable serialization of throwables for cross platform 29 | */ 30 | public class FSTCPThrowableSerializer extends FSTBasicObjectSerializer { 31 | @Override 32 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 33 | Throwable th = (Throwable) toWrite; 34 | out.writeStringUTF(th.getMessage()); 35 | } 36 | 37 | @Override 38 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 39 | String msg = in.readStringUTF(); 40 | try { 41 | Constructor constructor = objectClass.getConstructor(String.class); 42 | return constructor.newInstance(msg); 43 | } catch (Exception e) { 44 | return new Throwable("unable to deserialize original exception with message:"+msg); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTDateSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.serialization.serializers; 18 | 19 | import org.nustaq.serialization.FSTBasicObjectSerializer; 20 | import org.nustaq.serialization.FSTClazzInfo; 21 | import org.nustaq.serialization.FSTObjectInput; 22 | import org.nustaq.serialization.FSTObjectOutput; 23 | 24 | import java.io.IOException; 25 | import java.util.*; 26 | 27 | /** 28 | * Created with IntelliJ IDEA. 29 | * User: ruedi 30 | * Date: 02.12.12 31 | * Time: 14:16 32 | * To change this template use File | Settings | File Templates. 33 | */ 34 | public class FSTDateSerializer extends FSTBasicObjectSerializer { 35 | @Override 36 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 37 | out.writeLong(((Date)toWrite).getTime()); 38 | } 39 | 40 | /** 41 | * @return true if FST can skip a search for same instances in the serialized ObjectGraph. This speeds up reading and writing and makes 42 | * sense for short immutable such as Integer, Short, Character, Date, .. . For those classes it is more expensive (CPU, size) to do a lookup than to just 43 | * write the Object twice in case. 44 | */ 45 | @Override 46 | public boolean alwaysCopy() { 47 | return true; 48 | } 49 | 50 | @Override 51 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 52 | long l = in.readLong(); 53 | Object res = new Date(l); 54 | // in.registerObject(res,streamPosition,serializationInfo, referencee); can skip as alwaysCopy is true 55 | return res; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTJSonSerializers.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.serializers; 2 | 3 | import org.nustaq.serialization.FSTBasicObjectSerializer; 4 | import org.nustaq.serialization.FSTClazzInfo; 5 | import org.nustaq.serialization.FSTObjectInput; 6 | import org.nustaq.serialization.FSTObjectOutput; 7 | 8 | import java.io.IOException; 9 | import java.math.BigDecimal; 10 | 11 | /** 12 | * Created by ruedi on 24/05/15. 13 | */ 14 | public class FSTJSonSerializers { 15 | 16 | public static class BigDecSerializer extends FSTBasicObjectSerializer { 17 | @Override 18 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 19 | out.writeStringUTF(toWrite.toString()); 20 | } 21 | 22 | @Override 23 | public boolean alwaysCopy() { 24 | return true; 25 | } 26 | 27 | @Override 28 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 29 | return new BigDecimal(in.readStringUTF()); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTJSonUnmodifiableMapSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | * 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * 8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package org.nustaq.serialization.serializers; 15 | 16 | import org.nustaq.serialization.FSTClazzInfo; 17 | import org.nustaq.serialization.FSTObjectInput; 18 | import org.nustaq.serialization.FSTObjectOutput; 19 | import org.nustaq.serialization.util.FSTUtil; 20 | 21 | import java.io.IOException; 22 | import java.util.*; 23 | 24 | /** 25 | * For JSON only, see {@link Unable to deserialize unmodifiable collections from JSON}. 26 | * 27 | * @author Jakub Kubrynski 28 | */ 29 | public class FSTJSonUnmodifiableMapSerializer extends FSTMapSerializer { 30 | 31 | public static final Class UNMODIFIABLE_MAP_CLASS; 32 | 33 | static { 34 | UNMODIFIABLE_MAP_CLASS = Collections.unmodifiableMap(new HashMap()).getClass(); 35 | } 36 | 37 | @Override 38 | @SuppressWarnings("unchecked") 39 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 40 | try { 41 | // note: unlike with list's JDK uses a single wrapper for unmodifiable maps, so information regarding ordering gets lost. 42 | // as the enclosed map is private, there is also no possibility to detect that case 43 | // we could always create a linkedhashmap here, but this would have major performance drawbacks. 44 | 45 | // this only hits JSON codec as JSON codec does not implement a full JDK-serialization fallback (like the binary codecs) 46 | int len = in.readInt(); 47 | if (UNMODIFIABLE_MAP_CLASS.isAssignableFrom(objectClass)) { 48 | Map res = new HashMap(len); 49 | in.registerObject(res, streamPosition, serializationInfo, referencee); 50 | for (int i = 0; i < len; i++) { 51 | Object key = in.readObjectInternal(null); 52 | Object val = in.readObjectInternal(null); 53 | res.put(key, val); 54 | } 55 | return Collections.unmodifiableMap(res); 56 | } 57 | } catch (Throwable th) { 58 | FSTUtil.rethrow(th); 59 | } 60 | return null; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.serializers; 2 | 3 | 4 | import org.nustaq.serialization.FSTBasicObjectSerializer; 5 | import org.nustaq.serialization.FSTClazzInfo; 6 | import org.nustaq.serialization.FSTObjectInput; 7 | import org.nustaq.serialization.FSTObjectOutput; 8 | 9 | import java.io.IOException; 10 | import java.lang.reflect.InvocationHandler; 11 | import java.lang.reflect.Proxy; 12 | 13 | 14 | public class FSTProxySerializer extends FSTBasicObjectSerializer { 15 | 16 | @Override 17 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 18 | Class[] ifaces = clzInfo.getClazz().getInterfaces(); 19 | ClassLoader cl = out.getConf().getClassLoader(); 20 | out.writeInt(ifaces.length); 21 | for (Class i : ifaces) 22 | out.writeUTF(i.getName()); 23 | out.writeObject(Proxy.getInvocationHandler(toWrite)); 24 | } 25 | 26 | @Override 27 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPositioin) throws IOException, ClassNotFoundException { 28 | ClassLoader cl = in.getConf().getClassLoader(); 29 | int numIfaces = in.readInt(); 30 | String[] interfaces = new String[numIfaces]; 31 | for (int i = 0; i < numIfaces; i++) { 32 | interfaces[i] = in.readUTF(); 33 | } 34 | Class[] classObjs = new Class[interfaces.length]; 35 | 36 | for(int i = 0; i < interfaces.length; ++i) { 37 | try { 38 | classObjs[i] = Class.forName(interfaces[i], false, cl); 39 | } catch (ClassNotFoundException e) { 40 | classObjs[i] = Class.forName(interfaces[i], false, this.getClass().getClassLoader()); 41 | } 42 | } 43 | InvocationHandler ih = (InvocationHandler)in.readObject(); 44 | Object res = Proxy.newProxyInstance(in.getConf().getClassLoader(),classObjs,ih); 45 | in.registerObject(res,streamPositioin,serializationInfo,referencee); 46 | return res; 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTStringBufferSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.serialization.serializers; 18 | 19 | import org.nustaq.serialization.FSTBasicObjectSerializer; 20 | import org.nustaq.serialization.FSTClazzInfo; 21 | import org.nustaq.serialization.FSTObjectInput; 22 | import org.nustaq.serialization.FSTObjectOutput; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * Created with IntelliJ IDEA. 28 | * User: ruedi 29 | * Date: 12.11.12 30 | * Time: 01:20 31 | * To change this template use File | Settings | File Templates. 32 | */ 33 | public class FSTStringBufferSerializer extends FSTBasicObjectSerializer { 34 | @Override 35 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 36 | out.writeStringUTF(toWrite.toString()); // cruel slow stuff 37 | } 38 | 39 | /** 40 | * @return true if FST can skip a search for same instances in the serialized ObjectGraph. This speeds up reading and writing and makes 41 | * sense for short immutable such as Integer, Short, Character, Date, .. . For those classes it is more expensive (CPU, size) to do a lookup than to just 42 | * write the Object twice in case. 43 | */ 44 | @Override 45 | public boolean alwaysCopy() { 46 | return false; 47 | } 48 | 49 | @Override 50 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 51 | String s = in.readStringUTF(); 52 | StringBuffer stringBuffer = new StringBuffer(s); 53 | in.registerObject(stringBuffer, streamPosition, serializationInfo, referencee); 54 | return stringBuffer; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTStringBuilderSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.nustaq.serialization.serializers; 18 | 19 | import org.nustaq.serialization.FSTClazzInfo; 20 | import org.nustaq.serialization.FSTObjectInput; 21 | 22 | /** 23 | * Created with IntelliJ IDEA. 24 | * User: ruedi 25 | * Date: 12.11.12 26 | * Time: 01:20 27 | * To change this template use File | Settings | File Templates. 28 | */ 29 | public class FSTStringBuilderSerializer extends FSTStringBufferSerializer { 30 | @Override 31 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 32 | String s = in.readStringUTF(); 33 | StringBuilder stringBuilder = new StringBuilder(s); 34 | in.registerObject(stringBuilder, streamPosition,serializationInfo, referencee); 35 | return stringBuilder; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTStringSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.serializers; 17 | 18 | import org.nustaq.serialization.FSTBasicObjectSerializer; 19 | import org.nustaq.serialization.FSTClazzInfo; 20 | import org.nustaq.serialization.FSTObjectInput; 21 | import org.nustaq.serialization.FSTObjectOutput; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Created with IntelliJ IDEA. 27 | * User: ruedi 28 | * Date: 07.12.12 29 | * Time: 01:26 30 | * To change this template use File | Settings | File Templates. 31 | */ 32 | public class FSTStringSerializer extends FSTBasicObjectSerializer { 33 | 34 | public static FSTStringSerializer Instance = new FSTStringSerializer(); // used directly 35 | 36 | @Override 37 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 38 | out.writeStringUTF((String) toWrite); 39 | } 40 | 41 | @Override 42 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 43 | String s = in.readStringUTF(); 44 | in.registerObject(s, streamPosition, serializationInfo, referencee); 45 | return s; 46 | } 47 | 48 | @Override 49 | public boolean writeTupleEnd() { 50 | return false; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.serializers; 2 | 3 | import org.nustaq.serialization.FSTBasicObjectSerializer; 4 | import org.nustaq.serialization.FSTClazzInfo; 5 | import org.nustaq.serialization.FSTObjectInput; 6 | import org.nustaq.serialization.FSTObjectOutput; 7 | 8 | import java.io.IOException; 9 | import java.lang.reflect.Constructor; 10 | 11 | public class FSTThrowableSerializer extends FSTBasicObjectSerializer { 12 | @Override 13 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, 14 | FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 15 | Throwable t = (Throwable)toWrite; 16 | out.writeStringUTF(t.getMessage() != null ? t.getMessage() : "null"); 17 | StackTraceElement[] ste = t.getStackTrace(); 18 | out.writeObject(ste); 19 | out.writeObject(t.getCause()); 20 | out.writeObject(t.getSuppressed()); 21 | } 22 | 23 | 24 | @Override 25 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, 26 | FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 27 | Constructor constructor = objectClass.getConstructor(String.class); 28 | Throwable t = constructor.newInstance(in.readStringUTF()); // This causes stack trace to be filled in twice but not an easy way to solve 29 | StackTraceElement[] ste = (StackTraceElement[]) in.readObject(); 30 | if (ste!=null) 31 | t.setStackTrace(ste); 32 | t.initCause((Throwable) in.readObject()); 33 | Throwable[] suppressed = (Throwable[]) in.readObject(); 34 | for (Throwable s : suppressed) 35 | t.addSuppressed(s); 36 | return t; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/serializers/FSTTimestampSerializer.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.serializers; 2 | 3 | import java.io.IOException; 4 | 5 | import org.nustaq.serialization.FSTBasicObjectSerializer; 6 | import org.nustaq.serialization.FSTClazzInfo; 7 | import org.nustaq.serialization.FSTClazzInfo.FSTFieldInfo; 8 | import org.nustaq.serialization.FSTObjectInput; 9 | import org.nustaq.serialization.FSTObjectOutput; 10 | 11 | public class FSTTimestampSerializer extends FSTBasicObjectSerializer { 12 | 13 | @Override 14 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, 15 | FSTFieldInfo referencedBy, int streamPosition) throws IOException { 16 | out.writeLong(((java.sql.Timestamp)toWrite).getTime()); 17 | } 18 | 19 | @Override 20 | public boolean alwaysCopy(){ 21 | return true; 22 | } 23 | 24 | @Override 25 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, 26 | FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 27 | long l = in.readLong(); 28 | Object res = new java.sql.Timestamp(l); 29 | return res; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/simpleapi/FSTBufferTooSmallException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.simpleapi; 17 | 18 | /** 19 | * Created by ruedi on 09.11.14. 20 | */ 21 | public class FSTBufferTooSmallException extends RuntimeException { 22 | 23 | public static FSTBufferTooSmallException Instance = new FSTBufferTooSmallException(); 24 | 25 | @Override 26 | public synchronized Throwable fillInStackTrace() { 27 | return this; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/simpleapi/FSTCoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.simpleapi; 17 | 18 | import org.nustaq.serialization.FSTConfiguration; 19 | 20 | /** 21 | * Created by ruedi on 15.11.14. 22 | */ 23 | public interface FSTCoder { 24 | 25 | public Object toObject( byte arr[], int startIndex, int availableSize); 26 | public Object toObject( byte arr[] ); 27 | 28 | public int toByteArray( Object obj, byte result[], int resultOffset, int availableSize ); 29 | public byte[] toByteArray( Object o ); 30 | 31 | // take care: changes in setup have to happen before first use 32 | public FSTConfiguration getConf(); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/simpleapi/MinBinCoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Ruediger Moeller. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.nustaq.serialization.simpleapi; 17 | 18 | import org.nustaq.serialization.*; 19 | 20 | /** 21 | * Created by ruedi on 09.11.14. 22 | * 23 | * see DefaultCoder. Uses MinBinCodec 24 | * 25 | */ 26 | public class MinBinCoder extends DefaultCoder { 27 | 28 | public MinBinCoder(boolean shared, Class ... toPreRegister) { 29 | conf = FSTConfiguration.createMinBinConfiguration(); 30 | conf.setShareReferences(shared); 31 | if ( toPreRegister != null && toPreRegister.length > 0 ) { 32 | conf.registerCrossPlatformClassMappingUseSimpleName(toPreRegister); 33 | } 34 | input = new FSTObjectInput(conf); 35 | output = new FSTObjectOutput(conf); 36 | } 37 | 38 | public MinBinCoder(Class ... preregister) { 39 | this(true,preregister); 40 | } 41 | 42 | public MinBinCoder() { 43 | this(true); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/util/DefaultFSTInt2ObjectMapFactory.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.util; 2 | 3 | public class DefaultFSTInt2ObjectMapFactory implements FSTInt2ObjectMapFactory{ 4 | @Override 5 | public FSTInt2ObjectMap createMap(int size) { 6 | return new DefaultFSTInt2ObjectMap<>(size); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/util/FSTInt2ObjectMap.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.util; 2 | 3 | public interface FSTInt2ObjectMap { 4 | int size(); 5 | void put(int key, V value); 6 | V get(int key); 7 | void clear(); 8 | } -------------------------------------------------------------------------------- /src/main/java/org/nustaq/serialization/util/FSTInt2ObjectMapFactory.java: -------------------------------------------------------------------------------- 1 | package org.nustaq.serialization.util; 2 | 3 | public interface FSTInt2ObjectMapFactory { 4 | FSTInt2ObjectMap createMap(int size); 5 | } -------------------------------------------------------------------------------- /src/main/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Test

4 | -------------------------------------------------------------------------------- /src/test/jdk_bug/foo/Test.java: -------------------------------------------------------------------------------- 1 | package jdk_bug.foo; 2 | 3 | import jdk_bug.foo.bean.TestBean; 4 | 5 | // submitted & found by jswaelens from github 6 | public class Test { 7 | 8 | public static void main(String[] args) { 9 | 10 | TestBean b =new TestBean(); 11 | b.doSomething(false); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/jdk_bug/foo/bean/AnImpl.java: -------------------------------------------------------------------------------- 1 | package jdk_bug.foo.bean; 2 | import java.nio.ByteBuffer; 3 | 4 | 5 | public class AnImpl extends SomeAbstract { 6 | 7 | // private Properties p = new Properties(); 8 | public ByteBuffer p = ByteBuffer.allocateDirect(1000); 9 | { 10 | p.putInt(13); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/jdk_bug/foo/bean/SomeAbstract.java: -------------------------------------------------------------------------------- 1 | package jdk_bug.foo.bean; 2 | 3 | 4 | public abstract class SomeAbstract { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/test/jdk_bug/foo/bean/TestBeanAncestor.java: -------------------------------------------------------------------------------- 1 | package jdk_bug.foo.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | 6 | public class TestBeanAncestor implements Serializable { 7 | 8 | public UtilFactory factory = new UtilFactory(); 9 | } 10 | -------------------------------------------------------------------------------- /src/test/jdk_bug/foo/bean/UtilFactory.java: -------------------------------------------------------------------------------- 1 | package jdk_bug.foo.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | 6 | public class UtilFactory extends UtilFactoryAncestor implements Serializable { 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/test/jdk_bug/foo/bean/UtilFactoryAncestor.java: -------------------------------------------------------------------------------- 1 | package jdk_bug.foo.bean; 2 | 3 | 4 | public class UtilFactoryAncestor { 5 | 6 | public SomeAbstract a = new AnImpl(); 7 | } 8 | -------------------------------------------------------------------------------- /src/test/json/JsonTypOrder.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuedigerMoeller/fast-serialization/e8da5591daa09452791dcd992ea4f83b20937be7/src/test/json/JsonTypOrder.java -------------------------------------------------------------------------------- /src/test/json/StringEnc.java: -------------------------------------------------------------------------------- 1 | package json; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | import org.nustaq.serialization.coders.JSONAsString; 7 | 8 | import java.io.Serializable; 9 | import java.io.UnsupportedEncodingException; 10 | 11 | /** 12 | * Created by ruedi on 21.03.17. 13 | */ 14 | public class StringEnc { 15 | 16 | public static class TS implements Serializable { 17 | @JSONAsString 18 | byte[] bytes; 19 | } 20 | 21 | @Test 22 | public void testStringenc() throws UnsupportedEncodingException { 23 | TS t = new TS(); 24 | t.bytes = "ÄÖasdß".getBytes("UTF-8"); 25 | FSTConfiguration conf = FSTConfiguration.createJsonConfiguration(true,false); 26 | byte[] bytes = conf.asByteArray(t); 27 | System.out.println(new String(bytes,"UTF-8")); 28 | Object res = conf.asObject(bytes); 29 | String x = new String(((TS) res).bytes, "UTF-8"); 30 | System.out.println(x); 31 | Assert.assertTrue(x.equals("ÄÖasdß")); 32 | } 33 | 34 | static class PJ implements Serializable { 35 | int a = 10; 36 | } 37 | 38 | @Test 39 | public void testLeadingSpaceBug() { 40 | FSTConfiguration fst = FSTConfiguration.createJsonConfiguration(); 41 | PJ pojo = new PJ(); 42 | String x = fst.asJsonString(pojo); 43 | System.out.println(x); 44 | String x1 = fst.asJsonString(pojo); 45 | System.out.println(x1); 46 | Assert.assertTrue(x.equals(x1)); 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/json/testjson.json: -------------------------------------------------------------------------------- 1 | { 2 | "styp": "Schnmock[]", 3 | "seq": [ 4 | 2, 5 | { 6 | "typ": "JSCompetitionChannel", 7 | "obj": { 8 | "stickyPost": { 9 | "typ": "ArticleTeaser", 10 | "obj": { 11 | "discussionIds": "pok" 12 | } 13 | }, 14 | "name": "HoCaX_Ideen_Forum" 15 | } 16 | }, 17 | { 18 | "typ": "JSCOMPETITIONCHANNEL", 19 | "obj": { 20 | "stickyPost": { 21 | "typ": "ARTICLETEASER", 22 | "obj": { 23 | "discussionIds": "POK" 24 | } 25 | }, 26 | "name": "HOCAX" 27 | } 28 | } 29 | ] 30 | } -------------------------------------------------------------------------------- /src/test/kson/KsonCustomer.java: -------------------------------------------------------------------------------- 1 | package kson; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by ruedi on 12.08.2014. 8 | */ 9 | public class KsonCustomer { 10 | private int id; 11 | private String name; 12 | 13 | private List phoneNumbers = new ArrayList(); 14 | 15 | 16 | public int getId() { 17 | return id; 18 | } 19 | 20 | public void setId(int id) { 21 | this.id = id; 22 | } 23 | public String getName() { 24 | return name; 25 | } 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | public List getPhoneNumbers() { 30 | return phoneNumbers; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/kson/KsonPhoneNumber.java: -------------------------------------------------------------------------------- 1 | package kson; 2 | 3 | /** 4 | * Created by ruedi on 12.08.2014. 5 | */ 6 | public class KsonPhoneNumber { 7 | private String type; 8 | private String value; 9 | 10 | public KsonPhoneNumber(String type, String value) { 11 | this.type = type; 12 | this.value = value; 13 | } 14 | 15 | public String getType() { 16 | return type; 17 | } 18 | public void setType(String type) { 19 | this.type = type; 20 | } 21 | public String getValue() { 22 | return value; 23 | } 24 | public void setValue(String value) { 25 | this.value = value; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/kson/test.json: -------------------------------------------------------------------------------- 1 | { 2 | 3 | "aString": "StringWithoutWhiteSpaceNeedNoQuotes", 4 | "aMap": { 5 | "Quoted" : { "str": "Hello", "someValue": 13 }, 6 | "UnQuoted" : { "str": "Hello1", "someValue": 14 }, 7 | "QuotOther" : { "str": "Hello13", "someValue": 15 } 8 | }, 9 | 10 | "aList": [ 11 | { 12 | "nameList": [ "a", "b", "c c", "d", "e e" ], 13 | "someValues": [ 1, 2, 3, 4, 5, 6, 3457 ] 14 | }, 15 | { 16 | "nameList": [ "Short" ], 17 | "someValues": [ 1, 2, 3 ] 18 | }, 19 | { 20 | "nameList": [], 21 | "someValues": [] 22 | } 23 | ], 24 | 25 | "untypedList": [ 26 | { 27 | "_type" : "pojo", "str" : "Hello13", "someValue" : 15 28 | }, 29 | { 30 | "_type" : "other" , 31 | "nameList" : ["a", "b"] , 32 | "someValues" : [ 1,2,3 ] 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /src/test/kson/test.kson: -------------------------------------------------------------------------------- 1 | test { 2 | aString: StringWithoutWhiteSpaceNeedNoQuotes 3 | 4 | # if collections/arrays are typed, no type information must be provided for an object. Just '{ .. }' 5 | aMap: { 6 | 'Quoted' : { str: Hello someValue: 13 } 7 | UnQuoted : { str: Hello1 someValue: 14 } 8 | "QuotOther" : { str: Hello13 someValue: 15 } 9 | } 10 | 11 | objectMap: { 12 | { str: Hello someValue: 13 } : aString 13 | { str: Hello1 someValue: 131 } : aString1 14 | } 15 | 16 | aList: { 17 | { 18 | nameList: { a b "c c" d 'e e' } 19 | someValues: { 1 2 3 4 5 6 3457 } 20 | } 21 | { 22 | nameList: { 'Short' } 23 | someValues: { 1 2 3 } 24 | } 25 | { 26 | nameList: {} 27 | someValues: {} 28 | } 29 | } 30 | 31 | # if collections/arrays are untyped (only object supported), objects need a type ' { .. }'. 32 | # to avoid ugly full qualified type names, use 'map' on the KKonfig class 33 | untypedList: { 34 | pojo { str: Hello13 someValue: 15 } 35 | other { 36 | nameList: { a b "c c" d 'e e' } 37 | someValues: { 1 2 3 4 5 6 3457 } 38 | } 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /src/test/net/TestObjectServer.java: -------------------------------------------------------------------------------- 1 | package net; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.net.TCPObjectServer; 6 | import org.nustaq.net.TCPObjectSocket; 7 | 8 | import java.io.EOFException; 9 | import java.io.IOException; 10 | import java.util.HashMap; 11 | 12 | /** 13 | * Created by ruedi on 22.08.14. 14 | */ 15 | public class TestObjectServer { 16 | 17 | public static final int PORT = 7777; 18 | 19 | public void server() throws IOException { 20 | TCPObjectServer server = new TCPObjectServer(PORT); 21 | server.start( new TCPObjectServer.NewClientListener() { 22 | // for each client an own thread is created by default. 23 | // so no need to create a new Thread for the client here 24 | @Override 25 | public void connectionAccepted(TCPObjectSocket client) { 26 | try { 27 | while( true ) { 28 | Object request = client.readObject(); 29 | if ( request == null ) 30 | return; // connection closed 31 | client.writeObject("Hello, I received: "); 32 | client.writeObject(request); 33 | client.flush(); 34 | } 35 | } catch (EOFException eof) { 36 | //e.printStackTrace(); 37 | } catch (Exception e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | }); 42 | } 43 | 44 | volatile boolean ok = false; 45 | public void client() throws Exception { 46 | TCPObjectSocket socket = new TCPObjectSocket("localhost", PORT); 47 | 48 | // send request 49 | HashMap toWrite = new HashMap(); 50 | toWrite.put("Greetings form year ", 2014 ); 51 | toWrite.put("Random ", Math.random() ); 52 | socket.writeObject(toWrite); 53 | socket.flush(); // <== important, else nothing happens ! 54 | 55 | // await 2 responses 56 | System.out.println(socket.readObject()); 57 | System.out.println(socket.readObject()); 58 | 59 | // done, close 60 | socket.close(); 61 | ok = true; 62 | } 63 | 64 | @Test 65 | public void test() throws Exception { 66 | server(); 67 | Thread.sleep(1000); 68 | client(); 69 | Thread.sleep(2000); 70 | Assert.assertTrue(ok); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/offheap/BinaryQueueTest.java: -------------------------------------------------------------------------------- 1 | package offheap; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.offheap.BinaryQueue; 6 | import org.nustaq.offheap.bytez.ByteSource; 7 | import org.nustaq.offheap.bytez.onheap.HeapBytez; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | * Created by moelrue on 5/5/15. 13 | */ 14 | public class BinaryQueueTest { 15 | 16 | @Test 17 | public void binQ() { 18 | HeapBytez sink = new HeapBytez(100); 19 | BinaryQueue q = new BinaryQueue(); 20 | for ( int ii = 0; ii < 500; ii++ ) { 21 | q.add(createMSG((byte) 1, 88)); 22 | Assert.assertTrue(q.available() == 88); 23 | q.add(createMSG((byte) 2, 99)); 24 | Assert.assertTrue(q.available() == 88 + 99); 25 | long len = q.poll(sink, 0, 88); 26 | for (int i = 0; i < len; i++) { 27 | Assert.assertTrue(sink.get(i) == 1); 28 | } 29 | Assert.assertTrue(q.available() == 99); 30 | len = q.poll(sink, 0, 99); 31 | for (int i = 0; i < len; i++) { 32 | Assert.assertTrue(sink.get(i) == 2); 33 | } 34 | Assert.assertTrue(q.available() == 0); 35 | } 36 | } 37 | 38 | @Test 39 | public void binOverflow() { 40 | HeapBytez sink = new HeapBytez(100); 41 | BinaryQueue q = new BinaryQueue(); 42 | for ( int ii = 0; ii < 50; ii++ ) { 43 | q.add(createMSG((byte) 1, 88)); 44 | q.add(createMSG((byte) 2, 99)); 45 | } 46 | Assert.assertTrue(q.available() == 50 * (99 + 88)); 47 | for ( int ii = 0; ii < 50; ii++ ) { 48 | long len = q.poll(sink, 0, 88); 49 | for (int i = 0; i < len; i++) { 50 | Assert.assertTrue(sink.get(i) == 1); 51 | } 52 | len = q.poll(sink, 0, 99); 53 | for (int i = 0; i < len; i++) { 54 | Assert.assertTrue(sink.get(i) == 2); 55 | } 56 | } 57 | System.out.println("cap "+q.capacity()+" size "+q.available()); 58 | } 59 | 60 | private ByteSource createMSG(byte num, int len) { 61 | byte[] b = new byte[len]; 62 | Arrays.fill(b, num); 63 | return new HeapBytez(b); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/offheap/LongMapTest.java: -------------------------------------------------------------------------------- 1 | package offheap; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.offheap.FSTLongOffheapMap; 6 | 7 | /** 8 | * Created by ruedi on 15.11.14. 9 | */ 10 | public class LongMapTest { 11 | 12 | @Test 13 | public void testLongMap() { 14 | FSTLongOffheapMap longMap = new FSTLongOffheapMap<>(FSTLongOffheapMap.GB,1000000); 15 | 16 | for ( int i = 0; i < 1000000; i++) { 17 | longMap.put((long) i,"HalliHallo"+i); 18 | } 19 | 20 | for ( int i = 0; i < 1000000; i++) { 21 | String s = longMap.get((long) i); 22 | Assert.assertTrue(s.equals("HalliHallo"+i)); 23 | } 24 | 25 | for ( int i = 0; i < 1000000; i++) { 26 | longMap.remove((long) i); 27 | Assert.assertTrue(longMap.get((long) i) == null); 28 | } 29 | 30 | longMap.free(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/ser/BasicAndroidTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.junit.Test; 4 | import org.nustaq.serialization.FSTConfiguration; 5 | 6 | /** 7 | * Created by ruedi on 12.12.14. 8 | */ 9 | public class BasicAndroidTest extends BasicFSTTest { 10 | 11 | @Override 12 | protected FSTConfiguration getTestConfiguration() { 13 | FSTConfiguration.isAndroid = true; 14 | return FSTConfiguration.createAndroidDefaultConfiguration(); 15 | } 16 | 17 | @Override @Test 18 | public void testSelfRef() { 19 | super.testSelfRef(); 20 | } 21 | 22 | @Override @Test 23 | public void testSelfRefArr() { 24 | super.testSelfRefArr(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/ser/BasicBinaryUnsafeTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.junit.Ignore; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | 7 | /** 8 | * Created by ruedi on 12.12.14. 9 | */ 10 | public class BasicBinaryUnsafeTest extends BasicFSTTest { 11 | 12 | @Override 13 | protected FSTConfiguration getTestConfiguration() { 14 | FSTConfiguration.isAndroid = false; 15 | return FSTConfiguration.createUnsafeBinaryConfiguration(); 16 | } 17 | 18 | // unsure wha this fails for unsafe version 19 | // bug was introduced with 2.45 FSTConfiguration addition of some serializers .. 20 | 21 | @Test 22 | @Ignore 23 | public void testVersioningIssue84() { 24 | 25 | } 26 | 27 | @Test @Ignore 28 | public void testVersioning() { 29 | 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/test/ser/BasicReuseTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Rule; 5 | import org.junit.Test; 6 | import org.junit.rules.ExpectedException; 7 | import org.nustaq.serialization.FSTConfiguration; 8 | import org.nustaq.serialization.FSTObjectInput; 9 | import org.nustaq.serialization.FSTObjectOutput; 10 | 11 | import java.io.ByteArrayInputStream; 12 | import java.io.ByteArrayOutputStream; 13 | import java.io.IOException; 14 | 15 | public class BasicReuseTest { 16 | 17 | @Rule 18 | public ExpectedException expectedException = ExpectedException.none(); 19 | 20 | @Test 21 | public void testStreamReuse() throws Exception { 22 | FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration(); 23 | 24 | String expected = "Hello, World!"; 25 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 26 | FSTObjectOutput fstObjectOutput = configuration.getObjectOutput(baos); 27 | try { 28 | fstObjectOutput.writeObject(expected); 29 | } finally { 30 | fstObjectOutput.flush(); 31 | } 32 | byte[] serializedData = baos.toByteArray(); 33 | FSTObjectInput input = configuration.getObjectInput(new ByteArrayInputStream(serializedData)); 34 | Object read = input.readObject(); 35 | Assert.assertEquals(expected, read); 36 | 37 | FSTObjectInput secondInput = configuration.getObjectInput(new ByteArrayInputStream(new byte[0])); 38 | expectedException.expect(IOException.class); 39 | expectedException.expectMessage("Failed to read"); 40 | secondInput.readObject(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/ser/Bug34.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.IOException; 6 | 7 | import org.junit.Test; 8 | import org.nustaq.serialization.FSTBasicObjectSerializer; 9 | import org.nustaq.serialization.FSTClazzInfo; 10 | import org.nustaq.serialization.FSTClazzInfo.FSTFieldInfo; 11 | import org.nustaq.serialization.FSTConfiguration; 12 | import org.nustaq.serialization.FSTObjectInput; 13 | import org.nustaq.serialization.FSTObjectOutput; 14 | 15 | // contributed by rdicroce 16 | public class Bug34 { 17 | 18 | @Test 19 | public void testCustomSerializer() throws Exception { 20 | FSTConfiguration FST = FSTConfiguration.createDefaultConfiguration(); 21 | //FST.setForceSerializable(true); 22 | FST.registerSerializer(Bug34.NonSerializableClass.class, new Serializer(), false); 23 | FSTObjectOutput out = FST.getObjectOutput(); 24 | out.writeObject(new Bug34.NonSerializableClass()); 25 | 26 | FSTObjectInput in = FST.getObjectInput(out.getCopyOfWrittenBuffer()); 27 | assertEquals(NonSerializableClass.class, in.readObject().getClass()); 28 | } 29 | 30 | private static class Serializer extends FSTBasicObjectSerializer { 31 | 32 | @Override 33 | public void writeObject(FSTObjectOutput out, Object toWrite, 34 | FSTClazzInfo clzInfo, FSTFieldInfo referencedBy, 35 | int streamPosition) throws IOException { 36 | out.writeByte(0); 37 | } 38 | 39 | @Override 40 | public Object instantiate(Class objectClass, FSTObjectInput in, 41 | FSTClazzInfo serializationInfo, FSTFieldInfo referencee, 42 | int streamPosition) throws IOException, 43 | ClassNotFoundException, InstantiationException, 44 | IllegalAccessException { 45 | Object o = new NonSerializableClass(); 46 | in.readByte(); 47 | in.registerObject(o, streamPosition, serializationInfo, referencee); 48 | return o; 49 | } 50 | 51 | } 52 | 53 | public static class NonSerializableClass { 54 | 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /src/test/ser/ForceSerIssue89.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.nustaq.serialization.FSTConfiguration; 4 | 5 | import java.net.InetAddress; 6 | import java.net.UnknownHostException; 7 | 8 | /** 9 | * Created by ruedi on 29/10/15. 10 | */ 11 | public class ForceSerIssue89 { 12 | 13 | public static class JettyInfo { 14 | private final InetAddress ipAddress; 15 | private final Integer port; 16 | private final boolean isCoordinator; 17 | private final String controllerId; 18 | 19 | public JettyInfo(final InetAddress ipAddress, final Integer port, final boolean isCoordinator, final String controllerId) { 20 | this.ipAddress = ipAddress; 21 | this.port = port; 22 | this.isCoordinator = isCoordinator; 23 | this.controllerId = controllerId; 24 | } 25 | 26 | //getters for all fields 27 | } 28 | 29 | public static void main(String[] args) throws UnknownHostException { 30 | FSTConfiguration conf = FSTConfiguration.createUnsafeBinaryConfiguration().setForceSerializable(true); 31 | JettyInfo poaksd1 = new JettyInfo(InetAddress.getLocalHost(), 8888, true, "poaksd"); 32 | Object poaksd = conf.asObject(conf.asByteArray(poaksd1)); 33 | System.out.println("POK"); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/ser/Git67.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.junit.Test; 4 | import org.nustaq.serialization.FSTConfiguration; 5 | import org.nustaq.serialization.FSTObjectOutput; 6 | import org.nustaq.serialization.FSTObjectOutputNoShared; 7 | import org.nustaq.serialization.serializers.FSTCollectionSerializer; 8 | 9 | import java.io.IOException; 10 | import java.util.LinkedHashSet; 11 | import java.util.Set; 12 | 13 | /** 14 | * Created by moelrue on 5/7/15. 15 | */ 16 | public class Git67 { 17 | 18 | @Test 19 | public void testUnshared() { 20 | Set obj = new LinkedHashSet<>(); 21 | obj.add(11373L); 22 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 23 | conf.setShareReferences(false); 24 | conf.registerSerializer(LinkedHashSet.class,new FSTCollectionSerializer(),true); 25 | byte[] bytes = conf.asByteArray(obj); 26 | Object o = conf.asObject(bytes); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/ser/GitHub159.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import java.io.IOException; 4 | import java.io.ObjectInputStream; 5 | import java.io.ObjectOutputStream; 6 | import java.io.Serializable; 7 | import java.net.URI; 8 | import java.util.UUID; 9 | 10 | import org.nustaq.serialization.FSTConfiguration; 11 | import org.nustaq.serialization.FSTObjectInput; 12 | import org.nustaq.serialization.FSTObjectInputNoShared; 13 | import org.nustaq.serialization.FSTObjectOutput; 14 | import org.nustaq.serialization.FSTObjectOutputNoShared; 15 | 16 | /** 17 | * Created by pmarx on 05.12.2016. 18 | */ 19 | public class GitHub159 { 20 | 21 | public static class TransientField implements Serializable { 22 | private transient String transientString; 23 | private String string; 24 | 25 | public TransientField(String string) { 26 | this.string = string; 27 | this.transientString = string; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return transientString; 33 | } 34 | 35 | private void writeObject(ObjectOutputStream os) throws IOException { 36 | os.defaultWriteObject(); 37 | } 38 | 39 | private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException { 40 | is.defaultReadObject(); 41 | this.transientString = string; 42 | } 43 | } 44 | 45 | public static void main(String[] args) throws Exception { 46 | encodeDecode(URI.create("https://github.com/RuedigerMoeller/fast-serialization/issues/159")); 47 | encodeDecode(URI.create("https://github.com/RuedigerMoeller/fast-serialization/issues/159").toURL()); 48 | encodeDecode(new TransientField(UUID.randomUUID().toString())); 49 | } 50 | 51 | private static void encodeDecode(Object object) throws Exception { 52 | try { 53 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 54 | conf.setShareReferences(false); 55 | try (FSTObjectInput input = new FSTObjectInputNoShared(conf); 56 | FSTObjectOutput output = new FSTObjectOutputNoShared(conf)) { 57 | 58 | output.writeObject(object); 59 | byte[] bytes = output.getCopyOfWrittenBuffer(); 60 | 61 | input.resetForReuseUseArray(bytes); 62 | Object read = input.readObject(); 63 | 64 | System.out.println(read); 65 | System.out.println(object.equals(read)); 66 | } 67 | } catch (Throwable e) { 68 | System.err.println(e.getClass() + ": " + e.getMessage()); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/ser/Github61.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.nustaq.serialization.*; 4 | 5 | import java.io.*; 6 | 7 | /** 8 | * Created by ruedi on 14.04.2015. 9 | */ 10 | public class Github61 { 11 | 12 | /** Size of primitive array */ 13 | static final int SIZE = 50_000_000; 14 | 15 | public static void main(String[] args) throws Exception { 16 | 17 | CountingOutputStream count; 18 | long start, elapsed; 19 | 20 | Object obj = new double[SIZE]; 21 | 22 | count = new CountingOutputStream(); 23 | FSTConfiguration fastconf = FSTConfiguration.createUnsafeBinaryConfiguration(); 24 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 25 | while ( true ) { 26 | 27 | start = System.nanoTime(); 28 | try(ObjectOutputStream oos = new ObjectOutputStream(count)) { 29 | oos.writeObject(obj); 30 | } 31 | elapsed = System.nanoTime() - start; 32 | System.out.println("STD " + count.getCount() + " bytes written in " + (elapsed) / 1000000L + "ms"); 33 | count.reset(); 34 | 35 | start = System.nanoTime(); 36 | try(FSTObjectOutput fos = conf.getObjectOutput(count)) { 37 | fos.writeObject(obj); 38 | } 39 | elapsed = System.nanoTime() - start; 40 | System.out.println("FST " + count.getCount() + " bytes written in " + (elapsed) / 1000000L + "ms"); 41 | count.reset(); 42 | 43 | start = System.nanoTime(); 44 | try(FSTObjectOutput fos = fastconf.getObjectOutput(count)) { 45 | fos.writeObject(obj); 46 | } 47 | elapsed = System.nanoTime() - start; 48 | System.out.println("FST unsafe " + count.getCount() + " bytes written in " + (elapsed)/1000000L + "ms"); 49 | count.reset(); 50 | } 51 | } 52 | 53 | 54 | public static class CountingOutputStream extends ByteArrayOutputStream { 55 | 56 | public CountingOutputStream() { 57 | super(SIZE*10); 58 | } 59 | 60 | public int getCount() { 61 | return count; 62 | } 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/ser/I177.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | import org.nustaq.serialization.FSTObjectOutput; 7 | import java.io.IOException; 8 | import java.time.Duration; 9 | 10 | /** 11 | * Created by ruedi on 10.04.17. 12 | */ 13 | public class I177 { 14 | 15 | @Test 16 | public void test() throws IOException { 17 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 18 | Object[] x = { Duration.ofDays(1), Duration.ofDays(2) }; 19 | Object[] res = (Object[]) conf.asObject(conf.asByteArray(x)); 20 | Assert.assertTrue( x[0].equals(res[0]) && x[1].equals(res[1])); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/ser/I178.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | /** 4 | * Created by ruedi on 10.04.17. 5 | */ 6 | public class I178 implements java.io.Serializable { 7 | public static void main(String[] args) throws java.io.IOException { 8 | new org.nustaq.serialization.FSTObjectOutput().writeObject(new I178()); 9 | } 10 | private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { 11 | throw new java.io.IOException("Boom!"); 12 | } 13 | } -------------------------------------------------------------------------------- /src/test/ser/I79.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.junit.*; 4 | import org.nustaq.serialization.*; 5 | import org.nustaq.serialization.simpleapi.*; 6 | 7 | import java.io.*; 8 | 9 | /** 10 | * Created by moelrue on 15.07.2015. 11 | */ 12 | public class I79 { 13 | 14 | static FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 15 | 16 | @Test 17 | public void fstObj() throws IOException, ClassNotFoundException { 18 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 19 | Serializable[] os = new Serializable[]{"mysh zzx", 1, 'c'}; 20 | 21 | try (FSTObjectOutput fos = new FSTObjectOutput(out,conf)) { 22 | for (Serializable o : os) { 23 | fos.writeObject(o); 24 | } 25 | } 26 | 27 | byte[] buf = out.toByteArray(); 28 | InputStream in = new ByteArrayInputStream(buf); 29 | 30 | FSTObjectInput oin = new FSTObjectInput(in,conf); 31 | for (Object o : os) { 32 | Object obj = oin.readObject(); 33 | System.out.println(obj); 34 | Assert.assertEquals(o, obj); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/ser/Issue91.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.nustaq.serialization.FSTConfiguration; 4 | import org.nustaq.serialization.FSTObjectOutput; 5 | 6 | import java.util.Arrays; 7 | 8 | /** 9 | * Created by moelrue on 27.10.2015. 10 | */ 11 | public class Issue91 { 12 | 13 | public static void main(String[] args) throws Exception { 14 | FSTConfiguration config = FSTConfiguration.createDefaultConfiguration(); 15 | FSTObjectOutput out = config.getObjectOutput(); 16 | out.writeObject("foobar"); 17 | byte[] savedBuffer = out.getCopyOfWrittenBuffer(); 18 | byte[] reallySavedBuffer = Arrays.copyOf(savedBuffer, savedBuffer.length); 19 | 20 | System.out.println(Arrays.equals(savedBuffer, reallySavedBuffer)); 21 | 22 | out.resetForReUse(); 23 | out.writeObject("blah"); 24 | byte[] secondBuffer = out.getCopyOfWrittenBuffer(); 25 | 26 | System.out.println(Arrays.equals(savedBuffer, reallySavedBuffer)); 27 | 28 | config.getObjectInput(savedBuffer); 29 | config.getObjectInputCopyFrom(secondBuffer, 0, secondBuffer.length); 30 | System.out.println(Arrays.equals(savedBuffer, reallySavedBuffer)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/ser/JSonUnmodifiableTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.junit.Test; 4 | import org.nustaq.serialization.FSTConfiguration; 5 | 6 | /** 7 | * Created by ruedi on 24/05/16. 8 | */ 9 | public class JSonUnmodifiableTest extends UmodifiableTest { 10 | 11 | @Override @Test 12 | public void testUnmodifiableMap() { 13 | super.testUnmodifiableMap(); 14 | } 15 | 16 | @Test 17 | public void testUnmodifiableOrderedMap() { 18 | super.testUnmodifiableOrderedMap(); 19 | } 20 | 21 | @Override @Test 22 | public void testUnmodifiableList() { 23 | super.testUnmodifiableList(); 24 | } 25 | 26 | @Override 27 | protected FSTConfiguration getConfiguration() { 28 | return FSTConfiguration.createJsonNoRefConfiguration(); 29 | } 30 | 31 | @Override @Test 32 | public void testUnmodifiableLinkedList() { 33 | super.testUnmodifiableLinkedList(); 34 | } 35 | 36 | @Override @Test 37 | public void testUnmodifiableLinkedHashMap() { 38 | super.testUnmodifiableLinkedHashMap(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/ser/LineageTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import java.io.Serializable; 4 | import java.util.Arrays; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.*; 8 | import static org.nustaq.serialization.FSTClazzLineageInfo.*; 9 | 10 | /** 11 | * Created by odd on 2017-03-09. 12 | */ 13 | public class LineageTest { 14 | public static class O {} 15 | public static class OO extends O {} 16 | public interface I {} 17 | public interface II extends I {} 18 | public static class OIO extends O implements I {} 19 | public static class OIOO extends OIO {} 20 | public static class OIOOIO extends OIOO implements I {} 21 | public static class OIOIIO extends OIO implements II {} 22 | public static class OIOOIIO extends OIOO implements II {} 23 | 24 | @Test 25 | public void testSpecificity() { 26 | assertSpecificity(null, 0); 27 | assertSpecificity(Object.class, 0); 28 | assertSpecificity(Serializable.class, 1); 29 | assertSpecificity(String.class, 4); 30 | assertSpecificity(O.class, 1); 31 | assertSpecificity(OO.class, 2); 32 | assertSpecificity(I.class, 1); 33 | assertSpecificity(II.class, 2); 34 | assertSpecificity(OIO.class, 3); 35 | assertSpecificity(OIOO.class, 4); 36 | assertSpecificity(OIOOIO.class, 6); 37 | assertSpecificity(OIOIIO.class, 6); 38 | assertSpecificity(OIOOIIO.class, 7); 39 | } 40 | 41 | @Test 42 | public void testLineage() { 43 | assertLineage(null); 44 | assertLineage(Object.class, Object.class); 45 | assertLineage(Serializable.class, Serializable.class); 46 | assertLineage(String.class, String.class, Serializable.class, Comparable.class, CharSequence.class, Object.class); 47 | assertLineage(O.class, O.class, Object.class); 48 | assertLineage(OO.class, OO.class, O.class, Object.class); 49 | assertLineage(I.class, I.class); 50 | assertLineage(II.class, II.class, I.class); 51 | assertLineage(OIO.class, OIO.class, O.class, I.class, Object.class); 52 | assertLineage(OIOO.class, OIOO.class, OIO.class, O.class, I.class, Object.class); 53 | assertLineage(OIOOIO.class, OIOOIO.class, OIOO.class, OIO.class, O.class, I.class, Object.class); 54 | assertLineage(OIOIIO.class, OIOIIO.class, OIO.class, II.class, O.class, I.class, Object.class); 55 | assertLineage(OIOOIIO.class, OIOOIIO.class, OIOO.class, OIO.class, II.class, O.class, I.class, Object.class); 56 | } 57 | 58 | private void assertSpecificity(final Class clazz, final int expected) { 59 | assertEquals(clazz != null ? clazz.getSimpleName() : "null" + " is " + expected, expected, getSpecificity(clazz)); 60 | } 61 | private void assertLineage(final Class clazz, final Class... expected) { 62 | assertArrayEquals(clazz != null ? clazz.getSimpleName() : "null" + " is " + Arrays.toString(expected), expected, getLineage(clazz)); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/ser/OverflowTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.Externalizable; 5 | import java.io.IOException; 6 | import java.io.ObjectInput; 7 | import java.io.ObjectOutput; 8 | 9 | import org.junit.Ignore; 10 | import org.junit.Test; 11 | import org.nustaq.serialization.FSTConfiguration; 12 | import org.nustaq.serialization.FSTObjectInput; 13 | import org.nustaq.serialization.FSTObjectOutput; 14 | 15 | public class OverflowTest { 16 | 17 | @Test 18 | @Ignore 19 | public void test() throws Throwable { 20 | FSTConfiguration fc = FSTConfiguration.createDefaultConfiguration(); 21 | 22 | byte[] b; 23 | int len; 24 | try (FSTObjectOutput foo = new FSTObjectOutput(fc)) { 25 | foo.writeObject(new Outer()); 26 | b = foo.getBuffer(); 27 | len = foo.getWritten(); 28 | } 29 | ByteArrayInputStream bais = new ByteArrayInputStream(b, 0, len); 30 | FSTObjectInput foi = fc.getObjectInput(bais); 31 | foi.readObject(); 32 | } 33 | 34 | // Up to about stream offset 16K there is this other table. This needs to 35 | // be big enough to break past that and also produce enough stack frames to 36 | // overflow. 37 | private static final int OUTER_CT = 30000; 38 | 39 | public static class Outer implements Externalizable { 40 | @Override 41 | public void writeExternal(ObjectOutput out) throws IOException { 42 | for (int i = 0; i < OUTER_CT; ++i) { 43 | out.writeObject(new Inner()); 44 | } 45 | } 46 | 47 | @Override 48 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 49 | for (int i = 0; i < OUTER_CT; ++i) { 50 | in.readObject(); 51 | } 52 | } 53 | } 54 | 55 | public static class Inner implements Externalizable { 56 | @Override 57 | public void writeExternal(ObjectOutput out) throws IOException { 58 | // An array of length 0 produces a period of 7 and we want to 59 | // target 23 which is the hash table size as picked up in the 60 | // debugger 61 | out.writeObject(new byte[16]); 62 | } 63 | 64 | @Override 65 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 66 | in.readObject(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/ser/Play.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import com.cedarsoftware.util.DeepEquals; 4 | import org.nustaq.serialization.*; 5 | 6 | import java.awt.*; 7 | import java.io.Serializable; 8 | 9 | /** 10 | * Created by ruedi on 23/05/15. 11 | */ 12 | public class Play implements Serializable { 13 | 14 | public Play() { 15 | } 16 | 17 | String str[]; 18 | 19 | public static class SimpleClass implements Serializable { 20 | String name = "You"; 21 | double aDouble = 13.3456; 22 | int integers[] = { 1,2,3,4,5 }; 23 | short shorts[] = { 1,2,3,4,5 }; 24 | 25 | Object objects[] = { 1,2,"Bla", new Point(1,2) }; 26 | } 27 | 28 | public static class SampleClass implements Serializable { 29 | String a = "bla bla bla bla bla bla bla bla bla bla bla bla bla "; 30 | Object b = a; 31 | } 32 | 33 | public static class EmptyClass implements Serializable { 34 | int x = 123123; 35 | } 36 | 37 | public static void main(String[] args) { 38 | FSTObjectRegistry.POS_MAP_SIZE = 1; 39 | FSTConfiguration conf = FSTConfiguration.createJsonNoRefConfiguration(); 40 | System.out.println(conf.asObject(conf.asByteArray("∂ √ ∞ ∑ ≈ ≠ ≡ ≤ ≥ ∆ ∏ ∩ ⌠ ⌡ │ ∫ Ω ⅓ ⅔ ⅛ ⅜ ⅝ ⅞ ♭ ♮ ♯ ♩ ♪ © ® ™"))); 41 | 42 | 43 | conf.registerCrossPlatformClassMappingUseSimpleName( 44 | SampleClass.class, 45 | Object[].class, 46 | Object[][].class, 47 | int[][].class, 48 | int[][][].class 49 | ); 50 | 51 | 52 | // Object p = new SimpleClass(); 53 | Object p = new SampleClass(); 54 | System.out.println(conf.asJsonString(p)); 55 | byte[] bytes = conf.asByteArray(p); 56 | Object deser = conf.asObject(bytes); 57 | System.out.println(DeepEquals.deepEquals(p,deser)); 58 | while( true ) 59 | sb(conf); 60 | } 61 | 62 | protected static void sb(FSTConfiguration conf) { 63 | long tim = System.currentTimeMillis(); 64 | EmptyClass ec = new EmptyClass(); 65 | for ( int i = 0; i < 1_000_000; i++ ) { 66 | byte[] bytes = conf.asByteArray(ec); 67 | Object deser = conf.asObject(bytes); 68 | if ( deser == null ) { 69 | System.out.println("POK"); 70 | } 71 | } 72 | long dur = System.currentTimeMillis()-tim; 73 | System.out.println("dur:"+dur); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/ser/ReadResolve.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.nustaq.serialization.FSTConfiguration; 4 | import org.nustaq.serialization.FSTObjectInput; 5 | import org.nustaq.serialization.FSTObjectOutput; 6 | import org.junit.Test; 7 | 8 | import java.io.Serializable; 9 | 10 | public class ReadResolve 11 | { 12 | public static class Holder implements Serializable { 13 | private Object o; 14 | private Object o2; 15 | } 16 | 17 | public static class ToRead implements Serializable { 18 | private final String string; 19 | 20 | public ToRead(String string) { 21 | this.string = string; 22 | } 23 | 24 | private Object readResolve() { 25 | return string; 26 | } 27 | } 28 | 29 | public static void checkEquals( Object a, Object b ) { 30 | if ( ! a.equals(b) ) { 31 | throw new RuntimeException("fail"); 32 | } 33 | } 34 | 35 | @Test 36 | public void testReadResolve() throws Exception { 37 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 38 | 39 | Holder holder = new Holder(); 40 | holder.o = new ToRead("foo"); 41 | holder.o2 = holder.o; 42 | 43 | byte[] b = null; 44 | FSTObjectOutput out = new FSTObjectOutput(conf); 45 | out.writeObject(holder); 46 | out.flush(); 47 | b = out.getBuffer(); 48 | 49 | FSTObjectInput in = new FSTObjectInput(conf); 50 | in.resetForReuseUseArray(b,b.length); 51 | Object res = in.readObject(); 52 | 53 | checkEquals(Holder.class, res.getClass()); 54 | checkEquals(String.class, ((Holder) res).o.getClass()); 55 | checkEquals("foo", ((Holder) res).o); 56 | 57 | checkEquals(String.class, ((Holder) res).o2.getClass()); 58 | checkEquals("foo", ((Holder) res).o2); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/test/ser/SetupAlloc.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import org.nustaq.serialization.FSTConfiguration; 4 | 5 | /** 6 | * Created by ruedi on 07.11.2014. 7 | */ 8 | public class SetupAlloc { 9 | 10 | public static void main(String arg[]) { 11 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 12 | 13 | long tim = System.currentTimeMillis(); 14 | 15 | for ( int i = 0; i < 1000*1000; i++) { 16 | // conf.getObjectOutput(); 17 | conf.getObjectInput(new byte[10]); 18 | } 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/test/ser/TestFastSerialization.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.ObjectInput; 7 | import java.io.ObjectInputStream; 8 | import java.io.ObjectOutput; 9 | import java.io.ObjectOutputStream; 10 | 11 | import org.junit.Assert; 12 | import org.junit.Test; 13 | import org.nustaq.serialization.FSTConfiguration; 14 | import org.nustaq.serialization.FSTObjectOutput; 15 | import ser.externalizable.ExternalizableTestClass; 16 | 17 | 18 | public class TestFastSerialization { 19 | 20 | @Test 21 | public void serializationTest() throws IOException, ClassNotFoundException { 22 | 23 | // Instantiation of the ExternalizableTestClass object 24 | int integer = 10; 25 | String path = "path"; 26 | ExternalizableTestClass object = new ExternalizableTestClass(integer, path); 27 | 28 | // when serialized and deserialized, the integer value should be 29 | // overwritten by readResolve and set to 1. 30 | 31 | // Java 32 | // 1. serialize 33 | byte[] data1; 34 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { 35 | out.writeObject(object); 36 | data1 = bos.toByteArray(); 37 | } 38 | 39 | // 2. deserialize 40 | ExternalizableTestClass object1; 41 | try (ByteArrayInputStream bis = new ByteArrayInputStream(data1); ObjectInput in = new ObjectInputStream(bis)) { 42 | object1 = (ExternalizableTestClass) in.readObject(); 43 | } 44 | 45 | Assert.assertEquals(1, object1.getInteger()); 46 | 47 | // FST 48 | // 1. serialize 49 | FSTConfiguration config = FSTConfiguration.createDefaultConfiguration(); 50 | byte[] data2 = config.asByteArray(object); 51 | 52 | // 2. deserialize 53 | ExternalizableTestClass object2 = (ExternalizableTestClass) config.asObject(data2); 54 | 55 | Assert.assertEquals(1, object2.getInteger()); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/test/ser/TestWriteReplaceInList.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.io.ByteArrayOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectInput; 9 | import java.io.ObjectStreamException; 10 | import java.io.Serializable; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import org.junit.Test; 15 | import org.nustaq.serialization.FSTConfiguration; 16 | import org.nustaq.serialization.FSTObjectInput; 17 | import org.nustaq.serialization.FSTObjectOutput; 18 | import org.nustaq.serialization.FSTObjectRegistry; 19 | 20 | public class TestWriteReplaceInList { 21 | 22 | @Test 23 | public void testWriteReplaceInList() throws IOException, ClassNotFoundException { 24 | Container c = new Container(); 25 | 26 | BaseClass b1 = new BaseClass(); 27 | b1.value = "morphMe"; 28 | c.list.add(b1); 29 | BaseClass b2 = new BaseClass(); 30 | b2.value = "morphMe"; 31 | c.list.add(b2); 32 | 33 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); 34 | FSTObjectOutput out = new FSTObjectOutput(buf, getTestConfiguration()); 35 | out.writeObject(c); 36 | out.close(); 37 | 38 | ObjectInput in = getTestConfiguration().getObjectInput(new ByteArrayInputStream(buf.toByteArray())); 39 | Container res = (Container) in.readObject(); 40 | assertEquals("you have morphed", ((Morpher) res.list.get(0)).value); 41 | assertEquals("you have morphed", ((Morpher) res.list.get(1)).value); 42 | } 43 | 44 | @org.junit.Before 45 | public void setUp() throws Exception { 46 | FSTObjectRegistry.POS_MAP_SIZE = 1; 47 | } 48 | 49 | protected FSTConfiguration getTestConfiguration() { 50 | FSTConfiguration.isAndroid = false; 51 | return FSTConfiguration.createDefaultConfiguration(); 52 | } 53 | 54 | public static class BaseClass implements Serializable { 55 | public String value; 56 | 57 | private Object writeReplace() throws ObjectStreamException { 58 | if (value.equals("morphMe")) { 59 | Morpher m = new Morpher(); 60 | m.value = "you have morphed"; 61 | return m; 62 | } 63 | return this; 64 | } 65 | } 66 | 67 | public static class Morpher extends BaseClass { 68 | } 69 | 70 | public static class Container implements Serializable { 71 | public List list = new ArrayList<>(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/test/ser/UmodifiableTest.java: -------------------------------------------------------------------------------- 1 | package ser; 2 | 3 | import com.cedarsoftware.util.DeepEquals; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | 7 | import java.util.*; 8 | 9 | import static org.junit.Assert.assertTrue; 10 | 11 | /** 12 | * Created by ruedi on 22/05/16. 13 | */ 14 | public class UmodifiableTest { 15 | 16 | @Test 17 | public void testUnmodifiableMap() { 18 | FSTConfiguration conf = getConfiguration(); 19 | HashMap map = new HashMap(); 20 | map.put("Hello", new Date()); 21 | Map un = Collections.unmodifiableMap(map); 22 | Map res = (Map) conf.asObject(conf.asByteArray(un)); 23 | assertTrue(DeepEquals.deepEquals(res, un)); 24 | } 25 | 26 | @Test 27 | public void testUnmodifiableOrderedMap() { 28 | FSTConfiguration conf = getConfiguration(); 29 | LinkedHashMap map = new LinkedHashMap(); 30 | map.put("Hello", new Date()); 31 | Map un = Collections.unmodifiableMap(map); 32 | Map res = (Map) conf.asObject(conf.asByteArray(un)); 33 | assertTrue(DeepEquals.deepEquals(res, un)); 34 | } 35 | 36 | @Test 37 | public void testUnmodifiableList() { 38 | FSTConfiguration conf = getConfiguration(); 39 | ArrayList list = new ArrayList(); 40 | list.add("Hello"); 41 | list.add(new Date()); 42 | List un = Collections.unmodifiableList(list); 43 | List res = (List) conf.asObject(conf.asByteArray(un)); 44 | assertTrue(DeepEquals.deepEquals(res,un)); 45 | } 46 | 47 | protected FSTConfiguration getConfiguration() { 48 | return FSTConfiguration.createDefaultConfiguration(); 49 | } 50 | 51 | @Test 52 | public void testUnmodifiableLinkedList() { 53 | FSTConfiguration conf = getConfiguration(); 54 | List list = new LinkedList(); 55 | list.add("Hello"); 56 | list.add(new Date()); 57 | List un = Collections.unmodifiableList(list); 58 | List res = (List) conf.asObject(conf.asByteArray(un)); 59 | assertTrue(DeepEquals.deepEquals(res,un)); 60 | } 61 | 62 | @Test 63 | public void testUnmodifiableLinkedHashMap() { 64 | FSTConfiguration conf = getConfiguration(); 65 | 66 | Map m1 = new LinkedHashMap(); 67 | m1.put("a", 1); 68 | m1.put("BB", 1); 69 | m1.put("ccc", 1); 70 | m1.put("aa", 1); 71 | m1.put("BBa", 1); 72 | m1.put("ccca", 1); 73 | m1 = Collections.unmodifiableMap(m1); 74 | 75 | Map un = Collections.unmodifiableMap(m1); 76 | Map res = (Map) conf.asObject(conf.asByteArray(un)); 77 | assertTrue(DeepEquals.deepEquals(res,un)); 78 | 79 | System.out.println(res.keySet()); 80 | System.out.println(m1.keySet()); 81 | } 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/test/ser/androidbitset/AndroidBigInt.java: -------------------------------------------------------------------------------- 1 | package ser.androidbitset; 2 | 3 | /** 4 | * Created by ruedi on 08/02/15. 5 | */ 6 | 7 | import java.io.IOException; 8 | import java.io.ObjectInputStream; 9 | import java.io.ObjectOutputStream; 10 | import java.io.Serializable; 11 | import java.util.Random; 12 | 13 | /** 14 | * An immutable signed integer of arbitrary magnitude. 15 | * 16 | *

Fast Cryptography

17 | * This implementation is efficient for operations traditionally used in 18 | * cryptography, such as the generation of large prime numbers and computation 19 | * of the modular inverse. 20 | * 21 | *

Slow Two's Complement Bitwise Operations

22 | * This API includes operations for bitwise operations in two's complement 23 | * representation. Two's complement is not the internal representation used by 24 | * this implementation, so such methods may be inefficient. Use {@link 25 | * java.util.BitSet} for high-performance bitwise operations on 26 | * arbitrarily-large sequences of bits. 27 | */ 28 | public class AndroidBigInt extends Number implements Serializable { 29 | 30 | /** sign field, used for serialization. */ 31 | private int signum; 32 | 33 | /** absolute value field, used for serialization */ 34 | private byte[] magnitude; 35 | 36 | public AndroidBigInt(int signum, byte[] magnitude) { 37 | this.signum = signum; 38 | this.magnitude = magnitude; 39 | } 40 | 41 | /** 42 | * Assigns all transient fields upon deserialization of a {@code AndroidBigInt} 43 | * instance. 44 | */ 45 | private void readObject(ObjectInputStream in) 46 | throws IOException, ClassNotFoundException { 47 | in.defaultReadObject(); 48 | // BigInt bigInt = new BigInt(); 49 | // bigInt.putBigEndian(magnitude, signum < 0); 50 | // setBigInt(bigInt); 51 | } 52 | 53 | /** 54 | * Prepares this {@code AndroidBigInt} for serialization, i.e. the 55 | * non-transient fields {@code signum} and {@code magnitude} are assigned. 56 | */ 57 | private void writeObject(ObjectOutputStream out) throws IOException { 58 | // BigInt bigInt = getBigInt(); 59 | // signum = bigInt.sign(); 60 | // magnitude = bigInt.bigEndianMagnitude(); 61 | out.defaultWriteObject(); 62 | } 63 | 64 | @Override 65 | public int intValue() { 66 | return 0; 67 | } 68 | 69 | @Override 70 | public long longValue() { 71 | return 0; 72 | } 73 | 74 | @Override 75 | public float floatValue() { 76 | return 0; 77 | } 78 | 79 | @Override 80 | public double doubleValue() { 81 | return 0; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/test/ser/bighm/BigObjTest.java: -------------------------------------------------------------------------------- 1 | package ser.bighm; 2 | 3 | import com.cedarsoftware.util.DeepEquals; 4 | import junit.framework.Assert; 5 | import org.junit.Test; 6 | import org.nustaq.serialization.FSTConfiguration; 7 | import org.nustaq.serialization.FSTObjectInput; 8 | import org.nustaq.serialization.FSTObjectOutput; 9 | 10 | import java.io.FileInputStream; 11 | import java.io.FileOutputStream; 12 | import java.io.IOException; 13 | import java.io.Serializable; 14 | import java.util.HashMap; 15 | import java.util.Iterator; 16 | 17 | /** 18 | * Created by ruedi on 22/02/15. 19 | */ 20 | public class BigObjTest { 21 | 22 | static class TestCL implements Serializable { 23 | double sd = Math.random(); 24 | double avg = Math.random(); 25 | } 26 | 27 | public HashMap createHM() { 28 | HashMap hm = new HashMap(); 29 | for ( int i=0; i < 500000; i++ ) { 30 | hm.put(i,Math.random()); 31 | } 32 | return hm; 33 | } 34 | 35 | public HashMap createHM1(HashMap hmk) { 36 | HashMap hm = new HashMap(); 37 | for (Iterator iterator = hmk.keySet().iterator(); iterator.hasNext(); ) { 38 | Object next = iterator.next(); 39 | hm.put(next,new TestCL()); 40 | } 41 | return hm; 42 | } 43 | 44 | @Test 45 | public void testHM() throws IOException, ClassNotFoundException { 46 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 47 | HashMap hm = createHM(); 48 | HashMap hm1 = createHM1(hm); 49 | FSTObjectOutput oo = conf.getObjectOutput(); 50 | oo.writeObject(hm); 51 | oo.writeObject(hm1); 52 | byte[] copyOfWrittenBuffer = oo.getCopyOfWrittenBuffer(); 53 | 54 | FSTObjectInput in = conf.getObjectInput(copyOfWrittenBuffer); 55 | Object o1 = in.readObject(); 56 | Object o2 = in.readObject(); 57 | Assert.assertTrue(DeepEquals.deepEquals(o1,hm)); 58 | Assert.assertTrue(DeepEquals.deepEquals(o2,hm1)); 59 | } 60 | 61 | @Test 62 | public void testWithStreams() throws IOException, ClassNotFoundException { 63 | HashMap hm = createHM(); 64 | HashMap hm1 = createHM1(hm); 65 | 66 | FSTObjectOutput out = new FSTObjectOutput(new FileOutputStream("../epsis.oos")); 67 | out.writeObject(hm); 68 | out.writeObject(hm1); 69 | out.close(); 70 | 71 | FSTObjectInput in = new FSTObjectInput(new FileInputStream("../epsis.oos")); 72 | HashMap o1 = (HashMap) in.readObject(); 73 | HashMap o2 = (HashMap) in.readObject(); 74 | in.close(); 75 | Assert.assertTrue(DeepEquals.deepEquals(o1, hm)); 76 | Assert.assertTrue(DeepEquals.deepEquals(o2, hm1)); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/test/ser/externalizable/ExternalizableTestClass.java: -------------------------------------------------------------------------------- 1 | package ser.externalizable; 2 | import java.io.Externalizable; 3 | import java.io.IOException; 4 | import java.io.ObjectInput; 5 | import java.io.ObjectOutput; 6 | import java.io.ObjectStreamException; 7 | 8 | 9 | public class ExternalizableTestClass implements Externalizable { 10 | 11 | 12 | private String path; 13 | private int integer; 14 | 15 | public ExternalizableTestClass() { 16 | } 17 | 18 | public ExternalizableTestClass(int integer, String path) { 19 | this.path = path; 20 | this.integer = integer; 21 | } 22 | 23 | @Override 24 | public void writeExternal(ObjectOutput out) throws IOException { 25 | out.writeUTF(getPath()); 26 | } 27 | 28 | @Override 29 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 30 | setPath(in.readUTF()); 31 | } 32 | 33 | public Object readResolve() throws ObjectStreamException { 34 | return new ExternalizableTestClass(1, this.getPath()); 35 | } 36 | 37 | public String getPath() { 38 | return path; 39 | } 40 | 41 | public void setPath(String path) { 42 | this.path = path; 43 | } 44 | 45 | public int getInteger() { 46 | return integer; 47 | } 48 | 49 | public void setInteger(int integer) { 50 | this.integer = integer; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/ser/hzcastblog/Order.java: -------------------------------------------------------------------------------- 1 | package ser.hzcastblog; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | /** 9 | * from hzcast blog post 10 | */ 11 | public class Order implements Serializable { 12 | 13 | public long orderId; 14 | public Date date; 15 | public List orderLines = new LinkedList(); 16 | 17 | } -------------------------------------------------------------------------------- /src/test/ser/hzcastblog/OrderLine.java: -------------------------------------------------------------------------------- 1 | package ser.hzcastblog; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * from hzcast blog 7 | */ 8 | public class OrderLine implements Serializable { 9 | public String product; 10 | public int amount; 11 | } -------------------------------------------------------------------------------- /src/test/ser/hzcastblog/RunTest.java: -------------------------------------------------------------------------------- 1 | package ser.hzcastblog; 2 | 3 | import org.nustaq.serialization.simpleapi.DefaultCoder; 4 | 5 | import java.util.Date; 6 | import java.util.Random; 7 | 8 | /** 9 | * Created by ruedi on 24/12/14. 10 | */ 11 | public class RunTest { 12 | 13 | static String[] products; 14 | static int maxOrderLines = 5; 15 | 16 | static Order createNewOrder(Random random, int id) { 17 | Order order = new Order(); 18 | order.orderId = random.nextInt(id); 19 | order.date = new Date(); 20 | 21 | int orderlineCount = random.nextInt(maxOrderLines); 22 | for (int k = 0; k < orderlineCount; k++) { 23 | OrderLine orderLine = new OrderLine(); 24 | orderLine.amount = random.nextInt(100); 25 | orderLine.product = products[random.nextInt(products.length)]; 26 | order.orderLines.add(orderLine); 27 | } 28 | 29 | return order; 30 | } 31 | 32 | public static void main( String arg[] ) { 33 | Random r = new Random(1000); 34 | products = new String[100]; 35 | for (int k = 0; k < 100; k++) { 36 | products[k] = "product-" + k; 37 | } 38 | 39 | Order orders[] = new Order[10000]; 40 | for (int i = 0; i < orders.length; i++) { 41 | orders[i] = createNewOrder(r,i+1); 42 | } 43 | 44 | int curOrder = 0; 45 | DefaultCoder coder = new DefaultCoder(true,Order.class,OrderLine.class); 46 | // DefaultCoder coder = new DefaultCoder(false,Order.class,OrderLine.class); faster, no ref sharing 47 | byte buf[] = new byte[10000]; // reuse 48 | // write once for decode test only 49 | coder.toByteArray(orders[0], buf, 0, buf.length); 50 | 51 | int count = 0; 52 | 53 | while( true ) { 54 | long tim = System.currentTimeMillis(); 55 | for ( int i = 0; i < 1_000_0000; i++ ) { 56 | coder.toByteArray(orders[count], buf, 0, buf.length); 57 | // Object deser = coder.toObject(buf); 58 | } 59 | System.out.println("time: "+(System.currentTimeMillis()-tim) ); 60 | } 61 | 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/ser/issue52/I52.java: -------------------------------------------------------------------------------- 1 | package ser.issue52; 2 | 3 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | 7 | import java.math.BigInteger; 8 | import java.security.KeyPair; 9 | import java.security.KeyPairGenerator; 10 | import java.security.SecureRandom; 11 | import java.security.Security; 12 | import java.security.spec.RSAKeyGenParameterSpec; 13 | 14 | /** 15 | * Created by ruedi on 01/02/15. 16 | */ 17 | public class I52 { 18 | 19 | // Fermat F4, largest known fermat prime 20 | private static final BigInteger PUBLIC_EXP = new BigInteger("10001", 16);; 21 | private static final int STRENGTH = 1024; 22 | 23 | @Test 24 | public void test() throws Exception { 25 | // install BouncyCastle provider 26 | Security.addProvider(new BouncyCastleProvider()); 27 | 28 | // generate a keypair 29 | KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "BC"); 30 | RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(STRENGTH, PUBLIC_EXP); 31 | gen.initialize(params, new SecureRandom()); 32 | KeyPair keyPair = gen.generateKeyPair(); 33 | 34 | FSTConfiguration fst = FSTConfiguration.createDefaultConfiguration(); 35 | 36 | // serialize 37 | byte[] serialized = fst.asByteArray(keyPair); 38 | 39 | // deserialize --> crash 40 | KeyPair deserialized = (KeyPair) fst.asObject(serialized); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/ser/jsonbench/JSonBench.java: -------------------------------------------------------------------------------- 1 | package ser.jsonbench; 2 | 3 | import org.nustaq.serialization.FSTConfiguration; 4 | 5 | import javax.json.Json; 6 | import javax.json.JsonObject; 7 | import javax.json.JsonReader; 8 | import javax.json.JsonWriter; 9 | import java.io.ByteArrayInputStream; 10 | import java.io.ByteArrayOutputStream; 11 | import java.io.IOException; 12 | import java.io.Serializable; 13 | 14 | /** 15 | * Created by ruedi on 02/07/15. 16 | */ 17 | public class JSonBench { 18 | 19 | public static void javaxTest(JsonObject obj) { 20 | try (ByteArrayOutputStream oos = new ByteArrayOutputStream(); 21 | JsonWriter writer = Json.createWriter(oos)) { 22 | writer.writeObject(obj); 23 | byte b[] = oos.toByteArray(); 24 | try (ByteArrayInputStream bais = new ByteArrayInputStream(b)) { 25 | JsonReader reader = Json.createReader(bais); 26 | JsonObject jsonObject = reader.readObject(); 27 | } 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | static FSTConfiguration conf = FSTConfiguration.createJsonConfiguration(false, false); 34 | public static void fstJson(Serializable ser) { 35 | byte[] bytes = conf.asByteArray(ser); 36 | Object o = conf.asObject(bytes); 37 | } 38 | 39 | 40 | public static class SerObj implements Serializable { 41 | int num; 42 | double dbl; 43 | String str; 44 | 45 | public SerObj() { 46 | num = 999; 47 | dbl = 1.234; 48 | str = "Hello JSON"; 49 | } 50 | } 51 | 52 | public static void main( String a[] ) { 53 | 54 | JsonObject json = Json.createObjectBuilder().add("num",999).add("dbl",1.234).add("str", "Hello JSON").build(); 55 | 56 | SerObj ser = new SerObj(); 57 | conf.registerCrossPlatformClassMappingUseSimpleName(SerObj.class); 58 | 59 | while( true ) { 60 | long now = System.currentTimeMillis(); 61 | for ( int i = 0; i < 10_000; i++ ) { 62 | javaxTest(json); 63 | } 64 | System.out.println("javax: "+(System.currentTimeMillis()-now)); 65 | 66 | now = System.currentTimeMillis(); 67 | for ( int i = 0; i < 10_000; i++ ) { 68 | fstJson(ser); 69 | } 70 | System.out.println("fst: "+(System.currentTimeMillis()-now)); 71 | 72 | } 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/test/ser/offheaplatency/MktDataDispatcher.java: -------------------------------------------------------------------------------- 1 | package ser.offheaplatency; 2 | 3 | import java.util.*; 4 | import java.util.concurrent.*; 5 | 6 | public final class MktDataDispatcher implements Runnable{ 7 | 8 | private volatile boolean keepDispatching; 9 | private final ExecutorService service; 10 | private final MyFSTSerializer serializer; 11 | private final MktDataListener listener; 12 | private final AbstractQueue eventQueue; 13 | 14 | public MktDataDispatcher( int queueSize, MyFSTSerializer serializer, MktDataListener listener ){ 15 | this.serializer = serializer; 16 | this.listener = listener; 17 | this.eventQueue = new ArrayBlockingQueue( queueSize ); 18 | this.service = Executors.newFixedThreadPool( 1 ); 19 | } 20 | 21 | public final void start( ){ 22 | serializer.start( ); 23 | keepDispatching = true; 24 | service.execute( this ); 25 | } 26 | 27 | public final boolean enqueue( final MktDataEvent event ){ 28 | return eventQueue.offer( event ); 29 | } 30 | 31 | @Override 32 | public final void run( ){ 33 | 34 | while( keepDispatching ){ 35 | 36 | try{ 37 | MktDataEvent event = eventQueue.poll(); 38 | if( event == null ){ 39 | // Thread.yield(); 40 | continue; 41 | } 42 | 43 | if( serializer.toStore() ){ 44 | serializer.storeEvent( event ); 45 | } 46 | listener.update( event ); 47 | 48 | }catch( Exception e ){ 49 | e.printStackTrace( ); 50 | } 51 | } 52 | } 53 | 54 | protected final int getQueueSize( ){ 55 | return eventQueue.size( ); 56 | } 57 | 58 | public final void stop(){ 59 | serializer.stop( ); 60 | keepDispatching = false; 61 | service.shutdown(); 62 | } 63 | 64 | public interface MktDataListener{ 65 | public boolean update( MktDataEvent event ); 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /src/test/ser/offheaplatency/MktDataEvent.java: -------------------------------------------------------------------------------- 1 | package ser.offheaplatency; 2 | 3 | import java.io.Serializable; 4 | import java.util.concurrent.atomic.AtomicLong; 5 | 6 | /** 7 | * Created by ruedi on 19/06/15. 8 | */ 9 | public final class MktDataEvent implements Serializable { 10 | 11 | private final long sequenceId; 12 | private final long creationTime; 13 | private final String symbol; 14 | private final double bidPrice; 15 | private final long bidQuantity; 16 | private final double askPrice; 17 | private final long askQuantity; 18 | 19 | private final static long serialVersionUID = 1L; 20 | private final static AtomicLong SEQUENCE = new AtomicLong(); 21 | 22 | public MktDataEvent(String symbol, double bidPrice, long bidQuantity, double askPrice, long askQuantity){ 23 | 24 | this.creationTime = System.nanoTime( ); 25 | this.sequenceId = SEQUENCE.incrementAndGet(); 26 | this.symbol = symbol; 27 | this.bidPrice = bidPrice; 28 | this.bidQuantity = bidQuantity; 29 | this.askPrice = askPrice; 30 | this.askQuantity = askQuantity; 31 | } 32 | 33 | public final long getSequenceId( ){ 34 | return sequenceId; 35 | } 36 | 37 | public final long getCreationTime( ){ 38 | return creationTime; 39 | } 40 | 41 | public final String getSymbol(){ 42 | return symbol; 43 | } 44 | 45 | public final double getBidPrice( ){ 46 | return bidPrice; 47 | } 48 | 49 | public final long getBidQuantity( ){ 50 | return bidQuantity; 51 | } 52 | 53 | public final double getAskPrice( ){ 54 | return askPrice; 55 | } 56 | 57 | public final long getAskQuantity( ){ 58 | return askQuantity; 59 | } 60 | } -------------------------------------------------------------------------------- /src/test/ser/offheaplatency/MyFSTSerializer.java: -------------------------------------------------------------------------------- 1 | package ser.offheaplatency; 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | import org.nustaq.offheap.*; 6 | import org.nustaq.serialization.simpleapi.*; 7 | 8 | public final class MyFSTSerializer{ 9 | 10 | private final boolean toStore; 11 | private final String fileName; 12 | private final long memorySize; 13 | private final FSTCoder fastCoder; 14 | private final FSTLongOffheapMap offHeapMap; 15 | 16 | public MyFSTSerializer( boolean toStore, String location, String journalName, FSTCoder fastCoder, long memorySize, int count ) throws Exception{ 17 | this.toStore = toStore; 18 | this.fileName = location + File.separator + journalName + ".mmf"; 19 | this.memorySize = memorySize; 20 | this.fastCoder = fastCoder; 21 | this.offHeapMap = new FSTLongOffheapMap<>( fileName, memorySize, count, fastCoder ); 22 | // this.offHeapMap = new FSTLongOffheapMap<>( memorySize, 2*count, fastCoder ); 23 | } 24 | 25 | public final boolean toStore( ){ 26 | return toStore; 27 | } 28 | 29 | public final String getFilename( ){ 30 | return fileName; 31 | } 32 | 33 | public final void start( ){ 34 | fastCoder.getConf().setCrossPlatform( false ); 35 | fastCoder.getConf().setPreferSpeed( true ); 36 | fastCoder.getConf().setShareReferences( false ); 37 | fastCoder.getConf().registerClass( Long.class, MktDataEvent.class ); 38 | System.out.println("Journaling started at " + fileName + " with Memory " + memorySize ) ; 39 | } 40 | 41 | public final void storeEvent( MktDataEvent event ){ 42 | offHeapMap.put( event.getSequenceId(), event ); 43 | } 44 | 45 | public final Collection retrieveAllEvents( ){ 46 | Map retrievedMap = new LinkedHashMap<>(); 47 | 48 | for( Iterator iterator = offHeapMap.values(); iterator.hasNext(); ){ 49 | MktDataEvent event = (MktDataEvent) iterator.next(); 50 | retrievedMap.put( event.getSequenceId(), event ); 51 | } 52 | 53 | return retrievedMap.values(); 54 | } 55 | 56 | public final void stop( ){ 57 | try{ 58 | offHeapMap.free( ); 59 | System.out.println("Stopped Journal and freed memory." ); 60 | }catch( Exception e ){ 61 | e.printStackTrace( ); 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /src/test/ser/security/NestedHS.java: -------------------------------------------------------------------------------- 1 | package ser.security; 2 | 3 | import org.nustaq.serialization.FSTConfiguration; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.io.ByteArrayOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectInputStream; 9 | import java.io.ObjectOutputStream; 10 | import java.util.HashSet; 11 | import java.util.Set; 12 | 13 | 14 | /** 15 | * Created by ruedi on 22/11/15. 16 | * 17 | * DOS attack 18 | */ 19 | public class NestedHS { 20 | private static final boolean USEFST = true; 21 | static FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 22 | 23 | // billion-laughs-style DoS for java serialization 24 | 25 | public static void main(String[] args) throws Exception { 26 | deserialize(payload()); 27 | } 28 | 29 | static Object deserialize(byte[] bytes) throws Exception { 30 | if (USEFST) { 31 | return conf.asObject(bytes); 32 | } 33 | return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); 34 | } 35 | 36 | static byte[] payload() throws IOException { 37 | Set root = new HashSet(); 38 | Set s1 = root; 39 | Set s2 = new HashSet(); 40 | for (int i = 0; i < 100; i++) { 41 | Set t1 = new HashSet(); 42 | Set t2 = new HashSet(); 43 | t1.add("foo"); // make it not equal to t2 44 | s1.add(t1); 45 | s1.add(t2); 46 | s2.add(t1); 47 | s2.add(t2); 48 | s1 = t1; 49 | s2 = t2; 50 | } 51 | return serialize(root); 52 | } 53 | 54 | static byte[] serialize(Object o) throws IOException { 55 | if (USEFST) { 56 | return conf.asByteArray(o); 57 | } 58 | ByteArrayOutputStream ba = new ByteArrayOutputStream(); 59 | ObjectOutputStream oos = new ObjectOutputStream(ba); 60 | oos.writeObject(o); 61 | oos.close(); 62 | return ba.toByteArray(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/test/ser/serializerdelegate/CondTest.java: -------------------------------------------------------------------------------- 1 | package ser.serializerdelegate; 2 | 3 | import org.nustaq.serialization.*; 4 | import org.nustaq.serialization.FSTObjectInput.*; 5 | import org.nustaq.serialization.annotations.*; 6 | 7 | import java.io.*; 8 | import java.lang.reflect.*; 9 | import java.util.*; 10 | 11 | /** 12 | * Created by moelrue on 07.07.2015. 13 | */ 14 | public class CondTest { 15 | 16 | static class Unregisterd implements Serializable { 17 | int x = 3; 18 | } 19 | 20 | static class CTest implements Serializable { 21 | 22 | int aNum; 23 | 24 | ArrayList conditional1; 25 | @Conditional ArrayList conditional0; 26 | String aString; 27 | CTest other; 28 | @Conditional Object unregistered; 29 | 30 | public CTest() { 31 | this.aNum = 0; 32 | conditional1 = new ArrayList(); 33 | conditional0 = new ArrayList(); 34 | } 35 | } 36 | 37 | public static void main(String[] args) throws IOException, ClassNotFoundException { 38 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 39 | 40 | conf.registerClass(CTest.class,Unregisterd.class); 41 | 42 | CTest cTest = new CTest(); 43 | cTest.other = new CTest(); 44 | cTest.aNum = 3; 45 | cTest.unregistered = new Unregisterd(); 46 | cTest.conditional0.add(new CTest()); 47 | cTest.conditional0.add(cTest.unregistered); 48 | cTest.conditional0.add(cTest.other); 49 | cTest.conditional1.add(new Unregisterd()); 50 | 51 | byte b[] = conf.asByteArray(cTest); 52 | 53 | FSTObjectInput oin = new FSTObjectInput(new ByteArrayInputStream(b), conf); 54 | oin.setConditionalCallback(new ConditionalCallback() { 55 | @Override 56 | public boolean shouldSkip(Object halfDecoded, int streamPosition, Field field) { 57 | return ((CTest) halfDecoded).aNum == 3; 58 | } 59 | }); 60 | CTest o = (CTest) oin.readObject(); 61 | System.out.println(); 62 | } 63 | } -------------------------------------------------------------------------------- /src/test/ser/serializerdelegate/SerializerDelegateTest.java: -------------------------------------------------------------------------------- 1 | package ser.serializerdelegate; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.*; 6 | 7 | import java.io.IOException; 8 | import java.io.Serializable; 9 | 10 | /** 11 | * Created by ruedi on 07/07/15. 12 | * 13 | * Demonstrates how to patch an object and how to 'null' instances of a specific object. Note the example makes no 14 | * sense as it demonstrates unwrapping and NULL'ing using one serializer. 15 | * 16 | */ 17 | public class SerializerDelegateTest { 18 | 19 | static interface WrapperInterface { 20 | Object getWrapped(); 21 | } 22 | 23 | static class WrappedObject implements WrapperInterface, Serializable { 24 | Object wrapped; 25 | 26 | public WrappedObject(Object wrapped) { 27 | this.wrapped = wrapped; 28 | } 29 | 30 | @Override 31 | public Object getWrapped() { 32 | return wrapped; 33 | } 34 | } 35 | 36 | static class MyExample implements Serializable{ 37 | Object aa = new WrappedObject( "Hello" ); 38 | Object object = "Hello"; 39 | } 40 | 41 | 42 | static class MySkippingSerializer extends FSTBasicObjectSerializer { 43 | 44 | @Override 45 | public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { 46 | out.writeObject(((WrappedObject)toWrite).getWrapped()); 47 | } 48 | 49 | @Override 50 | public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { 51 | in.readObject(); // ensure everything read and registered same order as on write time 52 | 53 | return REALLY_NULL; // but just don't use/return it. 54 | // Note: returning 'null' will result in fst attempting to construct an instance 55 | // and call readObject on the serializer 56 | } 57 | } 58 | 59 | @Test 60 | public void main() { 61 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 62 | final MySkippingSerializer skippingSer = new MySkippingSerializer(); 63 | 64 | conf.setSerializerRegistryDelegate(new FSTSerializerRegistryDelegate() { 65 | @Override 66 | public FSTObjectSerializer getSerializer(Class cl) { 67 | if (WrapperInterface.class.isAssignableFrom(cl)) { 68 | return skippingSer; 69 | } 70 | return null; 71 | } 72 | }); 73 | 74 | MyExample myExample = new MyExample(); 75 | byte[] b = conf.asByteArray(myExample); 76 | 77 | MyExample res = (MyExample) conf.asObject(b); 78 | Assert.assertTrue(res.aa == null); 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/ser/serializers/FSTEnumSetSerializerTest.java: -------------------------------------------------------------------------------- 1 | package ser.serializers; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | 7 | import java.io.Serializable; 8 | import java.util.EnumSet; 9 | import java.util.Set; 10 | 11 | /** 12 | * User: Evgeniy Devyatyh 13 | */ 14 | public class FSTEnumSetSerializerTest { 15 | 16 | private enum SomeEnum { 17 | override { 18 | @Override 19 | public boolean getSomeBool() { 20 | return true; 21 | } 22 | }, plain; 23 | 24 | public boolean getSomeBool() { 25 | return false; 26 | } 27 | } 28 | 29 | static class Clazz implements Serializable { 30 | Set enumSet = EnumSet.of(SomeEnum.override, SomeEnum.plain); 31 | 32 | } 33 | 34 | @Test 35 | public void shouldSerializeEnumSet() throws Exception { 36 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 37 | byte[] bytes = conf.asByteArray(new Clazz()); 38 | Clazz o = (Clazz) conf.asObject(bytes); 39 | Assert.assertEquals(o.enumSet, EnumSet.of(SomeEnum.override, SomeEnum.plain)); 40 | } 41 | 42 | @Test 43 | public void shouldCompatibleSerializeEnumSet() throws Exception { 44 | FSTConfiguration conf = FSTConfiguration.createMinBinConfiguration(); 45 | byte[] bytes = conf.asByteArray(new Clazz()); 46 | Clazz o = (Clazz) conf.asObject(bytes); 47 | Assert.assertEquals(o.enumSet, EnumSet.of(SomeEnum.override, SomeEnum.plain)); 48 | } 49 | 50 | @Test 51 | public void shouldCompatibleSerializeEmptyEnumSet() throws Exception { 52 | FSTConfiguration conf = FSTConfiguration.createMinBinConfiguration(); 53 | Clazz object = new Clazz(); 54 | object.enumSet = EnumSet.noneOf(SomeEnum.class); 55 | byte[] bytes = conf.asByteArray(object); 56 | Clazz o = (Clazz) conf.asObject(bytes); 57 | Assert.assertEquals(o.enumSet, EnumSet.noneOf(SomeEnum.class)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/ser/serializers/FSTJSonUnmodifiableCollectionSerializerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | * 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * 8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package ser.serializers; 15 | 16 | import org.junit.Test; 17 | import org.nustaq.serialization.FSTConfiguration; 18 | import org.nustaq.serialization.serializers.FSTJSonUnmodifiableCollectionSerializer; 19 | 20 | import java.util.Collections; 21 | import java.util.List; 22 | import java.util.Set; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | import static org.junit.Assert.assertTrue; 26 | 27 | /** 28 | * @author Jakub Kubrynski 29 | */ 30 | @SuppressWarnings("unchecked") 31 | public class FSTJSonUnmodifiableCollectionSerializerTest { 32 | 33 | static final String TEST_VALUE = "TestValue"; 34 | 35 | @Test 36 | public void shouldSerializeUnmodifiableList() throws ClassNotFoundException { 37 | //given 38 | List list = Collections.unmodifiableList(Collections.singletonList(TEST_VALUE)); 39 | FSTConfiguration conf = FSTConfiguration.createJsonNoRefConfiguration(); 40 | //when 41 | byte[] bytes = conf.asByteArray((list)); 42 | list = (List) conf.asObject(bytes); 43 | //then 44 | assertTrue(FSTJSonUnmodifiableCollectionSerializer.UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())); 45 | assertEquals(1, list.size()); 46 | assertTrue(list.contains(TEST_VALUE)); 47 | } 48 | 49 | @Test 50 | public void shouldSerializeUnmodifiableSet() throws ClassNotFoundException { 51 | //given 52 | Set set = Collections.unmodifiableSet(Collections.singleton(TEST_VALUE)); 53 | FSTConfiguration conf = FSTConfiguration.createJsonNoRefConfiguration(); 54 | //when 55 | byte[] bytes = conf.asByteArray((set)); 56 | set = (Set) conf.asObject(bytes); 57 | //then 58 | assertTrue(FSTJSonUnmodifiableCollectionSerializer.UNMODIFIABLE_SET_CLASS.isAssignableFrom(set.getClass())); 59 | assertEquals(1, set.size()); 60 | assertTrue(set.contains(TEST_VALUE)); 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /src/test/ser/serializers/FSTJSonUnmodifiableMapSerializerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | * 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * 8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package ser.serializers; 15 | 16 | import org.junit.Test; 17 | import org.nustaq.serialization.FSTConfiguration; 18 | import org.nustaq.serialization.serializers.FSTJSonUnmodifiableMapSerializer; 19 | 20 | import java.util.Collections; 21 | import java.util.Map; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | import static org.junit.Assert.assertTrue; 25 | 26 | /** 27 | * @author Jakub Kubrynski 28 | */ 29 | @SuppressWarnings("unchecked") 30 | public class FSTJSonUnmodifiableMapSerializerTest { 31 | 32 | static final String TEST_VALUE = "TestValue"; 33 | static final String TEST_KEY = "TestKey"; 34 | 35 | @Test 36 | public void shouldSerializeUnmodifiableMap() throws ClassNotFoundException { 37 | //given 38 | Map map = Collections.unmodifiableMap(Collections.singletonMap(TEST_KEY, TEST_VALUE)); 39 | FSTConfiguration conf = FSTConfiguration.createJsonNoRefConfiguration(); 40 | //when 41 | byte[] bytes = conf.asByteArray((map)); 42 | map = (Map) conf.asObject(bytes); 43 | //then 44 | assertTrue(FSTJSonUnmodifiableMapSerializer.UNMODIFIABLE_MAP_CLASS.isAssignableFrom(map.getClass())); 45 | assertEquals(1, map.size()); 46 | assertTrue(map.containsKey(TEST_KEY)); 47 | assertTrue(map.containsValue(TEST_VALUE)); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /src/test/ser/serializers/FSTStreamEncoderTest.java: -------------------------------------------------------------------------------- 1 | package ser.serializers; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.nustaq.serialization.FSTConfiguration; 6 | 7 | import java.io.IOException; 8 | import java.sql.Timestamp; 9 | import java.util.concurrent.ExecutionException; 10 | import java.util.concurrent.ExecutorService; 11 | import java.util.concurrent.Executors; 12 | import java.util.concurrent.Future; 13 | 14 | /** 15 | * @author ykalemi 16 | * @since 28.04.18 17 | */ 18 | public class FSTStreamEncoderTest { 19 | 20 | @Test 21 | public void newConfigWontUseRegisteredClassFromPrevious() throws IOException { 22 | ExecutorService executor = Executors.newFixedThreadPool(1); 23 | 24 | for (int i = 0; i < 2; i++) { 25 | 26 | final int number = i; 27 | 28 | Thread t = new Thread() { 29 | 30 | @Override 31 | public void run() { 32 | 33 | FSTConfiguration cacheConfig = FSTConfiguration.createDefaultConfiguration(); 34 | cacheConfig.setClassLoader(Thread.currentThread().getContextClassLoader()); 35 | 36 | if (number == 0) { 37 | cacheConfig.registerClass(Timestamp.class); 38 | } 39 | 40 | Timestamp ts = new Timestamp(System.currentTimeMillis()); 41 | byte[] data = cacheConfig.asByteArray(ts); 42 | 43 | Timestamp deserialized = (Timestamp) cacheConfig.asObject(data); 44 | 45 | Assert.assertEquals(ts, deserialized); 46 | } 47 | }; 48 | 49 | Future f = executor.submit(t); 50 | boolean failed = true; 51 | try { 52 | f.get(); 53 | failed = false; 54 | } catch (InterruptedException e) { 55 | e.printStackTrace(); 56 | } catch (ExecutionException e) { 57 | e.printStackTrace(); 58 | } 59 | Assert.assertFalse(failed); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/ser/unit/tests/TestFastSerializationBean1.java: -------------------------------------------------------------------------------- 1 | package ser.unit.tests; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.ObjectInput; 7 | import java.io.ObjectInputStream; 8 | import java.io.ObjectOutput; 9 | import java.io.ObjectOutputStream; 10 | 11 | import org.junit.Assert; 12 | import org.junit.Test; 13 | import org.nustaq.serialization.FSTConfiguration; 14 | import org.nustaq.serialization.FSTObjectOutput; 15 | 16 | import ser.unit.tests.externalizable.BeanTestClass1; 17 | import ser.unit.tests.externalizable.ExternalizableTestClass; 18 | 19 | public class TestFastSerializationBean1 { 20 | 21 | @Test 22 | public void serializationTest() throws IOException, ClassNotFoundException { 23 | // Instantiation of the ExternalizableTestClass object 24 | int integer = 10; 25 | String path = "path"; 26 | ExternalizableTestClass object = new ExternalizableTestClass(integer, path); 27 | BeanTestClass1 bean = new BeanTestClass1(object, object); 28 | 29 | // when serialized and deserialized, the integer value of all ExternalizableTestClass objects should be 30 | // overwritten by readResolve and set to 1. 31 | 32 | // Java 33 | // 1. serialize 34 | byte[] data1; 35 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { 36 | out.writeObject(bean); 37 | data1 = bos.toByteArray(); 38 | } 39 | 40 | // 2. deserialize 41 | BeanTestClass1 bean1; 42 | try (ByteArrayInputStream bis = new ByteArrayInputStream(data1); ObjectInput in = new ObjectInputStream(bis)) { 43 | bean1 = (BeanTestClass1) in.readObject(); 44 | } 45 | 46 | Assert.assertEquals(1, bean1.getObject1().getInteger()); 47 | Assert.assertEquals(1, bean1.getObject2().getInteger()); 48 | 49 | // FST 50 | // 1. serialize 51 | FSTConfiguration config = FSTConfiguration.createDefaultConfiguration(); 52 | FSTObjectOutput out2 = config.getObjectOutput(); 53 | out2.writeObject(bean); 54 | byte[] data2 = out2.getCopyOfWrittenBuffer(); 55 | out2.close(); 56 | 57 | // 2. deserialize 58 | BeanTestClass1 bean2 = (BeanTestClass1) config.getObjectInput(data2).readObject(); 59 | 60 | Assert.assertEquals(1, bean2.getObject1().getInteger()); 61 | Assert.assertEquals(1, bean2.getObject2().getInteger()); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/test/ser/unit/tests/TestFastSerializationBean2.java: -------------------------------------------------------------------------------- 1 | package ser.unit.tests; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.ObjectInput; 7 | import java.io.ObjectInputStream; 8 | import java.io.ObjectOutput; 9 | import java.io.ObjectOutputStream; 10 | import java.util.HashSet; 11 | import java.util.Set; 12 | 13 | import org.junit.Assert; 14 | import org.junit.Test; 15 | import org.nustaq.serialization.FSTConfiguration; 16 | import org.nustaq.serialization.FSTObjectOutput; 17 | 18 | import ser.unit.tests.externalizable.BeanTestClass2; 19 | import ser.unit.tests.externalizable.ExternalizableTestClass; 20 | 21 | public class TestFastSerializationBean2 { 22 | 23 | @Test 24 | public void serializationTest() throws IOException, ClassNotFoundException { 25 | // Instantiation of the ExternalizableTestClass object 26 | int integer = 10; 27 | String path = "path"; 28 | ExternalizableTestClass object = new ExternalizableTestClass(integer, path); 29 | Set set = new HashSet(); 30 | set.add(object); 31 | BeanTestClass2 bean = new BeanTestClass2(object, set); 32 | 33 | // when serialized and deserialized, the integer value of all ExternalizableTestClass objects should be 34 | // overwritten by readResolve and set to 1. 35 | 36 | // Java 37 | // 1. serialize 38 | byte[] data1; 39 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { 40 | out.writeObject(bean); 41 | data1 = bos.toByteArray(); 42 | } 43 | 44 | // 2. deserialize 45 | BeanTestClass2 bean1; 46 | try (ByteArrayInputStream bis = new ByteArrayInputStream(data1); ObjectInput in = new ObjectInputStream(bis)) { 47 | bean1 = (BeanTestClass2) in.readObject(); 48 | } 49 | 50 | Assert.assertEquals(1, bean1.getObject().getInteger()); 51 | Assert.assertEquals(1, ((ExternalizableTestClass) bean1.getSet().toArray()[0]).getInteger()); 52 | 53 | // FST 54 | // 1. serialize 55 | FSTConfiguration config = FSTConfiguration.createDefaultConfiguration(); 56 | FSTObjectOutput out2 = config.getObjectOutput(); 57 | out2.writeObject(bean); 58 | byte[] data2 = out2.getCopyOfWrittenBuffer(); 59 | out2.close(); 60 | 61 | // 2. deserialize 62 | BeanTestClass2 bean2 = (BeanTestClass2) config.getObjectInput(data2).readObject(); 63 | 64 | Assert.assertEquals(1, bean2.getObject().getInteger()); 65 | Assert.assertEquals(1, ((ExternalizableTestClass) bean2.getSet().toArray()[0]).getInteger()); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/ser/unit/tests/TestFastSerializationList.java: -------------------------------------------------------------------------------- 1 | package ser.unit.tests; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.ObjectInput; 7 | import java.io.ObjectInputStream; 8 | import java.io.ObjectOutput; 9 | import java.io.ObjectOutputStream; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import org.junit.Assert; 14 | import org.junit.Test; 15 | import org.nustaq.serialization.FSTConfiguration; 16 | import org.nustaq.serialization.FSTObjectOutput; 17 | 18 | import ser.unit.tests.externalizable.ExternalizableTestClass; 19 | 20 | public class TestFastSerializationList { 21 | 22 | @SuppressWarnings("unchecked") 23 | @Test 24 | public void serializationTest() throws IOException, ClassNotFoundException { 25 | 26 | // Instantiation of the ExternalizableTestClass object 27 | int integer = 10; 28 | String path = "path"; 29 | List list = new ArrayList(); 30 | ExternalizableTestClass object = new ExternalizableTestClass(integer, path); 31 | list.add(object); 32 | list.add(object); 33 | 34 | // when serialized and deserialized, the integer value of all ExternalizableTestClass objects should be 35 | // overwritten by readResolve and set to 1. 36 | 37 | // Java 38 | // 1. serialize 39 | byte[] data1; 40 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { 41 | out.writeObject(list); 42 | data1 = bos.toByteArray(); 43 | } 44 | 45 | // 2. deserialize 46 | List list1 = new ArrayList(); 47 | try (ByteArrayInputStream bis = new ByteArrayInputStream(data1); ObjectInput in = new ObjectInputStream(bis)) { 48 | list1 = (List) in.readObject(); 49 | } 50 | 51 | for (ExternalizableTestClass testClass : list1) { 52 | Assert.assertEquals(1, testClass.getInteger()); 53 | } 54 | 55 | // FST 56 | // 1. serialize 57 | FSTConfiguration config = FSTConfiguration.createDefaultConfiguration(); 58 | FSTObjectOutput out2 = config.getObjectOutput(); 59 | out2.writeObject(list); 60 | byte[] data2 = out2.getCopyOfWrittenBuffer(); 61 | out2.close(); 62 | 63 | // 2. deserialize 64 | List list2 = (ArrayList) config.getObjectInput(data2).readObject(); 65 | 66 | for (ExternalizableTestClass testClass : list2) { 67 | Assert.assertEquals(1, testClass.getInteger()); 68 | } 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/test/ser/unit/tests/externalizable/BeanTestClass1.java: -------------------------------------------------------------------------------- 1 | package ser.unit.tests.externalizable; 2 | 3 | import java.io.Serializable; 4 | 5 | public class BeanTestClass1 implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private ExternalizableTestClass object1; 9 | 10 | private ExternalizableTestClass object2; 11 | 12 | public BeanTestClass1(ExternalizableTestClass object1, ExternalizableTestClass object2) { 13 | this.setObject1(object1); 14 | this.setObject2(object2); 15 | } 16 | 17 | public ExternalizableTestClass getObject1() { 18 | return object1; 19 | } 20 | 21 | public void setObject1(ExternalizableTestClass object1) { 22 | this.object1 = object1; 23 | } 24 | 25 | public ExternalizableTestClass getObject2() { 26 | return object2; 27 | } 28 | 29 | public void setObject2(ExternalizableTestClass object2) { 30 | this.object2 = object2; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/test/ser/unit/tests/externalizable/BeanTestClass2.java: -------------------------------------------------------------------------------- 1 | package ser.unit.tests.externalizable; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | public class BeanTestClass2 implements Serializable { 7 | private static final long serialVersionUID = 1L; 8 | 9 | private ExternalizableTestClass object; 10 | 11 | private Set set; 12 | 13 | public BeanTestClass2(ExternalizableTestClass object, Set set) { 14 | this.setObject1(object); 15 | this.setSet(set); 16 | } 17 | 18 | public ExternalizableTestClass getObject() { 19 | return object; 20 | } 21 | 22 | public void setObject1(ExternalizableTestClass object) { 23 | this.object = object; 24 | } 25 | 26 | public Set getSet() { 27 | return set; 28 | } 29 | 30 | public void setSet(Set set) { 31 | this.set = set; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/ser/unit/tests/externalizable/ExternalizableTestClass.java: -------------------------------------------------------------------------------- 1 | package ser.unit.tests.externalizable; 2 | import java.io.*; 3 | 4 | public class ExternalizableTestClass implements Externalizable { 5 | 6 | // private static final Logger LOGGER = LoggerFactory.getLogger(ExternalizableTestClass.class); 7 | 8 | private String path; 9 | private int integer; 10 | 11 | public ExternalizableTestClass() { 12 | } 13 | 14 | public ExternalizableTestClass(int integer, String path) { 15 | this.path = path; 16 | this.integer = integer; 17 | } 18 | 19 | public void writeExternal(ObjectOutput out) throws IOException { 20 | out.writeUTF(getPath()); 21 | } 22 | 23 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 24 | // LOGGER.info("readExternal()"); 25 | setPath(in.readUTF()); 26 | } 27 | 28 | public Object readResolve() throws ObjectStreamException { 29 | // LOGGER.info("readResolve()"); 30 | return new ExternalizableTestClass(1, this.getPath()); 31 | } 32 | 33 | public String getPath() { 34 | return path; 35 | } 36 | 37 | public void setPath(String path) { 38 | this.path = path; 39 | } 40 | 41 | public int getInteger() { 42 | return integer; 43 | } 44 | 45 | public void setInteger(int integer) { 46 | this.integer = integer; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test_nojunit/java/de/ruedigermoeller/heapofftest/AnotherOffheapThingy.java: -------------------------------------------------------------------------------- 1 | package com.ruedigermoeller.heapofftest; 2 | 3 | import org.nustaq.serialization.FSTClazzInfo; 4 | import org.nustaq.serialization.FSTConfiguration; 5 | import org.nustaq.serialization.util.FSTUtil; 6 | import sun.misc.Unsafe; 7 | 8 | import java.util.Date; 9 | 10 | /** 11 | * Copyright (c) 2012, Ruediger Moeller. All rights reserved. 12 | *

13 | * This library is free software; you can redistribute it and/or 14 | * modify it under the terms of the GNU Lesser General Public 15 | * License as published by the Free Software Foundation; either 16 | * version 2.1 of the License, or (at your option) any later version. 17 | *

18 | * This library is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | * Lesser General Public License for more details. 22 | *

23 | * You should have received a copy of the GNU Lesser General Public 24 | * License along with this library; if not, write to the Free Software 25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 26 | * MA 02110-1301 USA 27 | *

28 | * Date: 24.02.13 29 | * Time: 21:48 30 | * To change this template use File | Settings | File Templates. 31 | */ 32 | public class AnotherOffheapThingy { 33 | static class Sample extends Date { 34 | int x = 11; 35 | int y = 12; 36 | String s = "Hallo"; 37 | } 38 | 39 | public static T copyObj(Unsafe un, T test, int siz) { 40 | byte b[] = new byte[(int) siz]; 41 | un.copyMemory(test,0, b, 0, siz); 42 | return (T)b; 43 | } 44 | 45 | public static void main( String arg[] ) { 46 | System.setProperty("fst.unsafe","true"); 47 | Unsafe un = FSTUtil.getUnsafe(); 48 | FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); 49 | FSTClazzInfo cl = conf.getClassInfo(Sample.class); 50 | Sample test = new Sample(); 51 | System.out.println("fioff "+cl.getFieldInfo("fastTime",Date.class).getMemOffset() ); 52 | System.out.println("fioff "+cl.getFieldInfo("x",Sample.class).getMemOffset() ); 53 | 54 | test.x = 9999; 55 | 56 | Sample other = new Sample(); 57 | 58 | long pos = cl.getFieldInfo("x", Sample.class).getMemOffset(); 59 | long siz = cl.getFieldInfo("s", Sample.class).getMemOffset()+8; 60 | byte b[] = new byte[(int) siz]; 61 | un.copyMemory(test,0, b, 0, siz); 62 | 63 | other = copyObj(un,test, (int) siz); 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test_nojunit/java/de/ruedigermoeller/heapofftest/SimpleHistogram.java: -------------------------------------------------------------------------------- 1 | package com.ruedigermoeller.heapofftest; 2 | 3 | public class SimpleHistogram { 4 | int operCount; 5 | int milliDelayCount[] = new int[100]; 6 | int tenMilliDelayCount[] = new int[300]; 7 | int hundredDelayCount[] = new int[1000]; 8 | 9 | public SimpleHistogram() { 10 | } 11 | 12 | public int[] getMilliDelayCount() { 13 | return milliDelayCount; 14 | } 15 | 16 | public int[] getTenMilliDelayCount() { 17 | return tenMilliDelayCount; 18 | } 19 | 20 | public int[] getHundredDelayCount() { 21 | return hundredDelayCount; 22 | } 23 | 24 | 25 | public void runRequest(Runnable toRun) { 26 | long tim = System.currentTimeMillis(); 27 | toRun.run(); 28 | int dur = (int) (System.currentTimeMillis()-tim); 29 | operCount++; 30 | if ( dur < 50 ) 31 | milliDelayCount[dur]++; 32 | else if ( dur < 3000 ) 33 | tenMilliDelayCount[dur/10]++; 34 | else { 35 | hundredDelayCount[dur/100]++; 36 | } 37 | } 38 | 39 | public void clear() { 40 | milliDelayCount = new int[100]; 41 | tenMilliDelayCount = new int[300]; 42 | hundredDelayCount = new int[1000]; 43 | operCount = 0; 44 | } 45 | 46 | public void addTo( SimpleHistogram other ) { 47 | for (int i = 0; i < milliDelayCount.length; i++) { 48 | other.milliDelayCount[i] += milliDelayCount[i]; 49 | } 50 | for (int i = 0; i < tenMilliDelayCount.length; i++) { 51 | other.tenMilliDelayCount[i] += tenMilliDelayCount[i]; 52 | } 53 | for (int i = 0; i < hundredDelayCount.length; i++) { 54 | other.hundredDelayCount[i] += hundredDelayCount[i]; 55 | } 56 | other.operCount += operCount; 57 | } 58 | 59 | public void dump() { 60 | System.out.println( "ops:"+operCount); 61 | for (int i = 0; i < getMilliDelayCount().length; i++) { 62 | int i1 = getMilliDelayCount()[i]; 63 | if ( i1 > 0 ) { 64 | System.out.println("["+i+"]\t"+i1); 65 | } 66 | } 67 | for (int i = 0; i < getTenMilliDelayCount().length; i++) { 68 | int i1 = getTenMilliDelayCount()[i]; 69 | if ( i1 > 0 ) { 70 | System.out.println("["+i*10+"]\t"+i1); 71 | } 72 | } 73 | for (int i = 0; i < getHundredDelayCount().length; i++) { 74 | int i1 = getHundredDelayCount()[i]; 75 | if ( i1 > 0 ) { 76 | System.out.println("["+i*100+"]\t"+i1); 77 | } 78 | } 79 | } 80 | 81 | } -------------------------------------------------------------------------------- /src/test_nojunit/java/de/ruedigermoeller/heapofftest/structs/TestDate.java: -------------------------------------------------------------------------------- 1 | package com.ruedigermoeller.heapofftest.structs; 2 | 3 | import org.nustaq.offheap.structs.FSTStruct; 4 | 5 | import java.util.Date; 6 | 7 | /** 8 | * Copyright (c) 2012, Ruediger Moeller. All rights reserved. 9 | *

10 | * This library is free software; you can redistribute it and/or 11 | * modify it under the terms of the GNU Lesser General Public 12 | * License as published by the Free Software Foundation; either 13 | * version 2.1 of the License, or (at your option) any later version. 14 | *

15 | * This library is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | * Lesser General Public License for more details. 19 | *

20 | * You should have received a copy of the GNU Lesser General Public 21 | * License along with this library; if not, write to the Free Software 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 23 | * MA 02110-1301 USA 24 | *

25 | * Date: 10.07.13 26 | * Time: 01:27 27 | * To change this template use File | Settings | File Templates. 28 | */ 29 | public class TestDate extends FSTStruct { 30 | 31 | protected long time; 32 | 33 | public TestDate(long time) { 34 | this.time = time; 35 | } 36 | 37 | public Date toDate() { 38 | return new Date(time); 39 | } 40 | 41 | public long getTime() { 42 | return time; 43 | } 44 | 45 | public void setTime(long time) { 46 | this.time = time; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "TestDate{" + 52 | "time=" + time + " = " + new Date(time) + 53 | '}'; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test_nojunit/java/de/ruedigermoeller/heapofftest/structs/TestInstrumentLeg.java: -------------------------------------------------------------------------------- 1 | package com.ruedigermoeller.heapofftest.structs; 2 | 3 | import org.nustaq.offheap.structs.FSTStruct; 4 | 5 | /** 6 | * Copyright (c) 2012, Ruediger Moeller. All rights reserved. 7 | *

8 | * This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU Lesser General Public 10 | * License as published by the Free Software Foundation; either 11 | * version 2.1 of the License, or (at your option) any later version. 12 | *

13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | * Lesser General Public License for more details. 17 | *

18 | * You should have received a copy of the GNU Lesser General Public 19 | * License along with this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 21 | * MA 02110-1301 USA 22 | *

23 | * Date: 10.07.13 24 | * Time: 01:38 25 | * To change this template use File | Settings | File Templates. 26 | */ 27 | public class TestInstrumentLeg extends FSTStruct { 28 | protected TestInstrument instrument = new TestInstrument(); 29 | protected int legQty = 1; 30 | 31 | public TestInstrumentLeg() { 32 | this.instrument = instrument; 33 | this.legQty = legQty; 34 | } 35 | 36 | public TestInstrument getInstrument() { 37 | return instrument; 38 | } 39 | 40 | public int getLegQty() { 41 | return legQty; 42 | } 43 | 44 | public void setLegQty(int legQty) { 45 | this.legQty = legQty; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test_nojunit/java/de/ruedigermoeller/heapofftest/structs/TestMarket.java: -------------------------------------------------------------------------------- 1 | package com.ruedigermoeller.heapofftest.structs; 2 | 3 | import org.nustaq.offheap.structs.FSTStruct; 4 | import org.nustaq.offheap.structs.structtypes.StructString; 5 | 6 | /** 7 | * Copyright (c) 2012, Ruediger Moeller. All rights reserved. 8 | *

9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | *

14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | *

19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 22 | * MA 02110-1301 USA 23 | *

24 | * Date: 10.07.13 25 | * Time: 01:26 26 | * To change this template use File | Settings | File Templates. 27 | */ 28 | public class TestMarket extends FSTStruct { 29 | 30 | protected StructString mnemonic = new StructString(5); 31 | protected TestDate opens = new TestDate(0); 32 | protected TestDate closes = new TestDate(0); 33 | protected TestTimeZone zone = new TestTimeZone(); 34 | 35 | public StructString getMnemonic() { 36 | return mnemonic; 37 | } 38 | 39 | public TestDate getOpens() { 40 | return opens; 41 | } 42 | 43 | public TestDate getCloses() { 44 | return closes; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "TestMarket{" + 50 | "mnemonic=" + mnemonic + 51 | ", opens=" + opens + 52 | ", closes=" + closes + 53 | ", zone=" + zone + 54 | '}'; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test_nojunit/java/de/ruedigermoeller/heapofftest/structs/TestTimeZone.java: -------------------------------------------------------------------------------- 1 | package com.ruedigermoeller.heapofftest.structs; 2 | 3 | import org.nustaq.offheap.structs.FSTStruct; 4 | import org.nustaq.offheap.structs.structtypes.StructString; 5 | 6 | import java.util.TimeZone; 7 | 8 | /** 9 | * Copyright (c) 2012, Ruediger Moeller. All rights reserved. 10 | *

11 | * This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | *

16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | *

21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this library; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 24 | * MA 02110-1301 USA 25 | *

26 | * Date: 10.07.13 27 | * Time: 01:29 28 | * To change this template use File | Settings | File Templates. 29 | */ 30 | public class TestTimeZone extends FSTStruct { 31 | 32 | protected StructString zoneId = new StructString("GMT", 10); 33 | 34 | public StructString getZoneId() { 35 | return zoneId; 36 | } 37 | 38 | public void setZoneId(StructString zoneId) { 39 | this.zoneId = zoneId; 40 | } 41 | 42 | public TimeZone getTimeZone() { 43 | return TimeZone.getTimeZone(zoneId.toString()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /test.json: -------------------------------------------------------------------------------- 1 | { 2 | "typ": "call", 3 | "obj": { 4 | "futureKey": 1, 5 | "queue": 0, 6 | "receiverKey": 1, 7 | "args": { 8 | "styp": "array", 9 | "seq": [ 10 | 2, 11 | { 12 | "styp": "array", 13 | "seq": [ 14 | 3, 15 | "user", 16 | "pwd" 17 | ] 18 | } 19 | ] 20 | } 21 | } 22 | } --------------------------------------------------------------------------------