org.simplejavamail.internal.clisupport.serialization.SerializationUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cli-module Show documentation
Show all versions of cli-module Show documentation
Simple API, Complex Emails. Now with CLI support
/*
* Copyright © 2009 Benny Bottema ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simplejavamail.internal.clisupport.serialization;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy;
import de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer;
import org.jetbrains.annotations.NotNull;
import org.objenesis.strategy.StdInstantiatorStrategy;
/**
* Used to serialize attachments of nested Outlook messages. This is needed because outlook-message-parser returns a Java structure from a .msg source, but this conversion is 1-way. An Email object
* represents attachments as DataSources however, so for this we need to serialize back from the java structure to a binary format. This Util does this.
*
* Then for users to obtain the Javastructure again, they must use this util to deserialize the relevant attachment.
*
* @see GitHub issue #314
*/
public class SerializationUtil {
private static final Kryo KRYO = initKryo();
@NotNull
private static Kryo initKryo() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
UnmodifiableCollectionsSerializer.registerSerializers(kryo);
return kryo;
}
@NotNull
public static byte[] serialize(@NotNull final Object serializable) {
final Output output = new Output(1024, -1);
KRYO.writeClassAndObject(output, serializable);
return output.toBytes();
}
@SuppressWarnings("unchecked")
@NotNull
public static T deserialize(final byte@NotNull[] serialized) {
return (T) KRYO.readClassAndObject(new Input(serialized));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy