00001 package cedar.jetweb.db; 00002 00003 import java.sql.Blob; 00004 import java.sql.SQLException; 00005 import java.util.Vector; 00006 00014 public abstract class DBBlob2Array{ 00015 00019 public static Vector<Integer> getInteger(Blob blob) 00020 throws SQLException{ 00021 00022 Vector<Integer> column = null; 00023 00024 if(blob==null)return column; 00025 00026 byte theBytes[] = blob.getBytes((long)1, (int)blob.length()); 00027 00028 column = new Vector<Integer>(); 00029 00030 //check that we have a multiple of 4 bytes for turning into Ints 00031 //If not, discard the leftover bytes. 00032 00033 int numInts = theBytes.length / 4; 00034 numInts *=4; 00035 for(int ii=0; ii< numInts; ii+=4){ 00036 int anInt = 0; 00037 for(int jj=0; jj!=4; ++jj){ 00038 00039 int piece = (int)theBytes[ii+jj]; 00040 00041 //System.out.println(piece); 00042 00043 //casting thinks it should be a signed int 00044 if(piece<0&&jj!=3) { 00045 piece+=256; 00046 } 00047 00048 piece<<=8*jj; 00049 00050 //System.out.println(piece); 00051 00052 anInt+=piece; 00053 } 00054 column.add(anInt); 00055 } 00056 00057 return(column); 00058 } 00060 00064 public static byte[] byteMe(Vector<Integer> intArray){ 00065 00066 byte byteArray[] = new byte[intArray.size()*4]; 00067 00068 int kk=0; 00069 00070 for(int ii=0; ii!= intArray.size(); ++ii){ 00071 int anInt = intArray.get(ii); 00072 for(int jj=0; jj!=4; ++jj){ 00073 byteArray[kk] = (new Integer(anInt)).byteValue(); 00074 anInt>>=8; 00075 ++kk; 00076 } 00077 } 00078 00079 return(byteArray); 00080 } 00082 00088 public static byte[] byteMe(Object obj){ 00089 return(byteMe( (Vector<Integer>)obj )); 00090 } 00091 00092 00094 00102 public static Object convert (Blob blob, Class cl) 00103 throws SQLException{ 00104 00105 if(cl.equals(Vector.class)){ 00106 return(getInteger(blob)); 00107 }else if (cl.equals(Blob.class)){ 00108 return(blob); 00109 }else{ 00110 00111 System.out.println("Can only convert Blobs to vectors and blobs"); 00112 System.out.println("you attempted to convert a blob to a "+ 00113 cl.getName()); 00114 00115 return(null); 00116 } 00117 } 00118 00119 }
Generated Wed Jan 17 09:14:27 GMT 2007