All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src-main.org.awakefw.sql.jdbc.ClobHttp Maven / Gradle / Ivy

Go to download

Awake SQL is an open source framework that allows remote and secure JDBC access through HTTP.

The newest version!
/*
 * This file is part of Awake SQL. 
 * Awake SQL: Remote JDBC access over HTTP.                                    
 * Copyright (C) 2013,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                    
 *                                                                         
 * Awake SQL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.         
 *              
 * Awake SQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU          
 * Lesser General Public License for more details.       
 *                                  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see .
 *
 * If you develop commercial activities using Awake SQL, you must: 
 * a) disclose and distribute all source code of your own product,
 * b) license your own product under the GNU General Public License.
 * 
 * You can be released from the requirements of the license by
 * purchasing a commercial license. Buying such a license will allow you 
 * to ship Awake SQL with your closed source products without disclosing 
 * the source code.
 *
 * For more information, please contact KawanSoft SAS at this
 * address: [email protected]
 * 
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.awakefw.sql.jdbc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;

import org.apache.commons.io.IOUtils;
import org.awakefw.file.util.AwakeFileUtil;
import org.awakefw.file.util.Tag;

/**
 * Clob Wrapper. 
* Implements all the Connection methods. Usage is exactly the same as a Clob. */ public class ClobHttp implements Clob { private static final String CLOB_IS_NOT_ACCESSIBLE_ANYMORE = "CLOB is not accessible anymore.)"; private static final String CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION = Tag.AWAKE + "CLOB method is not yet implemented in Awake SQL."; /** The file that wrap the Blob */ private File file = null; private Reader reader = null; private InputStream inputStream = null; private OutputStream outputStream = null; private Writer writer = null; /** * Default Constructor */ public ClobHttp() { } /** * Constructor used when reading a downloaded Clob * * @param reader * the reader of the Clob */ public ClobHttp(Reader reader) throws SQLException { file = ClobHttp.createUniqueClobFile(); Writer writer = null; try { writer = new BufferedWriter(new FileWriter(file)); IOUtils.copy(reader, writer); } catch (Exception e) { throw new SQLException(e.getMessage(), e); } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(writer); } } /** * Constructor * * @param file * a file that is wrapped by this CLOB */ public ClobHttp(File file) { this.file = file; } /** * Create a unique clob file container * * @return a unique clob file container */ static File createUniqueClobFile() { String unique = AwakeFileUtil.getUniqueId(); File file = new File(AwakeFileUtil.getAwakeTempDir() + File.separator + "local-clob-" + unique + ".text"); return file; } /** * Returns the underlying file * @return the underlying file */ public File getFile() { return file; } /** * Retrieves the number of characters in the CLOB value * designated by this Clob object. * * @return length of the CLOB in characters * @exception SQLException * if there is an error accessing the length of the * CLOB value * @since 1.2 */ @Override public long length() throws SQLException { // Before reading, close the Output Stream if user forgot it IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(writer); if (file == null) { return 0; } if (!file.exists()) { throw new SQLException(CLOB_IS_NOT_ACCESSIBLE_ANYMORE); } return this.file.length(); } /** * Retrieves a copy of the specified substring in the CLOB * value designated by this Clob object. The substring begins * at position pos and has up to length * consecutive characters. * * @param pos * the first character of the substring to be extracted. The * first character is at position 1. * @param length * the number of consecutive characters to be copied * @return a String that is the specified substring in the * CLOB value designated by this Clob * object * @exception SQLException * if there is an error accessing the CLOB value * @since 1.2 */ @Override public String getSubString(long pos, int length) throws SQLException { // Before reading, close the Output Stream if user forgot it IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(writer); if (file == null) { return null; } if (!file.exists()) { throw new SQLException(CLOB_IS_NOT_ACCESSIBLE_ANYMORE); } try { reader = new BufferedReader(new FileReader(file)); char[] c = new char[length]; reader.skip(pos - 1); reader.read(c); return new String(c); } catch (IOException e) { throw new SQLException(e); } finally { IOUtils.closeQuietly(reader); } } /** * Retrieves the CLOB value designated by this * Clob object as a java.io.Reader object (or as a * stream of characters). * * @return a java.io.Reader object containing the * CLOB data * @exception SQLException * if there is an error accessing the CLOB value * @see #setCharacterStream * @since 1.2 */ @Override public Reader getCharacterStream() throws SQLException { // Before reading, close the Output Stream if user forgot it IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(writer); if (file == null) { return null; } if (!file.exists()) { throw new SQLException(CLOB_IS_NOT_ACCESSIBLE_ANYMORE); } try { reader = new BufferedReader(new FileReader(file)); return reader; } catch (IOException e) { throw new SQLException(e); } } /** * Retrieves the CLOB value designated by this * Clob object as an ascii stream. * * @return a java.io.InputStream object containing the * CLOB data * @exception SQLException * if there is an error accessing the CLOB value * @see #setAsciiStream * @since 1.2 */ @Override public InputStream getAsciiStream() throws SQLException { if (file == null) { return null; } if (!file.exists()) { throw new SQLException(CLOB_IS_NOT_ACCESSIBLE_ANYMORE); } try { inputStream = new BufferedInputStream(new FileInputStream(file)); return inputStream; } catch (IOException e) { throw new SQLException(e); } } /** * Retrieves the character position at which the specified substring * searchstr appears in the SQL CLOB value * represented by this Clob object. The search begins at * position start. * * @param searchstr * the substring for which to search * @param start * the position at which to begin searching; the first position * is 1 * @return the position at which the substring appears or -1 if it is not * present; the first position is 1 * @exception SQLException * if there is an error accessing the CLOB value * @since 1.2 */ @Override public long position(String searchstr, long start) throws SQLException { throw new SQLFeatureNotSupportedException( CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION); } /** * Retrieves the character position at which the specified Clob * object searchstr appears in this Clob object. * The search begins at position start. * * @param searchstr * the Clob object for which to search * @param start * the position at which to begin searching; the first position * is 1 * @return the position at which the Clob object appears or -1 * if it is not present; the first position is 1 * @exception SQLException * if there is an error accessing the CLOB value * @since 1.2 */ @Override public long position(Clob searchstr, long start) throws SQLException { throw new SQLFeatureNotSupportedException( CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION); } // ---------------------------- jdbc 3.0 ----------------------------------- /** * Writes the given Java String to the CLOB value * that this Clob object designates at the position * pos. * * @param pos * the position at which to start writing to the * CLOB value that this Clob object * represents * @param str * the string to be written to the CLOB value that * this Clob designates * @return the number of characters written * @exception SQLException * if there is an error accessing the CLOB value * * @since 1.4 */ @Override public int setString(long pos, String str) throws SQLException { throw new SQLFeatureNotSupportedException( CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION); } /** * Writes len characters of str, starting at * character offset, to the CLOB value that this * Clob represents. * * @param pos * the position at which to start writing to this * CLOB object * @param str * the string to be written to the CLOB value that * this Clob object represents * @param offset * the offset into str to start reading the * characters to be written * @param len * the number of characters to be written * @return the number of characters written * @exception SQLException * if there is an error accessing the CLOB value * * @since 1.4 */ @Override public int setString(long pos, String str, int offset, int len) throws SQLException { throw new SQLFeatureNotSupportedException( CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION); } /** * Retrieves a stream to be used to write Ascii characters to the * CLOB value that this Clob object represents, * starting at position pos. * * @param pos * the position at which to start writing to this * CLOB object * @return the stream to which ASCII encoded characters can be written * @exception SQLException * if there is an error accessing the CLOB value * @see #getAsciiStream * * @since 1.4 */ @Override public OutputStream setAsciiStream(long pos) throws SQLException { file = createUniqueClobFile(); try { outputStream = new BufferedOutputStream(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw new SQLException(e); } return outputStream; } /** * Retrieves a stream to be used to write a stream of Unicode characters to * the CLOB value that this Clob object * represents, at position pos. * * @param pos * the position at which to start writing to the * CLOB value * * @return a stream to which Unicode encoded characters can be written * @exception SQLException * if there is an error accessing the CLOB value * @see #getCharacterStream * * @since 1.4 */ @Override public Writer setCharacterStream(long pos) throws SQLException { file = createUniqueClobFile(); try { writer = new BufferedWriter(new FileWriter(file)); } catch (IOException e) { throw new SQLException(e); } return writer; } /** * Truncates the CLOB value that this Clob * designates to have a length of len characters. * * @param len * the length, in bytes, to which the CLOB value * should be truncated * @exception SQLException * if there is an error accessing the CLOB value * * @since 1.4 */ @Override public void truncate(long len) throws SQLException { throw new SQLFeatureNotSupportedException( CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION); } /** * This method frees the Clob object and releases the resources * the resources that it holds. The object is invalid once the * free method is called. *

* After free has been called, any attempt to invoke a method * other than free will result in a SQLException * being thrown. If free is called multiple times, the * subsequent calls to free are treated as a no-op. *

* * @throws SQLException * if an error occurs releasing the Clob's resources * * @exception SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ @Override public void free() throws SQLException { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(reader); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(writer); if (file != null) { this.file.delete(); } } /** * Closes the underlying input/output stream */ public void close() { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(reader); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(writer); } /** * Returns a Reader object that contains a partial * Clob value, starting with the character specified by pos, * which is length characters in length. * * @param pos * the offset to the first character of the partial value to be * retrieved. The first character in the Clob is at position 1. * @param length * the length in characters of the partial value to be retrieved. * @return Reader through which the partial Clob * value can be read. * @throws SQLException * if pos is less than 1 or if pos is greater than the number of * characters in the Clob or if pos + length is * greater than the number of characters in the * Clob * * @exception SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ @Override public Reader getCharacterStream(long pos, long length) throws SQLException { throw new SQLFeatureNotSupportedException( CLOB_FEATURE_NOT_SUPPORTED_IN_THIS_VERSION); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy