1 21 package org.apache.derby.impl.drda; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.BufferedInputStream ; 27 import java.sql.ResultSet ; 28 import java.sql.Blob ; 29 import java.sql.Clob ; 30 import java.sql.SQLException ; 31 32 import java.io.UnsupportedEncodingException ; 33 34 import org.apache.derby.iapi.reference.DRDAConstants; 35 import org.apache.derby.iapi.services.sanity.SanityManager; 36 import org.apache.derby.impl.jdbc.Util; 37 38 49 class EXTDTAInputStream extends InputStream { 50 51 private InputStream binaryInputStream = null; 52 53 private boolean isEmptyStream; 54 55 private ResultSet dataResultSet = null; 56 private Blob blob = null; 57 private Clob clob = null; 58 59 private EXTDTAInputStream(ResultSet rs, 60 int columnNumber, 61 int ndrdaType) 62 throws SQLException , IOException 63 { 64 65 this.dataResultSet = rs; 66 this.isEmptyStream = ! initInputStream(rs, 67 columnNumber, 68 ndrdaType); 69 70 } 71 72 73 74 98 public static EXTDTAInputStream getEXTDTAStream(ResultSet rs, int column, int drdaType) 99 throws SQLException { 100 try{ 101 int ndrdaType = drdaType | 1; 103 return new EXTDTAInputStream(rs, 104 column, 105 ndrdaType); 106 107 }catch(IOException e){ 108 throw new SQLException (e.getMessage()); 109 } 110 111 } 112 113 114 123 private static long getInputStreamLength(InputStream binaryInputStream) 124 throws SQLException { 125 long length = 0; 126 if (binaryInputStream == null) 127 return length; 128 129 try { 130 for (;;) { 131 int avail = binaryInputStream.available(); 132 binaryInputStream.skip(avail); 133 if (avail == 0) 134 break; 135 length += avail; 136 137 } 138 } catch (IOException ioe) { 140 throw Util.javaException(ioe); 141 } 142 143 return length; 144 145 } 146 147 148 153 public int read() throws IOException { 154 return binaryInputStream.read(); 155 } 156 157 162 public int available() throws IOException { 163 return binaryInputStream.available(); 164 } 165 166 171 public void close() throws IOException { 172 173 try{ 174 if (binaryInputStream != null) 175 binaryInputStream.close(); 176 binaryInputStream = null; 177 178 }finally{ 179 180 blob = null; 181 clob = null; 182 dataResultSet = null; 183 } 184 185 } 186 187 192 public boolean equals(Object arg0) { 193 return binaryInputStream.equals(arg0); 194 } 195 196 201 public int hashCode() { 202 return binaryInputStream.hashCode(); 203 } 204 205 210 public void mark(int arg0) { 211 binaryInputStream.mark(arg0); 212 } 213 214 219 public boolean markSupported() { 220 return binaryInputStream.markSupported(); 221 } 222 223 228 public int read(byte[] arg0) throws IOException { 229 return binaryInputStream.read(arg0); 230 } 231 232 237 public int read(byte[] arg0, int arg1, int arg2) throws IOException { 238 return binaryInputStream.read(arg0, arg1, arg2); 239 } 240 241 246 public void reset() throws IOException { 247 binaryInputStream.reset(); 248 } 249 250 255 public long skip(long arg0) throws IOException { 256 return binaryInputStream.skip(arg0); 257 } 258 259 260 protected boolean isEmptyStream(){ 261 return isEmptyStream; 262 } 263 264 265 277 private boolean initInputStream(ResultSet rs, 278 int column, 279 int ndrdaType) 280 throws SQLException , 281 IOException 282 { 283 284 InputStream is = null; 285 try{ 286 if (ndrdaType == DRDAConstants.DRDA_TYPE_NLOBBYTES) 288 { 289 blob = rs.getBlob(column); 290 if(blob == null){ 291 return false; 292 } 293 294 is = blob.getBinaryStream(); 295 296 } 297 else if (ndrdaType == DRDAConstants.DRDA_TYPE_NLOBCMIXED) 299 { 300 try { 301 clob = rs.getClob(column); 302 303 if(clob == null){ 304 return false; 305 } 306 307 is = new ReEncodedInputStream(clob.getCharacterStream()); 308 309 }catch (java.io.UnsupportedEncodingException e) { 310 throw new SQLException (e.getMessage()); 311 312 }catch (IOException e){ 313 throw new SQLException (e.getMessage()); 314 315 } 316 317 } 318 else 319 { 320 if (SanityManager.DEBUG) 321 { 322 SanityManager.THROWASSERT("NDRDAType: " + ndrdaType + 323 " not valid EXTDTA object type"); 324 } 325 } 326 327 boolean exist = is.read() > -1; 328 329 is.close(); 330 is = null; 331 332 if(exist){ 333 openInputStreamAgain(); 334 } 335 336 return exist; 337 338 }catch(IllegalStateException e){ 339 throw Util.javaException(e); 340 341 }finally{ 342 if(is != null) 343 is.close(); 344 345 } 346 347 } 348 349 350 356 private void openInputStreamAgain() throws IllegalStateException ,SQLException { 357 358 if(this.binaryInputStream != null){ 359 return; 360 } 361 362 InputStream is = null; 363 try{ 364 365 if(SanityManager.DEBUG){ 366 SanityManager.ASSERT( ( blob != null && clob == null ) || 367 ( clob != null && blob == null ), 368 "One of blob or clob must be non-null."); 369 } 370 371 if(blob != null){ 372 is = blob.getBinaryStream(); 373 374 }else if(clob != null){ 375 is = new ReEncodedInputStream(clob.getCharacterStream()); 376 } 377 378 }catch(IOException e){ 379 throw new IllegalStateException (e.getMessage()); 380 } 381 382 if(! is.markSupported() ){ 383 is = new BufferedInputStream (is); 384 } 385 386 this.binaryInputStream = is; 387 388 } 389 390 391 protected void finalize() throws Throwable { 392 close(); 393 } 394 395 396 } 397 | Popular Tags |