From f769cc54d0ba134fb353ab4a32d682bf7d8b70ec Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Fri, 11 May 2012 02:31:45 +0000 Subject: [PATCH] Don't search for newline if already at beginning of file, fix bug causing size=0 on gz files. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1337008 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/util/LineFileDocs.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LineFileDocs.java b/lucene/test-framework/src/java/org/apache/lucene/util/LineFileDocs.java index 18d738c185c..cb0869f0f5f 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/LineFileDocs.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/LineFileDocs.java @@ -84,17 +84,17 @@ public class LineFileDocs implements Closeable { private synchronized void open(Random random) throws IOException { InputStream is = getClass().getResourceAsStream(path); boolean needSkip = true; - long size = 0L; + long size = 0L, seekTo = 0L; if (is == null) { // if its not in classpath, we load it as absolute filesystem path (e.g. Hudson's home dir) File file = new File(path); + size = file.length(); if (path.endsWith(".gz")) { // if it is a gzip file, we need to use InputStream and slowly skipTo: is = new FileInputStream(file); } else { // optimized seek using RandomAccessFile: - size = file.length(); - final long seekTo = randomSeekPos(random, size); + seekTo = randomSeekPos(random, size); final FileChannel channel = new RandomAccessFile(path, "r").getChannel(); if (LuceneTestCase.VERBOSE) { System.out.println("TEST: LineFileDocs: file seek to fp=" + seekTo + " on open"); @@ -117,23 +117,30 @@ public class LineFileDocs implements Closeable { // If we only have an InputStream, we need to seek now, // but this seek is a scan, so very inefficient!!! if (needSkip) { - final long skipTo = randomSeekPos(random, size); + seekTo = randomSeekPos(random, size); if (LuceneTestCase.VERBOSE) { - System.out.println("TEST: LineFileDocs: stream skip to fp=" + skipTo + " on open"); + System.out.println("TEST: LineFileDocs: stream skip to fp=" + seekTo + " on open"); } - is.skip(skipTo); + is.skip(seekTo); } - int b; - do { - b = is.read(); - } while (b >= 0 && b != 13 && b != 10); - CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder() + // if we seeked somewhere, read until newline char + if (seekTo > 0L) { + int b; + do { + b = is.read(); + } while (b >= 0 && b != 13 && b != 10); + } + + CharsetDecoder decoder = IOUtils.CHARSET_UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); reader = new BufferedReader(new InputStreamReader(is, decoder), BUFFER_SIZE); - // read one more line, to make sure we are not inside a Windows linebreak (\r\n): - reader.readLine(); + + if (seekTo > 0L) { + // read one more line, to make sure we are not inside a Windows linebreak (\r\n): + reader.readLine(); + } } public synchronized void reset(Random random) throws IOException {