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 / TextFileAsList.java @ 47638

History | View | Annotate | Download (5.41 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.BufferedReader;
9
import java.io.Closeable;
10
import java.io.File;
11
import java.io.IOException;
12
import java.nio.charset.Charset;
13
import java.util.AbstractList;
14
import java.util.Date;
15
import java.util.List;
16
import org.apache.commons.io.FilenameUtils;
17
import org.apache.commons.io.IOUtils;
18
import org.gvsig.fmap.dal.store.simplereader.virtualrows.RandomAccessFileIndex;
19
import org.gvsig.fmap.dal.store.simplereader.virtualrows.RandomAccessFileReader;
20
import static org.gvsig.fmap.dal.store.simplereader.virtualrows.RandomAccessFileReader.FILTER_NONE;
21
import org.gvsig.tools.ToolsLocator;
22
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
23
import org.gvsig.tools.observer.Observable;
24
import org.gvsig.tools.task.SimpleTaskStatus;
25
import org.gvsig.tools.task.TaskStatus;
26
import org.gvsig.tools.task.TaskStatusManager;
27
import org.gvsig.tools.util.GetItemWithSize64;
28

    
29
/**
30
 *
31
 * @author gvSIG Team
32
 */
33
public class TextFileAsList 
34
        extends AbstractList<String> 
35
        implements Closeable, GetItemWithSize64<String>
36
    {
37

    
38
    private final RandomAccessFileReader reader;
39
    private final RandomAccessFileIndex index;
40
    
41
    public TextFileAsList(File text, File index, Charset charset) throws IOException {
42
        this.reader = new RandomAccessFileReader(text, charset);
43
        this.index = new RandomAccessFileIndex(index);
44
     }
45

    
46
    public TextFileAsList(RandomAccessFileReader reader, RandomAccessFileIndex index) throws IOException {
47
        this.reader = reader;
48
        this.index = index;
49
    }
50

    
51
    @Override
52
    public void close() {
53
        IOUtils.closeQuietly(this.reader);
54
        IOUtils.closeQuietly(this.index);
55
    }
56

    
57
    @Override
58
    public String get(int index) {
59
        try {
60
            long pos = this.index.get(index);
61
            this.reader.seek(pos);
62
            return nextLine();
63
        } catch (IOException ex) {
64
            throw new RuntimeException("Can't access to " + index + " element.", ex);
65
        }
66
    }
67

    
68
    @Override
69
    public int size() {
70
        return this.index.size();
71
    }
72

    
73
    @Override
74
    public void clear() {
75
        throw new UnsupportedOperationException();
76
    }
77

    
78
    @Override
79
    public long size64() {
80
        return this.index.size64();
81
    }
82

    
83
    @Override
84
    public String get64(long position) {
85
        try {
86
            long pos = this.index.get64(position);
87
            this.reader.seek(pos);
88
            return nextLine();
89
        } catch (IOException ex) {
90
            throw new RuntimeException("Can't access to " + position + " element.", ex);
91
        }
92
    }
93
    
94
//    private String nextLine() throws IOException {
95
//        String line = this.reader.readLine();
96
//        return line;
97
//    }
98

    
99
    private String nextLine() throws IOException {
100
        BufferedReader br = new BufferedReader(this.reader);
101
        String line = br.readLine();
102
        return line;
103
    }
104

    
105
    public static void main(String[] args) throws Exception {
106
        new DefaultLibrariesInitializer().fullInitialize();
107
        
108
        String fname;
109
//        fname = "/home/jjdelcerro/Descargas/test/origen_coordenadas.csv";
110
        fname = "/home/jjdelcerro/Descargas/test/esp_poblaciones.csv";
111
//        fname = "/home/jjdelcerro/Descargas/test/esp_provincias.csv";
112

    
113
        File data_file = new File(fname);
114
        File index_file = new File(FilenameUtils.removeExtension(data_file.getAbsolutePath()) + ".idx");
115

    
116
        final TaskStatusManager taskStatusManager = ToolsLocator.getTaskStatusManager();
117
        taskStatusManager.addObserver((Observable observable, Object notification) -> {
118
            TaskStatus status = taskStatusManager.getRunningTaskStatusMostRecent();
119
//            System.out.print("\033[?25l\r");
120
//            if( status!=null && status.isRunning() ) {
121
//                System.out.print("\033[?25l\r");
122
//                System.out.print(status.getTitle()+ " - " + status.getLabel());
123
//                System.out.print("\033[K\033[?12l\033[?25h");
124
//            }
125
//            System.out.flush();
126
        });
127
        SimpleTaskStatus status = taskStatusManager.createDefaultSimpleTaskStatus(data_file.getName());
128
        status.add();
129

    
130
        RandomAccessFileReader reader = new RandomAccessFileReader(data_file , "UTF-8");
131
        RandomAccessFileIndex index_lines = reader.createOrOpenIndexOfLines(index_file, FILTER_NONE, null);
132

    
133
        List<String> text = new TextFileAsList(reader,index_lines);
134
        System.out.println("Lines: " + text.size());
135
        int n = 0;
136
        for (String line : text) {
137
            if( n<100 ) {
138
                System.out.println(String.format("%6d: %s", n, line));
139
            } else if( n==100 ) {
140
                System.out.println("More records...");
141
            }
142
            n++;
143
        }
144
        System.out.println("-----------------");
145
        long t1 = new Date().getTime();
146
        for (int i = text.size()-1; i >= 0 ; i--) {
147
            String line = text.get(i);
148
            if( i<100 ) {
149
                System.out.println(String.format("%6d: %s", i, line));
150
            } else if( i==100 ) {
151
                System.out.println("More records...");
152
            }
153

    
154
        }
155
        long t2 = new Date().getTime();
156
        System.out.println("Time: "+ (t2-t1) + " ms");
157
        IOUtils.closeQuietly((Closeable) text);
158
    }
159

    
160
}