package hikefa.core.database.oracle;

import hikefa.core.exception.BizSysException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;

import oracle.sql.BLOB;

import org.hibernate.Hibernate;
import org.hibernate.lob.SerializableBlob;


public class BlobUtil35
{
	public static void blobToFile(Blob blob, String filename) throws BizSysException
	{
		try
		{
			File file = new File(filename);
			InputStream ins = blob.getBinaryStream();
			FileOutputStream fout = new FileOutputStream(file);
			byte[] b = new byte[1024];

			int len = 0;

			while ((len = ins.read(b)) != -1)
			{

				fout.write(b, 0, len);

			}
			fout.close();
			ins.close();

		}
		catch (Exception e)
		{
			throw new BizSysException("Blobļ[" + filename + "]ʧ");
		}
		// 
	}
	public static byte[] blobToByte(Blob blob) throws BizSysException
	{
		try
		{
			InputStream ins = blob.getBinaryStream();
			/*
			byte[] b=null;
			int size = ins.available();
			if (size<=0)
				return b;
			else if (size>1024*1024*10)
				throw new BizException("blobֶתbyteֳ10M,ܾ");
			b = new byte[size];

			ins.read(b, 0, size);
			ins.close();
			
			return b;
			*/
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			byte[] b = new byte[1024];
			while ((len = ins.read(b, 0, b.length)) != -1)
			{
				baos.write(b, 0, len);
			}
			byte[] buffer = baos.toByteArray();
			baos.close();

			return buffer;
			

		}
		catch (Exception e)
		{
			throw new BizSysException("Blobbyteʧ");
		}

	}

	public static Blob fileToBlob(String filename) throws BizSysException
	{
		Blob blob = null;
		try
		{
			FileInputStream fis = new FileInputStream(filename);
			blob = Hibernate.createBlob(fis);
		}
		catch (Exception e)
		{
			throw new BizSysException("ļ[" + filename + "]ȡBlobʧ");
		}
		return blob;

	}
	//ֱʹHibernate.createBlob(is);
	public static Blob streamToBlob(InputStream is) throws BizSysException
	{
		Blob blob = null;
		try
		{
			blob = Hibernate.createBlob(is);
		}
		catch (Exception e)
		{
			throw new BizSysException("ȡBlobʧ");
		}
		return blob;

	}
	//hibernate3.5org.hibernate.lob.SerializableBlobᱻȡȡ֮org.hibernate.jdbc.ResultSetWrapper
	//Hibernate.createBlob(new byte[1]);Ҳ
	public static void updateByteToOracleBlob(byte[] data,SerializableBlob blob) throws BizSysException
	{
		try
		{
			Blob newBlob = blob.getWrappedBlob();
			OutputStream out = null;
			if (newBlob instanceof BLOB)
			{
				out = ((BLOB)newBlob).getBinaryOutputStream();
				out.write(data);
				out.flush();
				out.close();
			}
		}
		catch (Exception e)
		{
			throw new BizSysException("ȡBlobʧ");
		}

	}
	
	public static void updateStreamToOracleBlob(InputStream fis,SerializableBlob blob) throws BizSysException
	{
		if (blob==null)
			throw new BizSysException("Blobǿ");
		try
		{
			Blob newBlob = blob.getWrappedBlob();
			byte[] data = new byte[(int)fis.available()];
			fis.read(data);
			OutputStream out = null;
			if (newBlob instanceof BLOB)
			{
				out = ((BLOB)newBlob).getBinaryOutputStream();
				out.write(data);
				out.flush();
				out.close();
			}
			fis.close();
		}
		catch (Exception e)
		{
			throw new BizSysException("ȡBlobʧ");
		}

	}
	
	public static void updateFileToOracleBlob(String filename,SerializableBlob blob) throws BizSysException
	{
		try
		{
			FileInputStream fis = new FileInputStream(filename);
			updateStreamToOracleBlob(fis,blob);
		}
		catch (Exception e)
		{
			throw new BizSysException("ļ[" + filename + "]ȡBlobʧ");
		}

	}
	//ַ
	public static Blob createEmptyBlob() 
	{
		Blob blob = null;
		blob = Hibernate.createBlob(new byte[1]);
		return blob;

	}
	//ַ
	public static BLOB createEmptyOracleBlob() throws Exception
	{
		return oracle.sql.BLOB.empty_lob();

	}
	
}
