001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.type.sql; 020 021import java.io.ByteArrayInputStream; 022import java.io.Serializable; 023import java.sql.Blob; 024import java.sql.SQLException; 025 026import lucee.runtime.exp.PageException; 027import lucee.runtime.op.Caster; 028 029 030 031 /** 032 * Implementation of the Interface java.sql.Blob 033 */ 034public final class BlobImpl implements java.sql.Blob, Serializable { 035 byte[] binaryData = null; 036 037 /** 038 * constructor of the class 039 * @param data 040 */ 041 private BlobImpl(byte[] data) { 042 binaryData = data; 043 } 044 045 @Override 046 public long length() throws SQLException { 047 return binaryData.length; 048 } 049 050 @Override 051 public byte[] getBytes(long pos, int length) throws SQLException { 052 byte[] newData = new byte[length]; 053 System.arraycopy(binaryData, (int) (pos - 1), newData, 0, length); 054 return newData; 055 } 056 057 @Override 058 public java.io.InputStream getBinaryStream() throws SQLException { 059 return new ByteArrayInputStream(binaryData); 060 } 061 062 @Override 063 public java.io.InputStream getBinaryStream(long pos, long length) { 064 // TODO impl this 065 return new ByteArrayInputStream(binaryData); 066 } 067 068 @Override 069 public long position(byte pattern[], long start) throws SQLException { 070 return (new String(binaryData)).indexOf(new String(pattern), (int) start); 071 } 072 073 @Override 074 public long position(java.sql.Blob pattern, long start) throws SQLException { 075 return position(pattern.getBytes(0, (int) pattern.length()), start); 076 } 077 078 @Override 079 public int setBytes(long pos, byte[] bytes) throws SQLException { 080 // TODO impl. 081 throw new SQLException("JDBC 3.0 Method setBytes not implemented"); 082 } 083 084 @Override 085 public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { 086 // TODO impl. 087 throw new SQLException("JDBC 3.0 Method setBytes not implemented"); 088 } 089 090 @Override 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 @Override 097 public void truncate(long len) throws SQLException { 098 // TODO impl. 099 throw new SQLException("JDBC 3.0 Method truncate not implemented"); 100 } 101 102 public static Blob toBlob(Object value) throws PageException { 103 if(value instanceof Blob) return (Blob)value; 104 return new BlobImpl(Caster.toBinary(value)); 105 } 106 107 /*public static Blob toBlob(byte[] value) { 108 109 Class blobClass = ClassUtil.loadClass("oracle.sql.BLOB",null); 110 if(blobClass!=null){ 111 try { 112 //BLOB blob = BLOB.getEmptyBLOB(); 113 Method getEmptyBLOB = blobClass.getMethod("getEmptyBLOB",new Class[]{}); 114 Object blob = getEmptyBLOB.invoke(null, ArrayUtil.OBJECT_EMPTY); 115 116 //blob.setBytes(value); 117 Method setBytes = blobClass.getMethod("setBytes", new Class[]{byte[].class}); 118 setBytes.invoke(blob, new Object[]{value}); 119 120 return (Blob) blob; 121 } 122 catch (Exception e) {} 123 } 124 return new BlobImpl(value); 125 }*/ 126 127 128 public void free() { 129 binaryData=new byte[0]; 130 } 131}