Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / virtualrows / AbstractCSVList.java @ 47638

History | View | Annotate | Download (2.48 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.csv.virtualrows;
7

    
8
import java.io.File;
9
import java.io.IOException;
10
import java.nio.charset.Charset;
11
import java.util.AbstractList;
12
import java.util.List;
13
import org.apache.commons.io.IOUtils;
14
import org.gvsig.fmap.dal.store.simplereader.virtualrows.RandomAccessFileIndex;
15
import org.gvsig.fmap.dal.store.simplereader.virtualrows.RandomAccessFileReader;
16

    
17
/**
18
 *
19
 * @author jjdelcerro
20
 */
21
public abstract class AbstractCSVList
22
        extends AbstractList<List<String>>
23
        implements CSVList {
24

    
25
    protected final RandomAccessFileReader reader;
26
    protected final RandomAccessFileIndex index;
27
    protected int skipLines;
28

    
29
    public AbstractCSVList(File text, File index, Charset charset) throws IOException {
30
        // FIXME: falla con campos multilinea
31
        // FIXME: falla si hay comentarios en el CSV
32
        this.reader = new RandomAccessFileReader(text, charset);
33
        this.index = new RandomAccessFileIndex(index);
34
        this.skipLines = 0;
35
    }
36

    
37
    public AbstractCSVList(RandomAccessFileReader reader, RandomAccessFileIndex index) throws IOException {
38
        this.reader = reader;
39
        this.index = index;
40
        this.skipLines = 0;
41
    }
42

    
43
    @Override
44
    public void close() {
45
        IOUtils.closeQuietly(this.reader);
46
        IOUtils.closeQuietly(this.index);
47
    }
48

    
49
    @Override
50
    public List<String> get(int index) {
51
        try {
52
            long pos = this.index.get(index+skipLines);
53
            this.reader.seek(pos);
54
            return nextRecord();
55
        } catch (IOException ex) {
56
            throw new RuntimeException("Can't access to " + index + " element.", ex);
57
        }
58
    }
59

    
60
    @Override
61
    public int size() {
62
        return this.index.size()-skipLines;
63
    }
64

    
65
    @Override
66
    public void clear() {
67
        throw new UnsupportedOperationException();
68
    }
69

    
70
    @Override
71
    public long size64() {
72
        return this.index.size64()-skipLines;
73
    }
74

    
75
    @Override
76
    public List<String> get64(long index) {
77
        try {
78
            long pos = this.index.get64(index+skipLines);
79
            this.reader.seek(pos);
80
            return nextRecord();
81
        } catch (IOException ex) {
82
            throw new RuntimeException("Can't access to " + index + " element.", ex);
83
        }
84
    }
85

    
86
    abstract protected List<String> nextRecord() throws IOException;
87

    
88
}