001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.io.BufferedReader;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileOutputStream;
027: import java.io.FileReader;
028: import java.io.FileWriter;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.Reader;
032: import java.io.Writer;
033: import java.nio.ByteBuffer;
034: import java.nio.CharBuffer;
035: import java.nio.channels.Channels;
036: import java.nio.channels.FileChannel;
037: import java.nio.channels.ReadableByteChannel;
038: import java.sql.SQLException;
039: import java.util.LinkedList;
040: import java.util.List;
041:
042: import net.sf.hajdbc.util.SQLExceptionFactory;
043:
044: /**
045: * @author Paul Ferraro
046: * @since 1.0
047: */
048: public class FileSupportImpl implements FileSupport {
049: private static final String TEMP_FILE_PREFIX = "ha-jdbc-"; //$NON-NLS-1$
050: private static final String TEMP_FILE_SUFFIX = ".lob"; //$NON-NLS-1$
051: private static final int BUFFER_SIZE = 8192;
052:
053: private List<File> fileList = new LinkedList<File>();
054:
055: /**
056: * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.InputStream)
057: */
058: @Override
059: public File createFile(InputStream inputStream) throws SQLException {
060: File file = this .createTempFile();
061:
062: try {
063: FileChannel fileChannel = new FileOutputStream(file)
064: .getChannel();
065: ReadableByteChannel inputChannel = Channels
066: .newChannel(inputStream);
067:
068: ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
069:
070: while (inputChannel.read(buffer) > 0) {
071: buffer.flip();
072:
073: fileChannel.write(buffer);
074:
075: buffer.compact();
076: }
077:
078: fileChannel.close();
079:
080: return file;
081: } catch (IOException e) {
082: throw SQLExceptionFactory.createSQLException(e);
083: }
084: }
085:
086: /**
087: * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.Reader)
088: */
089: @Override
090: public File createFile(Reader reader) throws SQLException {
091: File file = this .createTempFile();
092:
093: try {
094: Writer writer = new FileWriter(file);
095:
096: CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
097:
098: while (reader.read(buffer) > 0) {
099: buffer.flip();
100:
101: writer.append(buffer);
102:
103: buffer.clear();
104: }
105:
106: writer.close();
107:
108: return file;
109: } catch (IOException e) {
110: throw SQLExceptionFactory.createSQLException(e);
111: }
112: }
113:
114: /**
115: * @see net.sf.hajdbc.sql.FileSupport#getReader(java.io.File)
116: */
117: @Override
118: public Reader getReader(File file) throws SQLException {
119: try {
120: return new BufferedReader(new FileReader(file), BUFFER_SIZE);
121: } catch (IOException e) {
122: throw SQLExceptionFactory.createSQLException(e);
123: }
124: }
125:
126: /**
127: * @see net.sf.hajdbc.sql.FileSupport#getInputStream(java.io.File)
128: */
129: @Override
130: public InputStream getInputStream(File file) throws SQLException {
131: try {
132: return Channels.newInputStream(new FileInputStream(file)
133: .getChannel());
134: } catch (IOException e) {
135: throw SQLExceptionFactory.createSQLException(e);
136: }
137: }
138:
139: /**
140: * Creates a temp file and stores a reference to it so that it can be deleted later.
141: * @return a temp file
142: * @throws SQLException if an IO error occurs
143: */
144: private File createTempFile() throws SQLException {
145: try {
146: File file = File.createTempFile(TEMP_FILE_PREFIX,
147: TEMP_FILE_SUFFIX);
148:
149: this .fileList.add(file);
150:
151: return file;
152: } catch (IOException e) {
153: throw SQLExceptionFactory.createSQLException(e);
154: }
155: }
156:
157: /**
158: * @see net.sf.hajdbc.sql.FileSupport#close()
159: */
160: @Override
161: public void close() {
162: for (File file : this .fileList) {
163: if (!file.delete()) {
164: file.deleteOnExit();
165: }
166: }
167: }
168:
169: /**
170: * @see java.lang.Object#finalize()
171: */
172: @Override
173: protected void finalize() throws Throwable {
174: this.close();
175:
176: super.finalize();
177: }
178: }
|