Java Code Examples for com.esotericsoftware.kryo.Kryo.writeObject()

The following are Jave code examples for showing how to use writeObject() of the com.esotericsoftware.kryo.Kryo class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: happylifeplat-transaction   File: KryoSerializer.java   Source Code and License Vote up 6 votes
/**
 * 序列化
 *
 * @param obj 需要序更列化的对象
 * @return 序列化后的byte 数组
 * @throws TransactionException
 */
@Override
public byte[] serialize(Object obj) throws TransactionException {
    byte[] bytes;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (Exception ex) {
        throw new TransactionException("kryo serialize error" + ex.getMessage());
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {

        }
    }
    return bytes;
}
 
Example 2
Project: happylifeplat-tcc   File: KryoSerializer.java   Source Code and License Vote up 6 votes
/**
 * 序列化
 *
 * @param obj 需要序更列化的对象
 * @return 序列化后的byte 数组
 * @throws TccException
 */
@Override
public byte[] serialize(Object obj) throws TccException {
    byte[] bytes;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (Exception ex) {
        throw new TccException("kryo serialize error" + ex.getMessage());
    }
    return bytes;
}
 
Example 3
Project: gdx-cclibs   File: ObjectIntMapSerializer.java   Source Code and License Vote up 6 votes
public void write (Kryo kryo, Output output, ObjectIntMap map) {
    int length = map.size;
    output.writeVarInt(length, true);
    output.writeBoolean(false); // whether type is written (in case future version of ObjectIntMap supports type awareness)

    Serializer keySerializer = null;
    if (keyGenericType != null) {
        if (keySerializer == null) keySerializer = kryo.getSerializer(keyGenericType);
        keyGenericType = null;
    }

    for (Iterator iter = map.iterator(); iter.hasNext();) {
        ObjectIntMap.Entry entry = (ObjectIntMap.Entry)iter.next();
        if (keySerializer != null) {
            kryo.writeObject(output, entry.key, keySerializer);
        } else
            kryo.writeClassAndObject(output, entry.key);
        output.writeInt(entry.value);
    }
}
 
Example 4
Project: lionrpc   File: MessageEncoder.java   Source Code and License Vote up 6 votes
protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
    if(clazz.isInstance(o)){
        Kryo kryo = null;
        try{
        	kryo = pool.borrow();
        	ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            Output output = new Output(baos);  
            kryo.writeObject(output, o);  
            output.flush();  
            output.close();  
           
            byte[] data = baos.toByteArray(); 
            byteBuf.writeInt(data.length);
            byteBuf.writeBytes(data);
            baos.close();
        }catch(Exception e){
        	LOG.warn("MessageEncoder happen exception.", e);
        }finally{
        	if(kryo != null){
        		 pool.release(kryo);
        	}
        }
        
    }

}
 
Example 5
Project: react-native-region-monitor   File: PersistableData.java   Source Code and License Vote up 6 votes
public void save(@NonNull Context context)
{
	Kryo serialiser = createSerialiser();

	Log.d(TAG, "Save geofencing state... ");
	String path = context.getFilesDir().getAbsolutePath() + "/" + CACHE_FILE;
	Log.d(TAG, "Saving geofencing state to" + path);
	try
	{
		Output output = new Output(new FileOutputStream(path));
		serialiser.writeObject(output, this);
		output.close();
	}
	catch (FileNotFoundException e)
	{
		Log.e(TAG, "Could not serialise geofencing data", e);
	}
}
 
Example 6
Project: myth   File: KryoSerializer.java   Source Code and License Vote up 6 votes
/**
 * 序列化
 *
 * @param obj 需要序更列化的对象
 * @return 序列化后的byte 数组
 * @throws MythException 异常
 */
@Override
public byte[] serialize(Object obj) throws MythException {
    byte[] bytes;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (Exception ex) {
        throw new MythException("kryo serialize error" + ex.getMessage());
    }
    return bytes;
}
 
Example 7
Project: dremio-oss   File: TableMetadataSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(final Kryo kryo, final Output output, final TableMetadata table) {
  try{
    Preconditions.checkArgument(!table.isPruned(), "Cannot serialize a pruned table.");
  }catch(NamespaceException ex){
    throw Throwables.propagate(ex);
  }

  kryo.writeObject(output, table.getName().getPathComponents());
}
 
Example 8
Project: MobiRNN-EMDL17   File: DataUtilTest.java   Source Code and License Vote up 5 votes
@Test
    public void serialize() throws Exception {
        String testFilePath = getClass().getClassLoader().getResource("lstm_har-data").getFile();
        String inputFilePath =
                testFilePath + File.separator + "test_data" + File.separator + "sensor";
        final float[][][] inputs;

        final Kryo kryo = new Kryo();
        kryo.register(float[][][].class);
        final File dataBinFile = new File("data-small.bin");
        if (dataBinFile.exists()) {
            Input input = new Input(new FileInputStream(dataBinFile));
            inputs = kryo.readObject(input, float[][][].class);
            input.close();
        } else {
            inputs = DataUtil.parseInputData(inputFilePath);
            Output output = new Output(new FileOutputStream(dataBinFile));

            kryo.writeObject(output, inputs);
            output.close();
        }

//        Gson gson = new Gson();
//        final File dataBinFile = new File("data.json");
//        if (dataBinFile.exists()) {
//            inputs = gson.fromJson(FileUtils.readFileToString(dataBinFile), float[][][].class);
//        } else {
//            inputs = DataUtil.parseInputData(inputFilePath);
//            String result = gson.toJson(inputs);
//            FileUtils.writeStringToFile(new File("data.json"), result);
//        }


    }
 
Example 9
Project: dremio-oss   File: SqlOperatorSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(final Kryo kryo, final Output output, final T operator) {
  final int identity = System.identityHashCode(operator);
  final Integer ordinal = OperatorPopulator.FORWARD.get(identity);
  final boolean isKnown = ordinal != null;
  kryo.writeObject(output, isKnown);
  if (isKnown) {
    kryo.writeObject(output, ordinal);
    return;
  }

  super.write(kryo, output, operator);
}
 
Example 10
Project: athena   File: ImmutableMapSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(Kryo kryo, Output output, ImmutableMap<?, ?> object) {
    // wrapping with unmodifiableMap proxy
    // to avoid Kryo from writing only the reference marker of this instance,
    // which will be embedded right before this method call.
    kryo.writeObject(output, Collections.unmodifiableMap(object), mapSerializer);
}
 
Example 11
Project: gdx-cclibs   File: ObjectMapSerializer.java   Source Code and License Vote up 5 votes
public void write (Kryo kryo, Output output, T map) {
    int length = map.size;
    output.writeVarInt(length, true);
    output.writeBoolean(false); // whether type is written (in case future version of ObjectMap supports type awareness)

    Serializer keySerializer = null;
    if (keyGenericType != null) {
        if (keySerializer == null) keySerializer = kryo.getSerializer(keyGenericType);
        keyGenericType = null;
    }
    Serializer valueSerializer = null;
    if (valueGenericType != null) {
        if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueGenericType);
        valueGenericType = null;
    }

    for (Iterator iter = map.iterator(); iter.hasNext();) {
        ObjectMap.Entry entry = (ObjectMap.Entry)iter.next();
        if (keySerializer != null) {
            kryo.writeObject(output, entry.key, keySerializer);
        } else
            kryo.writeClassAndObject(output, entry.key);
        if (valueSerializer != null) {
            kryo.writeObjectOrNull(output, entry.value, valueSerializer);
        } else
            kryo.writeClassAndObject(output, entry.value);
    }
}
 
Example 12
Project: athena   File: EncodedResourcesSerializer.java   Source Code and License Vote up 5 votes
@Override
public void write(Kryo kryo, Output output, EncodedDiscreteResources object) {
    List<ClosedOpenRange> ranges = object.rangeSet().asRanges().stream()
            .map(ClosedOpenRange::of)
            .collect(Collectors.toList());
    kryo.writeObject(output, ranges);
    kryo.writeClassAndObject(output, object.codec());
}
 
Example 13
Project: dremio-oss   File: RelTraitSerializers.java   Source Code and License Vote up 5 votes
@Override
public void write(final Kryo kryo, final Output output, final T object) {
  final boolean isKnown = collationSet.contains(object);
  kryo.writeObject(output, isKnown);
  if (isKnown) {
    kryo.writeObject(output, object.equals(RelCollations.EMPTY) ? 0 : 1);
    return;
  }

  super.write(kryo, output, object);
}
 
Example 14
Project: springboot-shiro-cas-mybatis   File: URLSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final URL url) {
    kryo.writeObject(output, url.toExternalForm());
}
 
Example 15
Project: cas-5.1.0   File: ZonedDateTimeTranscoder.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final ZonedDateTime dateTime) {
    kryo.writeObject(output, dateTime.toInstant().toEpochMilli());
    kryo.writeObject(output, dateTime.getZone().getId());
}
 
Example 16
Project: springboot-shiro-cas-mybatis   File: GoogleAccountsServiceSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final GoogleAccountsService service) {
    super.write(kryo, output, service);
    kryo.writeObject(output, fieldHelper.getFieldValue(service, "requestId"));
    kryo.writeObject(output, fieldHelper.getFieldValue(service, "relayState"));
}
 
Example 17
Project: springboot-shiro-cas-mybatis   File: AbstractWebApplicationServiceSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final T service) {
    kryo.writeObject(output, service.getId());
    kryo.writeObject(output, fieldHelper.getFieldValue(service, "originalUrl"));
    kryo.writeObject(output, service.getArtifactId());
}
 
Example 18
Project: springboot-shiro-cas-mybatis   File: SamlServiceSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final SamlService service) {
    super.write(kryo, output, service);
    kryo.writeObject(output, service.getRequestID());
}
 
Example 19
Project: cas-5.1.0   File: SamlServiceSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(final Kryo kryo, final Output output, final SamlService service) {
    super.write(kryo, output, service);
    kryo.writeObject(output, service.getRequestID());
}
 
Example 20
Project: athena   File: AnnotationsSerializer.java   Source Code and License Vote up 4 votes
@Override
public void write(Kryo kryo, Output output, DefaultAnnotations object) {
    kryo.writeObject(output, object.asMap());
}