1 21 22 package org.dbunit.dataset.datatype; 23 24 import org.dbunit.dataset.ITable; 25 import org.dbunit.util.Base64; 26 27 import java.io.*; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.sql.Blob ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 35 40 public class BytesDataType extends AbstractDataType 41 { 42 private static final int MAX_URI_LENGTH = 256; 43 44 BytesDataType(String name, int sqlType) 45 { 46 super(name, sqlType, byte[].class, false); 47 } 48 49 private byte[] toByteArray(InputStream in, int length) throws IOException 50 { 51 ByteArrayOutputStream out = new ByteArrayOutputStream(length); 52 in = new BufferedInputStream(in); 53 int i = in.read(); 54 while (i != -1) 55 { 56 out.write(i); 57 i = in.read(); 58 } 59 return out.toByteArray(); 60 } 61 62 65 public Object typeCast(Object value) throws TypeCastException 66 { 67 if (value == null || value == ITable.NO_VALUE) 68 { 69 return null; 70 } 71 72 if (value instanceof byte[]) 73 { 74 return value; 75 } 76 77 if (value instanceof String ) 78 { 79 String stringValue = (String )value; 80 81 if (stringValue.length() == 0 || stringValue.length() > MAX_URI_LENGTH) 83 { 84 return Base64.decode((String )value); 85 } 86 87 try 88 { 89 try 90 { 91 URL url = new URL (stringValue); 93 return toByteArray(url.openStream(), 0); 94 } 95 catch (MalformedURLException e1) 96 { 97 try 98 { 99 File file = new File(stringValue); 101 return toByteArray(new FileInputStream(file), 102 (int)file.length()); 103 } 104 catch (FileNotFoundException e2) 105 { 106 return Base64.decode((String )value); 108 } 109 } 110 } 111 catch (IOException e) 112 { 113 throw new TypeCastException(value, this, e); 114 } 115 } 116 117 if (value instanceof Blob ) 118 { 119 try 120 { 121 Blob blobValue = (Blob )value; 122 return blobValue.getBytes(1, (int)blobValue.length()); 123 } 124 catch (SQLException e) 125 { 126 throw new TypeCastException(value, this, e); 127 } 128 } 129 130 if (value instanceof URL ) 131 { 132 try 133 { 134 return toByteArray(((URL )value).openStream(), 0); 135 } 136 catch (IOException e) 137 { 138 throw new TypeCastException(value, this, e); 139 } 140 } 141 142 if (value instanceof File) 143 { 144 try 145 { 146 File file = (File)value; 147 return toByteArray(new FileInputStream(file), 148 (int)file.length()); 149 } 150 catch (IOException e) 151 { 152 throw new TypeCastException(value, this, e); 153 } 154 } 155 156 throw new TypeCastException(value, this); 157 } 158 159 public int compare(Object o1, Object o2) throws TypeCastException 160 { 161 try 162 { 163 byte[] value1 = (byte[])typeCast(o1); 164 byte[] value2 = (byte[])typeCast(o2); 165 166 if (value1 == null && value2 == null) 167 { 168 return 0; 169 } 170 171 if (value1 == null && value2 != null) 172 { 173 return -1; 174 } 175 176 if (value1 != null && value2 == null) 177 { 178 return 1; 179 } 180 181 return compare(value1, value2); 182 } 183 catch (ClassCastException e) 184 { 185 throw new TypeCastException(e); 186 } 187 } 188 189 public int compare(byte[] v1, byte[] v2) throws TypeCastException 190 { 191 int len1 = v1.length; 192 int len2 = v2.length; 193 int n = Math.min(len1, len2); 194 int i = 0; 195 int j = 0; 196 197 if (i == j) 198 { 199 int k = i; 200 int lim = n + i; 201 while (k < lim) 202 { 203 byte c1 = v1[k]; 204 byte c2 = v2[k]; 205 if (c1 != c2) 206 { 207 return c1 - c2; 208 } 209 k++; 210 } 211 } 212 else 213 { 214 while (n-- != 0) 215 { 216 byte c1 = v1[i++]; 217 byte c2 = v2[j++]; 218 if (c1 != c2) 219 { 220 return c1 - c2; 221 } 222 } 223 } 224 return len1 - len2; 225 } 226 227 public Object getSqlValue(int column, ResultSet resultSet) 228 throws SQLException , TypeCastException 229 { 230 byte[] value = resultSet.getBytes(column); 231 if (value == null || resultSet.wasNull()) 232 { 233 return null; 234 } 235 return value; 236 } 237 238 public void setSqlValue(Object value, int column, PreparedStatement statement) 239 throws SQLException , TypeCastException 240 { 241 super.setSqlValue(value, column, statement); 242 } 243 244 } 245 246 247 248 249 250 251 | Popular Tags |