001    package railo.runtime.type.sql;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.Serializable;
005    import java.sql.Blob;
006    import java.sql.SQLException;
007    
008    import railo.runtime.exp.PageException;
009    import railo.runtime.op.Caster;
010    
011      
012     
013     /**
014     * Implementation of the Interface java.sql.Blob
015     */
016    public final class BlobImpl implements java.sql.Blob, Serializable {
017         byte[] binaryData = null;
018     
019         /**
020          * constructor of the class
021         * @param data
022         */
023        private BlobImpl(byte[] data)   {
024             binaryData = data;
025         }
026     
027         @Override
028        public long length() throws SQLException {
029             return binaryData.length;
030         }
031     
032         @Override
033        public byte[] getBytes(long pos, int length) throws SQLException   {
034             byte[] newData = new byte[length]; 
035             System.arraycopy(binaryData, (int) (pos - 1), newData, 0, length);
036             return newData;
037         }
038     
039         @Override
040        public java.io.InputStream getBinaryStream() throws SQLException    {
041            return new ByteArrayInputStream(binaryData);
042        }
043        
044        @Override
045        public java.io.InputStream getBinaryStream(long pos, long length)   {
046            // TODO impl this
047            return new ByteArrayInputStream(binaryData);
048        }
049    
050         @Override
051        public long position(byte pattern[], long start) throws SQLException        {
052             return (new String(binaryData)).indexOf(new String(pattern), (int) start);
053         }
054     
055         @Override
056        public long position(java.sql.Blob pattern, long start) throws SQLException {
057             return position(pattern.getBytes(0, (int) pattern.length()), start);
058         }
059     
060         @Override
061        public int setBytes(long pos, byte[] bytes) throws SQLException     {
062             // TODO impl.
063             throw new SQLException("JDBC 3.0 Method setBytes not implemented");
064         }
065    
066         @Override
067        public int setBytes(long pos, byte[] bytes, int offset, int len)    throws SQLException     {
068             // TODO impl.
069             throw new SQLException("JDBC 3.0 Method setBytes not implemented");
070         }
071     
072         @Override
073        public java.io.OutputStream setBinaryStream(long pos) throws SQLException   {
074             // TODO impl.
075             throw new SQLException("JDBC 3.0 Method setBinaryStream not implemented");
076         }
077     
078         @Override
079        public void truncate(long len) throws SQLException  {
080             // TODO impl.
081             throw new SQLException("JDBC 3.0 Method truncate not implemented");
082         }
083    
084        public static Blob toBlob(Object value) throws PageException {
085            if(value instanceof Blob) return (Blob)value;
086            return new BlobImpl(Caster.toBinary(value));
087        }
088        
089        /*public static Blob toBlob(byte[] value) {
090            
091            Class blobClass = ClassUtil.loadClass("oracle.sql.BLOB",null);
092            if(blobClass!=null){
093                    try {
094                            //BLOB blob = BLOB.getEmptyBLOB();
095                                    Method getEmptyBLOB = blobClass.getMethod("getEmptyBLOB",new Class[]{});
096                            Object blob = getEmptyBLOB.invoke(null, ArrayUtil.OBJECT_EMPTY);
097            
098                            //blob.setBytes(value);
099                            Method setBytes = blobClass.getMethod("setBytes", new Class[]{byte[].class});
100                            setBytes.invoke(blob, new Object[]{value});
101                            
102                            return (Blob) blob;
103                            } 
104                    catch (Exception e) {}
105            }
106            return new BlobImpl(value);
107        }*/
108        
109    
110            public void free() {
111                    binaryData=new byte[0];
112            }
113    }