com.nordea.oss.copybook.CopyBookSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of copybook4java Show documentation
Show all versions of copybook4java Show documentation
CopyBook serializer and deserializer for Java where CopyBook lines are used to annotate a normal Java class
The newest version!
/*
* Copyright (c) 2015. Troels Liebe Bentsen
* Copyright (c) 2016. Nordea Bank AB
* Licensed under the MIT license (LICENSE.txt)
*/
package com.nordea.oss.copybook;
import com.nordea.oss.copybook.serializers.CopyBookParser;
import com.nordea.oss.ByteUtils;
import com.nordea.oss.copybook.exceptions.CopyBookException;
import com.nordea.oss.copybook.serializers.CopyBookMapper;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class CopyBookSerializer {
private CopyBookMapper serializer;
public CopyBookSerializer(Class> type) {
this(type, false);
}
public CopyBookSerializer(Class> type, boolean debug) {
this(type, null, debug);
}
public CopyBookSerializer(Class> type, Class mapper, boolean debug) {
CopyBookParser parser = new CopyBookParser(type, debug);
try {
if(mapper != null) {
serializer = mapper.getDeclaredConstructor().newInstance();
} else {
serializer = parser.getSerializerClass().getDeclaredConstructor().newInstance();
}
serializer.initialize(parser.getConfig());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new CopyBookException("Failed to load Serialization class("+ parser.getSerializerClass().getSimpleName() +")", e);
}
}
public byte[] serialize(T obj) {
return serializer.serialize(obj);
}
public T deserialize(byte[] bytes, Class type) {
return serializer.deserialize(bytes, type);
}
public T deserialize(InputStream inputStream, Class type) throws IOException {
return deserialize(ByteUtils.toByteArray(inputStream), type);
}
public List getErrors() {
return null; // TODO: Implement depending on errorValue
}
public int getMinRecordSize() {
return serializer.getMinRecordSize();
}
public int getMaxRecordSize() {
return serializer.getMaxRecordSize();
}
}