All Downloads are FREE. Search and download functionalities are using the official Maven repository.

panda.cast.castor.ByteArrayCastor Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
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);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy