blob: a6c9377a9b7bcbebe4cc9907b765b19d7855a5d7 [file] [log] [blame] [raw]
package net.querz.io;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public interface Serializer<T> {
void toStream(T object, OutputStream out) throws IOException;
default void toFile(T object, File file) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
toStream(object, bos);
}
}
default byte[] toBytes(T object) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
toStream(object, bos);
bos.close();
return bos.toByteArray();
}
}