panda.cast.castor.ByteArrayCastor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.cast.castor;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.SQLException;
import panda.cast.CastContext;
import panda.codec.binary.Base64;
import panda.io.Streams;
import panda.lang.Exceptions;
import panda.vfs.FileItem;
/**
*
*/
public class ByteArrayCastor extends AnySingleCastor {
public ByteArrayCastor() {
super(byte[].class);
}
@Override
protected byte[] castValue(Object value, CastContext context) {
try {
if (value instanceof Blob) {
return ((Blob)value).getBytes(1, (int)((Blob)value).length());
}
if (value instanceof FileItem) {
FileItem fi = (FileItem)value;
return fi.isExists() ? fi.data() : null;
}
if (value instanceof InputStream) {
return Streams.toByteArray((InputStream)value);
}
if (value instanceof char[]) {
return Base64.decodeBase64(new String((char[])value));
}
if (value instanceof CharSequence) {
return Base64.decodeBase64(((CharSequence)value).toString());
}
if (value instanceof Reader) {
return Base64.decodeBase64((Reader)value);
}
return castError(value, context);
}
catch (SQLException e) {
throw Exceptions.wrapThrow(e);
}
catch (IOException e) {
throw Exceptions.wrapThrow(e);
}
}
}