01: /*
02: *
03: * Created on Nov 23, 2004
04: */
05: package org.relique.jdbc.csv;
06:
07: import java.io.BufferedReader;
08: import java.io.FileInputStream;
09: import java.io.FileNotFoundException;
10: import java.io.IOException;
11: import java.io.InputStreamReader;
12: import java.io.RandomAccessFile;
13: import java.io.UnsupportedEncodingException;
14:
15: /**
16: *
17: * @author Zoran Milakovic
18: */
19: public class CsvRandomAccessFile extends RandomAccessFile {
20:
21: private String fileName = null;
22: private String charset = null;
23: private long position = 0;
24:
25: public CsvRandomAccessFile(String fileName, String charset)
26: throws FileNotFoundException, UnsupportedEncodingException {
27: super (fileName, "rw");
28: if (charset != null)
29: this .charset = charset;
30: this .fileName = fileName;
31: }
32:
33: public String readCsvLine() throws IOException {
34: String retVal = null;
35: if (Utils.isUTF16(this .charset)) {
36: FileInputStream is = new FileInputStream(fileName);
37: BufferedReader reader = new BufferedReader(
38: new InputStreamReader(is, charset));
39: is.getChannel().position(this .position);
40: retVal = reader.readLine();
41: if (retVal != null && retVal.equals("")) {
42: retVal = reader.readLine();
43: }
44: int length = 0;
45: if (retVal != null) {
46: length = retVal.getBytes(this .charset).length;
47: }
48: this .position += length;
49: super .seek(this .position);
50: reader.close();
51: is.close();
52: } else {
53: retVal = super .readLine();
54: }
55: return retVal;
56: }
57:
58: public void seek(long pos) throws IOException {
59: super.seek(pos);
60: this.position = pos;
61: }
62:
63: }
|