1 package org.apache.ojb.broker.platforms; 2 3 17 18 import org.apache.ojb.broker.util.logging.Logger; 19 import org.apache.ojb.broker.util.logging.LoggerFactory; 20 21 import java.io.*; 22 import java.sql.Connection ; 23 import java.lang.reflect.InvocationTargetException ; 24 25 33 public class Oracle9iLobHandler 34 { 35 36 protected static Logger logger = LoggerFactory.getLogger(Oracle9iLobHandler.class); 37 38 private static ClobWrapper createTempCLOB(Connection connection, ClobWrapper clob) 39 { 40 if (clob == null) { 41 return null; 42 } 43 44 ClobWrapper tempClob = null; 45 try 46 { 47 tempClob = ClobWrapper.createTemporary(connection, true, ClobWrapper.getDurationSessionValue()); 49 50 clob.open(ClobWrapper.getModeReadOnlyValue()); 52 53 tempClob.open(ClobWrapper.getModeReadWriteValue()); 55 56 int bytesread; 58 59 Reader clobReader = clob.getCharacterStream(); 61 62 Writer tempClobWriter = tempClob.getCharacterOutputStream(); 64 65 char[] charbuffer = new char[clob.getBufferSize()]; 68 69 while ((bytesread = clobReader.read(charbuffer)) != -1) 71 { 72 tempClobWriter.write(charbuffer, 0, bytesread); 73 } 74 75 tempClobWriter.flush(); 77 tempClobWriter.close(); 78 clobReader.close(); 79 80 clob.close(); 82 tempClob.close(); 83 } 84 catch (Exception e) 85 { 86 logger.error("Error during temporary CLOB write", e); 87 freeTempLOB(tempClob, null); 88 } 89 return tempClob; 90 } 91 92 public static String convertCLOBtoString(Connection connection, Object nativeclob) 93 { 94 ClobWrapper temp = new ClobWrapper(); 95 temp.setClob(nativeclob); 96 99 ClobWrapper clob = createTempCLOB(connection, temp); 100 101 if (clob == null) { 102 return null; 103 } 104 105 String retval = null; 106 StringBuffer clobdata = new StringBuffer (); 108 int bytesread = 0; 110 try 111 { 112 clob.open(ClobWrapper.getModeReadOnlyValue()); 114 Reader clobReader = clob.getCharacterStream(); 116 char[] charbuffer = new char[clob.getBufferSize()]; 119 while ((bytesread = clobReader.read(charbuffer)) != -1) 122 { 123 clobdata.append(charbuffer, 0, bytesread); 124 } 125 clobReader.close(); 127 clob.close(); 129 retval = clobdata.toString(); 130 clobdata = null; 131 } 132 catch (Exception e) 133 { 134 logger.error("Error during CLOB read", e); 135 freeTempLOB(clob, null); 136 } 137 return retval; 138 } 139 140 public static Object createCLOBFromString(Connection conn, String clobData) 141 { 142 if (clobData == null) { 143 return null; 144 } 145 ClobWrapper clob = null; 146 try 147 { 148 clob = ClobWrapper.createTemporary(conn, true, ClobWrapper.getDurationSessionValue()); 149 if (clob != null) { 150 clob.open(ClobWrapper.getModeReadWriteValue()); 152 153 clob.trim(0); 155 156 Writer tempClobWriter = clob.getCharacterOutputStream(); 158 159 if (tempClobWriter != null) { 160 tempClobWriter.write(clobData); 162 163 tempClobWriter.flush(); 165 tempClobWriter.close(); 166 } 167 168 clob.close(); 170 } 171 } 172 catch (InvocationTargetException ite) { 173 Throwable t = ite.getTargetException(); 174 freeTempLOB(clob, null); 175 if (t instanceof java.lang.UnsatisfiedLinkError ) { 176 logger.error("Oracle JDBC-driver version does not match installed OCI-driver"); 177 } else { 178 logger.error("Error during temporary CLOB write", t); 179 } 180 } 181 catch (Exception e) 182 { 183 logger.error("Error during temporary CLOB write", e); 184 freeTempLOB(clob, null); 185 } 186 return clob == null ? null : clob.getClob(); 187 } 188 189 private static BlobWrapper createTempBLOB(Connection connection, BlobWrapper blob) 190 { 191 if (blob == null) { 192 return null; 193 } 194 195 BlobWrapper tempBlob = null; 196 try 197 { 198 tempBlob = BlobWrapper.createTemporary(connection, true, BlobWrapper.getDurationSessionValue()); 200 201 blob.open(BlobWrapper.getModeReadOnlyValue()); 203 204 tempBlob.open(BlobWrapper.getModeReadWriteValue()); 206 207 int bytesread; 209 210 InputStream blobInputStream = blob.getBinaryStream(); 212 213 OutputStream tempBlobOutputStream = tempBlob.getBinaryOutputStream(); 215 216 byte[] bytebuffer = new byte[blob.getBufferSize()]; 219 220 while ((bytesread = blobInputStream.read(bytebuffer)) != -1) 222 { 223 tempBlobOutputStream.write(bytebuffer, 0, bytesread); 224 } 225 226 tempBlobOutputStream.flush(); 228 tempBlobOutputStream.close(); 229 blobInputStream.close(); 230 231 blob.close(); 233 tempBlob.close(); 234 } 235 catch (Exception e) 236 { 237 logger.error("Error during temporary BLOB write", e); 238 freeTempLOB(null, tempBlob); 239 } 240 return tempBlob; 241 } 242 243 public static byte[] convertBLOBtoByteArray(Connection connection, Object nativeblob) 244 { 245 BlobWrapper temp = new BlobWrapper(); 246 temp.setBlob(nativeblob); 247 250 BlobWrapper blob = createTempBLOB(connection, temp); 251 if (blob == null) { 252 return null; 253 } 254 255 byte[] retval = null; 256 ByteArrayOutputStream blobdata = new ByteArrayOutputStream(); 258 int bytesread = 0; 260 try 261 { 262 blob.open(BlobWrapper.getModeReadOnlyValue()); 264 InputStream blobInputStream = blob.getBinaryStream(); 266 byte[] bytebuffer = new byte[blob.getBufferSize()]; 269 while ((bytesread = blobInputStream.read(bytebuffer)) != -1) 272 { 273 blobdata.write(bytebuffer, 0, bytesread); 274 } 275 blobInputStream.close(); 277 blobdata.flush(); 278 blobdata.close(); 279 280 blob.close(); 282 retval = blobdata.toByteArray(); 283 blobdata = null; 284 } 285 catch (Exception e) 286 { 287 logger.error("Error during BLOB read", e); 288 freeTempLOB(null, blob); 289 } 290 return retval; 291 } 292 293 public static Object createBLOBFromByteArray(Connection conn, byte[] blobData) 294 { 295 if (blobData == null) { 296 return null; 297 } 298 299 BlobWrapper blob = null; 300 try 301 { 302 blob = BlobWrapper.createTemporary(conn, true, BlobWrapper.getDurationSessionValue()); 303 304 blob.open(BlobWrapper.getModeReadWriteValue()); 306 307 blob.trim(0); 309 310 OutputStream tempBlobOutputStream = blob.getBinaryOutputStream(); 312 313 tempBlobOutputStream.write(blobData); 315 316 tempBlobOutputStream.flush(); 318 tempBlobOutputStream.close(); 319 320 blob.close(); 322 } 323 catch (InvocationTargetException ite) { 324 Throwable t = ite.getTargetException(); 325 freeTempLOB(null, blob); 326 if (t instanceof java.lang.UnsatisfiedLinkError ) { 327 logger.error("Oracle JDBC-driver version does not match installed OCI-driver"); 328 } else { 329 logger.error("Error during temporary BLOB write", t); 330 } 331 } 332 catch (Exception e) 333 { 334 logger.error("Error during temporary BLOB write", e); 335 freeTempLOB(null, blob); 336 } 337 return blob == null ? null : blob.getBlob(); 338 } 339 340 347 private static void freeTempLOB(ClobWrapper clob, BlobWrapper blob) 348 { 349 try 350 { 351 if (clob != null) 352 { 353 if (clob.isOpen()) 355 { 356 clob.close(); 357 } 358 359 clob.freeTemporary(); 361 } 362 363 if (blob != null) 364 { 365 if (blob.isOpen()) 367 { 368 blob.close(); 369 } 370 371 blob.freeTemporary(); 373 } 374 } 375 catch (Exception e) 376 { 377 logger.error("Error during temporary LOB release", e); 378 } 379 } 380 381 } 382 | Popular Tags |