| 1 21 package net.sf.hajdbc.sql; 22 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.FileReader ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.OutputStream ; 33 import java.io.Reader ; 34 import java.io.Writer ; 35 import java.nio.ByteBuffer ; 36 import java.nio.CharBuffer ; 37 import java.nio.channels.Channels ; 38 import java.nio.channels.FileChannel ; 39 import java.nio.channels.ReadableByteChannel ; 40 import java.nio.charset.Charset ; 41 import java.sql.Blob ; 42 import java.sql.Clob ; 43 import java.util.LinkedList ; 44 import java.util.List ; 45 46 import net.sf.hajdbc.SQLException; 47 48 52 public class FileSupportImpl implements FileSupport 53 { 54 private static final String TEMP_FILE_PREFIX = "ha-jdbc-"; 55 private static final String TEMP_FILE_SUFFIX = ".lob"; 56 private static final int BUFFER_SIZE = 8192; 57 58 private List <File > fileList = new LinkedList <File >(); 59 60 63 public File createFile(InputStream inputStream) throws java.sql.SQLException  64 { 65 File file = this.createTempFile(); 66 67 try 68 { 69 FileChannel fileChannel = new FileOutputStream (file).getChannel(); 70 ReadableByteChannel inputChannel = Channels.newChannel(inputStream); 71 72 ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); 73 74 while (inputChannel.read(buffer) > 0) 75 { 76 buffer.flip(); 77 78 fileChannel.write(buffer); 79 80 buffer.compact(); 81 } 82 83 fileChannel.close(); 84 85 return file; 86 } 87 catch (IOException e) 88 { 89 throw new SQLException(e); 90 } 91 } 92 93 96 public File createFile(Reader reader) throws java.sql.SQLException  97 { 98 File file = this.createTempFile(); 99 100 try 101 { 102 Writer writer = new FileWriter (file); 103 104 CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE); 105 106 while (reader.read(buffer) > 0) 107 { 108 buffer.flip(); 109 110 writer.append(buffer); 111 112 buffer.clear(); 113 } 114 115 writer.close(); 116 117 return file; 118 } 119 catch (IOException e) 120 { 121 throw new SQLException(e); 122 } 123 } 124 125 128 public File createFile(Blob blob) throws java.sql.SQLException  129 { 130 return this.createFile(blob.getBinaryStream()); 131 } 132 133 136 public File createFile(Clob clob) throws java.sql.SQLException  137 { 138 return this.createFile(clob.getCharacterStream()); 139 } 140 141 144 public Reader getReader(File file) throws java.sql.SQLException  145 { 146 try 147 { 148 return new BufferedReader (new FileReader (file), BUFFER_SIZE); 149 } 150 catch (IOException e) 151 { 152 throw new SQLException(e); 153 } 154 } 155 156 159 public InputStream getInputStream(File file) throws java.sql.SQLException  160 { 161 try 162 { 163 return Channels.newInputStream(new FileInputStream (file).getChannel()); 164 } 165 catch (IOException e) 166 { 167 throw new SQLException(e); 168 } 169 } 170 171 176 private File createTempFile() throws SQLException 177 { 178 try 179 { 180 File file = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX); 181 182 this.fileList.add(file); 183 184 return file; 185 } 186 catch (IOException e) 187 { 188 throw new SQLException(e); 189 } 190 } 191 192 195 public void close() 196 { 197 for (File file: this.fileList) 198 { 199 if (!file.delete()) 200 { 201 file.deleteOnExit(); 202 } 203 } 204 } 205 206 209 @Override  210 protected void finalize() throws Throwable  211 { 212 this.close(); 213 214 super.finalize(); 215 } 216 217 220 public Blob getBlob(File file) throws java.sql.SQLException  221 { 222 try 223 { 224 final FileChannel channel = new FileInputStream (file).getChannel(); 225 226 return new Blob () 227 { 228 public long length() throws java.sql.SQLException  229 { 230 try 231 { 232 return channel.size(); 233 } 234 catch (IOException e) 235 { 236 throw new SQLException(e); 237 } 238 } 239 240 public byte[] getBytes(long position, int length) throws java.sql.SQLException  241 { 242 ByteBuffer buffer = ByteBuffer.allocate(length); 243 244 try 245 { 246 channel.read(buffer, position); 247 } 248 catch (IOException e) 249 { 250 throw new SQLException(e); 251 } 252 253 buffer.compact(); 254 255 return buffer.array(); 256 } 257 258 public InputStream getBinaryStream() 259 { 260 return Channels.newInputStream(channel); 261 } 262 263 public long position(byte[] pattern, long start) 264 { 265 throw new UnsupportedOperationException (); 266 } 267 268 public long position(Blob pattern, long start) 269 { 270 throw new UnsupportedOperationException (); 271 } 272 273 public int setBytes(long position, byte[] bytes) throws java.sql.SQLException  274 { 275 return this.writeBuffer(position, ByteBuffer.wrap(bytes)); 276 } 277 278 public int setBytes(long position, byte[] bytes, int offset, int length) throws java.sql.SQLException  279 { 280 return this.writeBuffer(position, ByteBuffer.wrap(bytes, offset, length)); 281 } 282 283 private int writeBuffer(long position, ByteBuffer buffer) throws java.sql.SQLException  284 { 285 try 286 { 287 return channel.write(buffer, position); 288 } 289 catch (IOException e) 290 { 291 throw new SQLException(e); 292 } 293 } 294 295 public OutputStream setBinaryStream(long position) throws java.sql.SQLException  296 { 297 try 298 { 299 return Channels.newOutputStream(channel.position(position)); 300 } 301 catch (IOException e) 302 { 303 throw new SQLException(e); 304 } 305 } 306 307 public void truncate(long length) throws java.sql.SQLException  308 { 309 try 310 { 311 channel.truncate(length); 312 } 313 catch (IOException e) 314 { 315 throw new SQLException(e); 316 } 317 } 318 319 @Override  320 protected void finalize() throws IOException  321 { 322 channel.close(); 323 } 324 }; 325 } 326 catch (IOException e) 327 { 328 throw new SQLException(e); 329 } 330 } 331 332 335 public Clob getClob(File file) throws java.sql.SQLException  336 { 337 try 338 { 339 FileInputStream inputStream = new FileInputStream (file); 340 final Charset charset = Charset.forName(new InputStreamReader (inputStream).getEncoding()); 342 final FileChannel channel = inputStream.getChannel(); 343 344 final int charBytes = "a".getBytes(charset.name()).length; 346 347 return new Clob () 348 { 349 public long length() throws java.sql.SQLException  350 { 351 try 352 { 353 return this.charLength(channel.size()); 354 } 355 catch (IOException e) 356 { 357 throw new SQLException(e); 358 } 359 } 360 361 public String getSubString(long position, int length) throws java.sql.SQLException  362 { 363 ByteBuffer buffer = ByteBuffer.allocate(this.byteLength(length)); 364 365 try 366 { 367 channel.read(buffer, byteLength(position)); 368 } 369 catch (IOException e) 370 { 371 throw new SQLException(e); 372 } 373 374 buffer.compact(); 375 376 return String.valueOf(charset.decode(buffer).array()); 377 } 378 379 public Reader getCharacterStream() 380 { 381 return Channels.newReader(channel, charset.newDecoder(), -1); 382 } 383 384 public InputStream getAsciiStream() 385 { 386 return Channels.newInputStream(channel); 387 } 388 389 public long position(String pattern, long position) 390 { 391 throw new UnsupportedOperationException (); 392 } 393 394 public long position(Clob pattern, long position) 395 { 396 throw new UnsupportedOperationException (); 397 } 398 399 public int setString(long position, String value) throws java.sql.SQLException  400 { 401 try 402 { 403 return this.writeBuffer(position, ByteBuffer.wrap(value.getBytes(charset.name()))); 404 } 405 catch (IOException e) 406 { 407 throw new SQLException(e); 408 } 409 } 410 411 public int setString(long position, String value, int offset, int length) throws java.sql.SQLException  412 { 413 try 414 { 415 return this.writeBuffer(position, ByteBuffer.wrap(value.getBytes(charset.name()), this.byteLength(offset), this.byteLength(length))); 416 } 417 catch (IOException e) 418 { 419 throw new SQLException(e); 420 } 421 } 422 423 private int writeBuffer(long position, ByteBuffer buffer) throws java.sql.SQLException  424 { 425 try 426 { 427 return this.charLength(channel.write(buffer, this.byteLength(position))); 428 } 429 catch (IOException e) 430 { 431 throw new SQLException(e); 432 } 433 } 434 435 public OutputStream setAsciiStream(long position) throws java.sql.SQLException  436 { 437 try 438 { 439 return Channels.newOutputStream(channel.position(this.byteLength(position))); 440 } 441 catch (IOException e) 442 { 443 throw new SQLException(e); 444 } 445 } 446 447 public Writer setCharacterStream(long position) throws java.sql.SQLException  448 { 449 try 450 { 451 return Channels.newWriter(channel.position(this.byteLength(position)), charset.newEncoder(), -1); 452 } 453 catch (IOException e) 454 { 455 throw new SQLException(e); 456 } 457 } 458 459 public void truncate(long length) throws java.sql.SQLException  460 { 461 try 462 { 463 channel.truncate(this.byteLength(length)); 464 } 465 catch (IOException e) 466 { 467 throw new SQLException(e); 468 } 469 } 470 471 @Override  472 protected void finalize() throws IOException  473 { 474 channel.close(); 475 } 476 477 private int byteLength(int charLength) 478 { 479 return charLength * charBytes; 480 } 481 482 private long byteLength(long charLength) 483 { 484 return charLength * charBytes; 485 } 486 487 private int charLength(int byteLength) 488 { 489 return byteLength / charBytes; 490 } 491 492 private long charLength(long byteLength) 493 { 494 return byteLength / charBytes; 495 } 496 }; 497 } 498 catch (IOException e) 499 { 500 throw new SQLException(e); 501 } 502 } 503 } 504 | Popular Tags |