Revision 47652 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/simplereader/virtualrows/RandomAccessFileReader.java

View differences:

RandomAccessFileReader.java
21 21
import org.apache.commons.io.FilenameUtils;
22 22
import org.apache.commons.io.IOUtils;
23 23
import org.apache.commons.lang3.StringUtils;
24
import org.apache.commons.lang3.mutable.MutableInt;
24 25
import org.gvsig.tools.ToolsLocator;
25 26
import org.gvsig.tools.i18n.I18nManager;
26 27
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
......
163 164
    }
164 165

  
165 166
    public long countLines(Predicate<String> filter, SimpleTaskStatus status) throws IOException {
167
        return countLines(filter, new MutableInt(), status);
168
    }
169
    
170
    public long countLines(Predicate<String> filter, MutableInt maxLineLen, SimpleTaskStatus status) throws IOException {
166 171
        if (raf.length() == 0) {
167 172
            return 0;
168 173
        }
......
173 178
            status.message(i18n.getTranslation("_Calculating_number_of_lines"));
174 179
            status.setIndeterminate();
175 180
        }
181
        maxLineLen.setValue(0);
176 182
        BufferedReader breader = new BufferedReader(this, MAX_BUFFER_FOR_LINE);
177 183
        try {
178 184
            String line;
......
191 197
                    continue;
192 198
                }
193 199
                count++;
200
                int l = line.getBytes(this.getCharset()).length;
201
                if(l>maxLineLen.getValue()){
202
                    maxLineLen.setValue(l);
203
                }
194 204
            }
195 205
            if (status != null) {
196 206
                status.setCurValue(count);
......
257 267
    }
258 268
    
259 269
    public RandomAccessFileIndex createIndexOfLines(File index, boolean safe, Predicate<String> filter, SimpleTaskStatus status, Function<BufferedReader,Integer> numberOfLines) throws IOException {
260
        long countLines = this.countLines(filter, status);
270
        MutableInt maxLineLen = new MutableInt();
271
        long countLines = this.countLines(filter, maxLineLen, status);
261 272
        if (countLines < 1) {
262 273
            return null;
263 274
        }
275
        int maxBufferForLine = maxLineLen.getValue()+1024;
264 276
        RandomAccessFileIndex line_idx = new RandomAccessFileIndex();
265 277
        line_idx.create(index, countLines);
266 278

  
......
307 319
            } else {
308 320
                // Use buffered reader, fast and unsafe calculate position.
309 321
                StringBuilder builder = new StringBuilder();
310
                MyBufferedReader breader = new MyBufferedReader(this, MAX_BUFFER_FOR_LINE);
322
                MyBufferedReader breader = new MyBufferedReader(this, maxBufferForLine);
311 323
                while (lineno < countLines) {
312 324
                    this.seek(position);
313 325
                    breader.clean();
314 326
                    if(numberOfLines == null){
315 327
                        line = breader.readLine();
316 328
                    } else {
317
                        breader.mark(MAX_BUFFER_FOR_LINE);
329
                        breader.mark(maxBufferForLine);
318 330
                        Integer nextLine = numberOfLines.apply(breader);
319 331
                        breader.reset();
320 332
                        builder.setLength(0);

Also available in: Unified diff