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         /**
028         * @see java.sql.Blob#length()
029         */
030        public long length() throws SQLException {
031             return binaryData.length;
032         }
033     
034         /**
035         * @see java.sql.Blob#getBytes(long, int)
036         */
037        public byte[] getBytes(long pos, int length) throws SQLException   {
038             byte[] newData = new byte[length]; 
039             System.arraycopy(binaryData, (int) (pos - 1), newData, 0, length);
040             return newData;
041         }
042     
043         /**
044         * @see java.sql.Blob#getBinaryStream()
045         */
046        public java.io.InputStream getBinaryStream() throws SQLException    {
047            return new ByteArrayInputStream(binaryData);
048        }
049        
050        /**
051         * @see java.sql.Blob#getBinaryStream(long, long)
052         */
053        public java.io.InputStream getBinaryStream(long pos, long length)   {
054            // TODO impl this
055            return new ByteArrayInputStream(binaryData);
056        }
057    
058         /**
059         * @see java.sql.Blob#position(byte[], long)
060         */
061        public long position(byte pattern[], long start) throws SQLException        {
062             return (new String(binaryData)).indexOf(new String(pattern), (int) start);
063         }
064     
065         /**
066         * @see java.sql.Blob#position(java.sql.Blob, long)
067         */
068        public long position(java.sql.Blob pattern, long start) throws SQLException {
069             return position(pattern.getBytes(0, (int) pattern.length()), start);
070         }
071     
072         /**
073         * @see java.sql.Blob#setBytes(long, byte[])
074         */
075        public int setBytes(long pos, byte[] bytes) throws SQLException     {
076             // TODO impl.
077             throw new SQLException("JDBC 3.0 Method setBytes not implemented");
078         }
079    
080         /**
081         * @see java.sql.Blob#setBytes(long, byte[], int, int)
082         */
083        public int setBytes(long pos, byte[] bytes, int offset, int len)    throws SQLException     {
084             // TODO impl.
085             throw new SQLException("JDBC 3.0 Method setBytes not implemented");
086         }
087     
088         /**
089         * @see java.sql.Blob#setBinaryStream(long)
090         */
091        public java.io.OutputStream setBinaryStream(long pos) throws SQLException   {
092             // TODO impl.
093             throw new SQLException("JDBC 3.0 Method setBinaryStream not implemented");
094         }
095     
096         /**
097         * @see java.sql.Blob#truncate(long)
098         */
099        public void truncate(long len) throws SQLException  {
100             // TODO impl.
101             throw new SQLException("JDBC 3.0 Method truncate not implemented");
102         }
103    
104        public static Blob toBlob(Object value) throws PageException {
105            if(value instanceof Blob) return (Blob)value;
106            return new BlobImpl(Caster.toBinary(value));
107        }
108        
109        /*public static Blob toBlob(byte[] value) {
110            
111            Class blobClass = ClassUtil.loadClass("oracle.sql.BLOB",null);
112            if(blobClass!=null){
113                    try {
114                            //BLOB blob = BLOB.getEmptyBLOB();
115                                    Method getEmptyBLOB = blobClass.getMethod("getEmptyBLOB",new Class[]{});
116                            Object blob = getEmptyBLOB.invoke(null, ArrayUtil.OBJECT_EMPTY);
117            
118                            //blob.setBytes(value);
119                            Method setBytes = blobClass.getMethod("setBytes", new Class[]{byte[].class});
120                            setBytes.invoke(blob, new Object[]{value});
121                            
122                            return (Blob) blob;
123                            } 
124                    catch (Exception e) {}
125            }
126            return new BlobImpl(value);
127        }*/
128        
129    
130            public void free() {
131                    binaryData=new byte[0];
132            }
133    }