本文整理汇总了Java中org.nustaq.serialization.FSTObjectOutput类的典型用法代码示例。如果您正苦于以下问题:Java FSTObjectOutput类的具体用法?Java FSTObjectOutput怎么用?Java FSTObjectOutput使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FSTObjectOutput类属于org.nustaq.serialization包,在下文中一共展示了FSTObjectOutput类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Override
public void encode(Object obj, DataWriter out) throws IOException {
try {
FSTObjectOutput objOut = fst.getObjectOutput(out.asStream());
objOut.writeObject(obj);
objOut.flush();
} catch (RuntimeException e) {
// Workaround for FST throwing RuntimeException instead of NotSerializableException.
if (e.getMessage() != null && e.getMessage().indexOf("does not implement Serializable") > 0) {
NotSerializableException converted = new NotSerializableException(e.getMessage());
converted.setStackTrace(e.getStackTrace());
throw converted;
} else {
throw e;
}
}
}
示例2: FSWrite
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
private boolean FSWrite(String filepath, String tag) {
try {
if (!tag.equals("")) {
tag = "_" + tag;
}
Logger.getRootLogger().info("Writing ID results to file:" + FilenameUtils.getFullPath(filepath) + FilenameUtils.getBaseName(filepath) + tag + "_LCMSID.serFS...");
FileOutputStream fout = new FileOutputStream(FilenameUtils.getFullPath(filepath) + FilenameUtils.getBaseName(filepath) + tag + "_LCMSID.serFS", false);
FSTObjectOutput out = new FSTObjectOutput(fout);
ReduceMemoryUsage();
out.writeObject(this, LCMSID.class);
out.close();
fout.close();
} catch (Exception ex) {
Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
return false;
}
return true;
}
示例3: serializeCompressLZ4
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
public synchronized int serializeCompressLZ4(T obj, File file) {
try {
byte[] barray = conf.get().asByteArray(obj);
int compressedLength = barray.length;
int decompressedLength = -1;
// Notes: uses an LZ4 block size of 64KB, fast compressor
LZ4BlockOutputStream LZOS = new LZ4BlockOutputStream(new FileOutputStream(file));
FSTObjectOutput oos = conf.get().getObjectOutput(LZOS);
oos.writeObject(obj);
oos.flush();
LZOS.close();
return decompressedLength;
}
catch(Exception e) {
e.printStackTrace();
}
return -1;
}
示例4: saveModel
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
public static <T> void saveModel(
final DataOutputStream dos,
final CRFFeatureEncoder<String, T, String> fe,
final Vector weights,
final ParserLMFeatures plf,
final GazetteerFeatures gf,
final String dataVersion
) throws IOException {
dos.writeUTF(dataVersion);
fe.stateSpace.save(dos);
fe.nodeFeatures.save(dos);
fe.edgeFeatures.save(dos);
IOUtils.saveDoubles(dos, weights.toDoubles());
logger.debug("Saving ParserLMFeatures");
try(final FSTObjectOutput out = new FSTObjectOutput(dos)) {
out.writeObject(plf);
if (plf != null)
plf.logState();
logger.debug("Saving gazetteer features");
out.writeObject(gf);
}
}
示例5: main
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
SomeMediumClass mediumClass = new SomeMediumClass();
byte barray[] = conf.asByteArray(mediumClass);
System.out.println(barray.length);
SomeMediumClass object = (SomeMediumClass)conf.asObject(barray);
System.out.println(object);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FSTObjectOutput output = new FSTObjectOutput(outputStream);
output.writeObject(mediumClass);
output.close();
FSTObjectInput input = new FSTObjectInput(new ByteArrayInputStream(outputStream.toByteArray()));
object = (SomeMediumClass)input.readObject(SomeMediumClass.class);
System.out.println(object);
}
示例6: writeObject
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
EnumSet enset = (EnumSet) toWrite;
int count = 0;
out.writeInt(enset.size());
if ( enset.isEmpty() ) { //WTF only way to determine enumtype ..
EnumSet compl = EnumSet.complementOf(enset);
out.writeClassTag(FSTUtil.getRealEnumClass(compl.iterator().next().getClass()));
} else {
for (Object element : enset) {
if ( count == 0 ) {
out.writeClassTag(FSTUtil.getRealEnumClass(element.getClass()));
}
out.writeObjectInternal(element, null, Enum.class);
count++;
}
}
}
示例7: writeObject
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
ArrayList col = (ArrayList)toWrite;
int size = col.size();
out.writeInt(size);
Class lastClz = null;
FSTClazzInfo lastInfo = null;
for (int i = 0; i < size; i++) {
Object o = col.get(i);
if ( o != null ) {
lastInfo = out.writeObjectInternal(o, o.getClass() == lastClz ? lastInfo : null, null);
lastClz = o.getClass();
} else
out.writeObjectInternal(o, null, null);
}
}
示例8: writeObject
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
Map col = (Map)toWrite;
out.writeInt(col.size());
FSTClazzInfo lastKClzI = null;
FSTClazzInfo lastVClzI = null;
Class lastKClz = null;
Class lastVClz = null;
for (Iterator iterator = col.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry next = (Map.Entry) iterator.next();
Object key = next.getKey();
Object value = next.getValue();
if ( key != null && value != null ) {
lastKClzI = out.writeObjectInternal(key, key.getClass() == lastKClz ? lastKClzI : null, null);
lastVClzI = out.writeObjectInternal(value, value.getClass() == lastVClz ? lastVClzI : null, null);
lastKClz = key.getClass();
lastVClz = value.getClass();
} else
{
out.writeObjectInternal(key, null, null);
out.writeObjectInternal(value, null, null);
}
}
}
示例9: writeObject
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
EnumSet enset = (EnumSet) toWrite;
int count = 0;
out.writeInt(enset.size());
if ( enset.isEmpty() ) { //WTF only way to determine enumtype ..
EnumSet compl = EnumSet.complementOf(enset);
out.writeStringUTF(FSTUtil.getRealEnumClass(compl.iterator().next().getClass()).getName());
} else {
for (Object element : enset) {
if ( count == 0 ) {
out.writeStringUTF(FSTUtil.getRealEnumClass(element.getClass()).getName());
}
out.writeStringUTF(element.toString());
count++;
}
}
}
示例10: main
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
FSTConfiguration config = FSTConfiguration.createDefaultConfiguration();
FSTObjectOutput out = config.getObjectOutput();
out.writeObject("foobar");
byte[] savedBuffer = out.getCopyOfWrittenBuffer();
byte[] reallySavedBuffer = Arrays.copyOf(savedBuffer, savedBuffer.length);
System.out.println(Arrays.equals(savedBuffer, reallySavedBuffer));
out.resetForReUse();
out.writeObject("blah");
byte[] secondBuffer = out.getCopyOfWrittenBuffer();
System.out.println(Arrays.equals(savedBuffer, reallySavedBuffer));
config.getObjectInput(savedBuffer);
config.getObjectInputCopyFrom(secondBuffer, 0, secondBuffer.length);
System.out.println(Arrays.equals(savedBuffer, reallySavedBuffer));
}
示例11: encodeDecode
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
private static void encodeDecode(Object object) throws Exception {
try {
FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
conf.setShareReferences(false);
try (FSTObjectInput input = new FSTObjectInputNoShared(conf);
FSTObjectOutput output = new FSTObjectOutputNoShared(conf)) {
output.writeObject(object);
byte[] bytes = output.getCopyOfWrittenBuffer();
input.resetForReuseUseArray(bytes);
Object read = input.readObject();
System.out.println(read);
System.out.println(object.equals(read));
}
} catch (Throwable e) {
System.err.println(e.getClass() + ": " + e.getMessage());
}
}
示例12: testStreamReuse
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Test
public void testStreamReuse() throws Exception {
FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration();
String expected = "Hello, World!";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FSTObjectOutput fstObjectOutput = configuration.getObjectOutput(baos);
try {
fstObjectOutput.writeObject(expected);
} finally {
fstObjectOutput.flush();
}
byte[] serializedData = baos.toByteArray();
FSTObjectInput input = configuration.getObjectInput(new ByteArrayInputStream(serializedData));
Object read = input.readObject();
Assert.assertEquals(expected, read);
FSTObjectInput secondInput = configuration.getObjectInput(new ByteArrayInputStream(new byte[0]));
expectedException.expect(IOException.class);
expectedException.expectMessage("Failed to read");
secondInput.readObject();
}
示例13: test
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Test
@Ignore
public void test() throws Throwable {
FSTConfiguration fc = FSTConfiguration.createDefaultConfiguration();
byte[] b;
int len;
try (FSTObjectOutput foo = new FSTObjectOutput(fc)) {
foo.writeObject(new Outer());
b = foo.getBuffer();
len = foo.getWritten();
}
ByteArrayInputStream bais = new ByteArrayInputStream(b, 0, len);
FSTObjectInput foi = fc.getObjectInput(bais);
foi.readObject();
}
示例14: testReadResolve
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Test
public void testReadResolve() throws Exception {
FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
Holder holder = new Holder();
holder.o = new ToRead("foo");
holder.o2 = holder.o;
byte[] b = null;
FSTObjectOutput out = new FSTObjectOutput(conf);
out.writeObject(holder);
out.flush();
b = out.getBuffer();
FSTObjectInput in = new FSTObjectInput(conf);
in.resetForReuseUseArray(b,b.length);
Object res = in.readObject();
checkEquals(Holder.class, res.getClass());
checkEquals(String.class, ((Holder) res).o.getClass());
checkEquals("foo", ((Holder) res).o);
checkEquals(String.class, ((Holder) res).o2.getClass());
checkEquals("foo", ((Holder) res).o2);
}
示例15: testReadByte
import org.nustaq.serialization.FSTObjectOutput; //导入依赖的package包/类
@Test
public void testReadByte() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ByteArrayOutputStream fstbaos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(baos);
writebytes(baos,oout);
FSTObjectOutput fsto = new FSTObjectOutput(fstbaos);
writebytes(fstbaos,fsto);
ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
FSTObjectInput fstin = new FSTObjectInput(new ByteArrayInputStream(fstbaos.toByteArray()));
int rd;
while( (rd=oin.read()) != -1 ) {
Assert.assertTrue(rd == fstin.read() );
}
Assert.assertTrue( fstin.read() == -1 );
}