![JAR search and dependency download from the Maven repository](/logo.png)
org.kawanfw.sql.jdbc.util.ResultSetFileSplitter Maven / Gradle / Ivy
/*
* This file is part of AceQL.
* AceQL: Remote JDBC access over HTTP.
* Copyright (C) 2015, KawanSoft SAS
* (http://www.kawansoft.com). All rights reserved.
*
* AceQL is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* AceQL 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
* Any modifications to this file must keep this entire header
* intact.
*/
package org.kawanfw.sql.jdbc.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import org.apache.commons.io.IOUtils;
import org.kawanfw.commons.util.ClientLogger;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.commons.util.KeepTempFilePolicyParms;
import org.kawanfw.commons.util.Tag;
import org.kawanfw.sql.util.FileSplitSeparatorLine;
/**
*
* Class that allows to split in two file a received file that contains both a
* ResultSet and and a ResultSet.getMetaData():
*
* The ResultSet part id before the line that contain the separator produced by
* host: br.write(FileSplitSeparatorLine.RESULT_SET_DATABASE_METADATA_SEP +
* CR_LF);
*
* The MetaData part is after that line.
*
* @author Nicolas de Pomereu
*
*/
public class ResultSetFileSplitter {
/** Debug flag */
private static boolean DEBUG = FrameworkDebug
.isSet(ResultSetFileSplitter.class);
/** Universal and clean line separator */
public static String CR_LF = System.getProperty("line.separator");
/** The file that contains all: ResultSet + MetaData */
private File rsAndMetaDataFile = null;
/** The file that contains only the ResultSet */
private File rsFile = null;
/** The file that contains only the ResultSet */
private File metaDataFile = null;
/**
* Constructor
*
* @param rsAndMetaDataFile
* The file that contains all: ResultSet + MetaData
*
*/
public ResultSetFileSplitter(File rsAndMetaDataFile) throws SQLException {
if (rsAndMetaDataFile == null) {
String message = Tag.PRODUCT_PRODUCT_FAIL
+ "rsAndMetaDataFile can not be null!";
throw new SQLException(message, new IOException(message));
}
this.rsAndMetaDataFile = rsAndMetaDataFile;
splitFiles();
}
/**
* Split the file
*
* @throws IOException
*/
private void splitFiles() throws SQLException {
BufferedReader br = null;
BufferedWriter bwResultSet = null;
BufferedWriter bwMetaData = null;
try {
br = new BufferedReader(new FileReader(rsAndMetaDataFile));
rsFile = new File(rsAndMetaDataFile.toString() + ".1");
metaDataFile = new File(rsAndMetaDataFile.toString() + ".2");
bwResultSet = new BufferedWriter(new FileWriter(rsFile));
bwMetaData = new BufferedWriter(new FileWriter(metaDataFile));
boolean separatorReached = false;
String line = null;
while ((line = br.readLine()) != null) {
line.trim();
if (line.equals(FileSplitSeparatorLine.RESULT_SET_GET_METADATA_SEP)) {
separatorReached = true;
continue;
}
if (!separatorReached) {
bwResultSet.write(line + CR_LF);
} else {
bwMetaData.write(line + CR_LF);
}
}
} catch (Exception e) {
throw new SQLException(e);
} finally {
IOUtils.closeQuietly(br);
IOUtils.closeQuietly(bwResultSet);
IOUtils.closeQuietly(bwMetaData);
if (!DEBUG && !KeepTempFilePolicyParms.KEEP_TEMP_FILE) {
this.rsAndMetaDataFile.delete();
}
}
}
/**
* Returns the ResultSet File
*
* @return the ResultSet File
*/
public File getResultSetFile() {
return rsFile;
}
/**
* Returns the ResultSet File
*
* @return the ResultSet File
*/
public File getMetaDataFile() {
return metaDataFile;
}
/**
* Displays the given message if DEBUG is set.
*
* @param s
* the debug message
*/
@SuppressWarnings("unused")
private void debug(String s) {
if (DEBUG) {
ClientLogger.getLogger().log(Level.WARNING, s);
}
}
}
/**
*
*/
© 2015 - 2025 Weber Informatics LLC | Privacy Policy