KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > FileSupportImpl


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.sql;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.nio.ByteBuffer JavaDoc;
36 import java.nio.CharBuffer JavaDoc;
37 import java.nio.channels.Channels JavaDoc;
38 import java.nio.channels.FileChannel JavaDoc;
39 import java.nio.channels.ReadableByteChannel JavaDoc;
40 import java.nio.charset.Charset JavaDoc;
41 import java.sql.Blob JavaDoc;
42 import java.sql.Clob JavaDoc;
43 import java.util.LinkedList JavaDoc;
44 import java.util.List JavaDoc;
45
46 import net.sf.hajdbc.SQLException;
47
48 /**
49  * @author Paul Ferraro
50  * @since 1.0
51  */

52 public class FileSupportImpl implements FileSupport
53 {
54     private static final String JavaDoc TEMP_FILE_PREFIX = "ha-jdbc-";
55     private static final String JavaDoc TEMP_FILE_SUFFIX = ".lob";
56     private static final int BUFFER_SIZE = 8192;
57     
58     private List JavaDoc<File JavaDoc> fileList = new LinkedList JavaDoc<File JavaDoc>();
59     
60     /**
61      * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.InputStream)
62      */

63     public File JavaDoc createFile(InputStream JavaDoc inputStream) throws java.sql.SQLException JavaDoc
64     {
65         File JavaDoc file = this.createTempFile();
66         
67         try
68         {
69             FileChannel JavaDoc fileChannel = new FileOutputStream JavaDoc(file).getChannel();
70             ReadableByteChannel JavaDoc inputChannel = Channels.newChannel(inputStream);
71             
72             ByteBuffer JavaDoc 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 JavaDoc e)
88         {
89             throw new SQLException(e);
90         }
91     }
92     
93     /**
94      * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.Reader)
95      */

96     public File JavaDoc createFile(Reader JavaDoc reader) throws java.sql.SQLException JavaDoc
97     {
98         File JavaDoc file = this.createTempFile();
99         
100         try
101         {
102             Writer JavaDoc writer = new FileWriter JavaDoc(file);
103             
104             CharBuffer JavaDoc 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 JavaDoc e)
120         {
121             throw new SQLException(e);
122         }
123     }
124     
125     /**
126      * @see net.sf.hajdbc.sql.FileSupport#createFile(java.sql.Blob)
127      */

128     public File JavaDoc createFile(Blob JavaDoc blob) throws java.sql.SQLException JavaDoc
129     {
130         return this.createFile(blob.getBinaryStream());
131     }
132
133     /**
134      * @see net.sf.hajdbc.sql.FileSupport#createFile(java.sql.Clob)
135      */

136     public File JavaDoc createFile(Clob JavaDoc clob) throws java.sql.SQLException JavaDoc
137     {
138         return this.createFile(clob.getCharacterStream());
139     }
140
141     /**
142      * @see net.sf.hajdbc.sql.FileSupport#getReader(java.io.File)
143      */

144     public Reader JavaDoc getReader(File JavaDoc file) throws java.sql.SQLException JavaDoc
145     {
146         try
147         {
148             return new BufferedReader JavaDoc(new FileReader JavaDoc(file), BUFFER_SIZE);
149         }
150         catch (IOException JavaDoc e)
151         {
152             throw new SQLException(e);
153         }
154     }
155     
156     /**
157      * @see net.sf.hajdbc.sql.FileSupport#getInputStream(java.io.File)
158      */

159     public InputStream JavaDoc getInputStream(File JavaDoc file) throws java.sql.SQLException JavaDoc
160     {
161         try
162         {
163             return Channels.newInputStream(new FileInputStream JavaDoc(file).getChannel());
164         }
165         catch (IOException JavaDoc e)
166         {
167             throw new SQLException(e);
168         }
169     }
170     
171     /**
172      * Creates a temp file and stores a reference to it so that it can be deleted later.
173      * @return a temp file
174      * @throws SQLException if an IO error occurs
175      */

176     private File JavaDoc createTempFile() throws SQLException
177     {
178         try
179         {
180             File JavaDoc file = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
181             
182             this.fileList.add(file);
183             
184             return file;
185         }
186         catch (IOException JavaDoc e)
187         {
188             throw new SQLException(e);
189         }
190     }
191     
192     /**
193      * @see net.sf.hajdbc.sql.FileSupport#close()
194      */

195     public void close()
196     {
197         for (File JavaDoc file: this.fileList)
198         {
199             if (!file.delete())
200             {
201                 file.deleteOnExit();
202             }
203         }
204     }
205     
206     /**
207      * @see java.lang.Object#finalize()
208      */

209     @Override JavaDoc
210     protected void finalize() throws Throwable JavaDoc
211     {
212         this.close();
213         
214         super.finalize();
215     }
216
217     /**
218      * @see net.sf.hajdbc.sql.FileSupport#getBlob(java.io.File)
219      */

220     public Blob JavaDoc getBlob(File JavaDoc file) throws java.sql.SQLException JavaDoc
221     {
222         try
223         {
224             final FileChannel JavaDoc channel = new FileInputStream JavaDoc(file).getChannel();
225         
226             return new Blob JavaDoc()
227             {
228                 public long length() throws java.sql.SQLException JavaDoc
229                 {
230                     try
231                     {
232                         return channel.size();
233                     }
234                     catch (IOException JavaDoc e)
235                     {
236                         throw new SQLException(e);
237                     }
238                 }
239     
240                 public byte[] getBytes(long position, int length) throws java.sql.SQLException JavaDoc
241                 {
242                     ByteBuffer JavaDoc buffer = ByteBuffer.allocate(length);
243                     
244                     try
245                     {
246                         channel.read(buffer, position);
247                     }
248                     catch (IOException JavaDoc e)
249                     {
250                         throw new SQLException(e);
251                     }
252                     
253                     buffer.compact();
254                     
255                     return buffer.array();
256                 }
257     
258                 public InputStream JavaDoc getBinaryStream()
259                 {
260                     return Channels.newInputStream(channel);
261                 }
262     
263                 public long position(byte[] pattern, long start)
264                 {
265                     throw new UnsupportedOperationException JavaDoc();
266                 }
267     
268                 public long position(Blob JavaDoc pattern, long start)
269                 {
270                     throw new UnsupportedOperationException JavaDoc();
271                 }
272     
273                 public int setBytes(long position, byte[] bytes) throws java.sql.SQLException JavaDoc
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 JavaDoc
279                 {
280                     return this.writeBuffer(position, ByteBuffer.wrap(bytes, offset, length));
281                 }
282
283                 private int writeBuffer(long position, ByteBuffer JavaDoc buffer) throws java.sql.SQLException JavaDoc
284                 {
285                     try
286                     {
287                         return channel.write(buffer, position);
288                     }
289                     catch (IOException JavaDoc e)
290                     {
291                         throw new SQLException(e);
292                     }
293                 }
294                 
295                 public OutputStream JavaDoc setBinaryStream(long position) throws java.sql.SQLException JavaDoc
296                 {
297                     try
298                     {
299                         return Channels.newOutputStream(channel.position(position));
300                     }
301                     catch (IOException JavaDoc e)
302                     {
303                         throw new SQLException(e);
304                     }
305                 }
306     
307                 public void truncate(long length) throws java.sql.SQLException JavaDoc
308                 {
309                     try
310                     {
311                         channel.truncate(length);
312                     }
313                     catch (IOException JavaDoc e)
314                     {
315                         throw new SQLException(e);
316                     }
317                 }
318                 
319                 @Override JavaDoc
320                 protected void finalize() throws IOException JavaDoc
321                 {
322                     channel.close();
323                 }
324             };
325         }
326         catch (IOException JavaDoc e)
327         {
328             throw new SQLException(e);
329         }
330     }
331
332     /**
333      * @see net.sf.hajdbc.sql.FileSupport#getClob(java.io.File)
334      */

335     public Clob JavaDoc getClob(File JavaDoc file) throws java.sql.SQLException JavaDoc
336     {
337         try
338         {
339             FileInputStream JavaDoc inputStream = new FileInputStream JavaDoc(file);
340             // Determine the charset of the character data
341
final Charset JavaDoc charset = Charset.forName(new InputStreamReader JavaDoc(inputStream).getEncoding());
342             final FileChannel JavaDoc channel = inputStream.getChannel();
343             
344             // Calculate the number of bytes in a single character for the current charset
345
final int charBytes = "a".getBytes(charset.name()).length;
346             
347             return new Clob JavaDoc()
348             {
349                 public long length() throws java.sql.SQLException JavaDoc
350                 {
351                     try
352                     {
353                         return this.charLength(channel.size());
354                     }
355                     catch (IOException JavaDoc e)
356                     {
357                         throw new SQLException(e);
358                     }
359                 }
360
361                 public String JavaDoc getSubString(long position, int length) throws java.sql.SQLException JavaDoc
362                 {
363                     ByteBuffer JavaDoc buffer = ByteBuffer.allocate(this.byteLength(length));
364                     
365                     try
366                     {
367                         channel.read(buffer, byteLength(position));
368                     }
369                     catch (IOException JavaDoc 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 JavaDoc getCharacterStream()
380                 {
381                     return Channels.newReader(channel, charset.newDecoder(), -1);
382                 }
383
384                 public InputStream JavaDoc getAsciiStream()
385                 {
386                     return Channels.newInputStream(channel);
387                 }
388
389                 public long position(String JavaDoc pattern, long position)
390                 {
391                     throw new UnsupportedOperationException JavaDoc();
392                 }
393
394                 public long position(Clob JavaDoc pattern, long position)
395                 {
396                     throw new UnsupportedOperationException JavaDoc();
397                 }
398
399                 public int setString(long position, String JavaDoc value) throws java.sql.SQLException JavaDoc
400                 {
401                     try
402                     {
403                         return this.writeBuffer(position, ByteBuffer.wrap(value.getBytes(charset.name())));
404                     }
405                     catch (IOException JavaDoc e)
406                     {
407                         throw new SQLException(e);
408                     }
409                 }
410
411                 public int setString(long position, String JavaDoc value, int offset, int length) throws java.sql.SQLException JavaDoc
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 JavaDoc e)
418                     {
419                         throw new SQLException(e);
420                     }
421                 }
422
423                 private int writeBuffer(long position, ByteBuffer JavaDoc buffer) throws java.sql.SQLException JavaDoc
424                 {
425                     try
426                     {
427                         return this.charLength(channel.write(buffer, this.byteLength(position)));
428                     }
429                     catch (IOException JavaDoc e)
430                     {
431                         throw new SQLException(e);
432                     }
433                 }
434
435                 public OutputStream JavaDoc setAsciiStream(long position) throws java.sql.SQLException JavaDoc
436                 {
437                     try
438                     {
439                         return Channels.newOutputStream(channel.position(this.byteLength(position)));
440                     }
441                     catch (IOException JavaDoc e)
442                     {
443                         throw new SQLException(e);
444                     }
445                 }
446
447                 public Writer JavaDoc setCharacterStream(long position) throws java.sql.SQLException JavaDoc
448                 {
449                     try
450                     {
451                         return Channels.newWriter(channel.position(this.byteLength(position)), charset.newEncoder(), -1);
452                     }
453                     catch (IOException JavaDoc e)
454                     {
455                         throw new SQLException(e);
456                     }
457                 }
458
459                 public void truncate(long length) throws java.sql.SQLException JavaDoc
460                 {
461                     try
462                     {
463                         channel.truncate(this.byteLength(length));
464                     }
465                     catch (IOException JavaDoc e)
466                     {
467                         throw new SQLException(e);
468                     }
469                 }
470                 
471                 @Override JavaDoc
472                 protected void finalize() throws IOException JavaDoc
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 JavaDoc e)
499         {
500             throw new SQLException(e);
501         }
502     }
503 }
504
Popular Tags