📄 csvscrollablereader.java
字号:
/*
* CsvJdbc - a JDBC driver for CSV files
* Copyright (C) 2001 Jonathan Ackerman
* This library 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.
* This library 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.relique.jdbc.csv;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
/**
*This class Class provides facility to navigate on the Result Set.
*
* @author Chetan Gupta
* @version $Id: CSVScrollableReader.java,v 1.1 2004/04/09 11:17:13 gupta_chetan Exp $
*/
public class CSVScrollableReader extends CSVReaderAdapter {
//---------------------------------------------------------------------
// Traversal/Positioning
//---------------------------------------------------------------------
private static final int FIRST_RECORD = 0;
private ArrayList alRecordNos = null;
private int recordMappedToNo = FIRST_RECORD - 1;
private ArrayList alRecordsArray = null;
private int iRecordNo = 0;
private int iRecordArrayNo = 0;
/**
*Constructor for the CsvReader object
*
* @param fileName Description of Parameter
* @exception Exception Description of Exception
* @since
*/
public CSVScrollableReader(String fileName) throws Exception {
this(fileName, ',', false, null);
}
/**
* Can be put in adpater apart from the last line
*
* Insert the method's description here.
*
* Creation date: (6-11-2001 15:02:42)
*
* @param fileName java.lang.String
* @param separator char
* @param suppressHeaders boolean
* @exception java.lang.Exception The exception description.
* @since
*/
public CSVScrollableReader(
String fileName, char separator, boolean suppressHeaders, String charset)
throws java.lang.Exception {
BufferedReader input = null;
String buf = null;
this.separator = separator;
this.suppressHeaders = suppressHeaders;
this.fileName = fileName;
this.charset = charset;
if (charset != null) {
input =
new BufferedReader(
new InputStreamReader(new FileInputStream(fileName), charset));
} else {
input =
new BufferedReader(
new InputStreamReader(new FileInputStream(fileName)));
}
// input = new BufferedReader(new FileReader(fileName));
if (this.suppressHeaders) {
// No column names available. Read first data line and determine number of colums.
buf = input.readLine();
String[] data = parseCsvLine(buf);
columnNames = new String[data.length];
for (int i = 0; i < data.length; i++) {
columnNames[i] = "COLUMN" + String.valueOf(i + 1);
}
data = null;
// throw away.
} else {
String headerLine = input.readLine();
columnNames = parseCsvLine(headerLine);
}
loopAndFetchData(input, buf);
fillRecordNoMapping();
iRecordNo = FIRST_RECORD - 1;
iRecordArrayNo = FIRST_RECORD - 1;
}
/*
* Can be improved a lot,
* a) lazy loading
* b) integrate it with loopAndFetchData
*/
private void fillRecordNoMapping() throws SQLException {
alRecordNos = new ArrayList();
iRecordNo = FIRST_RECORD - 1;
iRecordArrayNo = FIRST_RECORD - 1;
//Map the records in two step
//1) Add the first record
if (alRecordsArray.size() > 1) {
alRecordNos.add(0, new Integer(0));
}
//2) After next the record No will be the last of previous record and 1 less then next record so, we only add the last record
//Add rest of the records after checking that its not the last records
for (
int i = 1;
readDataFirstTime() && ((alRecordsArray.size() - 1) > iRecordArrayNo);
i++) {
alRecordNos.add(i, new Integer(iRecordArrayNo + 1));
}
}
private boolean readDataFirstTime() throws SQLException {
columns = new String[columnNames.length];
String dataLine = null;
iRecordArrayNo++;
if (
(iRecordArrayNo < FIRST_RECORD)
|| (iRecordArrayNo >= alRecordsArray.size())) {
return false;
}
dataLine = (String) alRecordsArray.get(iRecordArrayNo);
columns = parseCsvLine(dataLine);
return true;
}
/**
*Description of the Method
*
* @return Description of the Returned Value
* @exception SQLException Description of Exception
* @since
*/
private boolean loopAndFetchData(BufferedReader input, String buf)
throws SQLException {
alRecordsArray = new ArrayList();
while (true) {
columns = new String[columnNames.length];
String dataLine = null;
try {
if (suppressHeaders && (buf != null)) {
// The buffer is not empty yet, so use this first.
dataLine = buf;
buf = null;
} else {
// read new line of data from input.
dataLine = input.readLine();
}
if (dataLine == null) {
input.close();
return false;
}
} catch (IOException e) {
throw new SQLException(e.toString());
}
alRecordsArray.add(dataLine);
}
}
// This code updated with code by Stuart Mottram to handle line breaks in fields
// see bug #492063
protected String[] parseCsvLine(String line) throws SQLException {
Vector values = new Vector();
boolean inQuotedString = false;
String value = "";
String orgLine = line;
int currentPos = 0;
int fullLine = 0;
while (fullLine == 0) {
currentPos = 0;
line += separator;
while (currentPos < line.length()) {
// System.out.println("=currentPos == " + currentPos);
char currentChar = line.charAt(currentPos);
if ((value.length() == 0) && (currentChar == '"') && !inQuotedString) {
// System.out.println("\n\n\n == ");
// System.out.println("-currentChar == " + currentChar);
// System.out.println("-currentPos == " + currentPos);
// System.out.println("-line == " + line);
// System.out.println("-inQuotedString == " + inQuotedString);
currentPos++;
inQuotedString = true;
continue;
}
if (currentChar == '"') {
char nextChar = line.charAt(currentPos + 1);
if (nextChar == '"') {
value += currentChar;
currentPos++;
} else {
if (!inQuotedString) {
throw new SQLException(
"Unexpected '\"' in position " + currentPos + ". Line="
+ orgLine);
}
if (inQuotedString && (nextChar != separator)) {
/* System.out.println("\n\n\n == ");
System.out.println("currentChar == " + currentChar);
System.out.println("currentPos == " + currentPos);
System.out.println("line == " + line);
System.out.println("nextChar == " + nextChar);
System.out.println("inQuotedString == " + inQuotedString);
*/
throw new SQLException(
"Expecting " + separator + " in position " + (currentPos + 1)
+ ". Line=" + orgLine);
}
values.add(value);
value = "";
inQuotedString = false;
currentPos++;
}
} else {
if (currentChar == separator) {
if (inQuotedString) {
value += currentChar;
} else {
values.add(value);
value = "";
}
} else {
value += currentChar;
}
}
currentPos++;
}
if (inQuotedString) {
// Remove extra , added at start
value = value.substring(0, value.length() - 1);
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -