@Override public String readLine() { try { // Continually poll until we get a result, unless the underlying reader is finished. for (; ; ) { checkAndThrowIfWorkerException(); final String pollResult = this.lineQueue.poll(100, TimeUnit.MILLISECONDS); // Not ideal for small files. if (pollResult == null) { if (eofReached) { checkAndThrowIfWorkerException(); return lineQueue.poll(); // If there is nothing left, returns null as expected. Otherwise, grabs next element. } } else { return pollResult; } } } catch (final InterruptedException e) { throw new TribbleException("Line polling interrupted.", e); } }
/** * Convenience factory for composing a LineReader from an InputStream. */ public static LineReader fromBufferedStream(final InputStream bufferedStream, final LineReaderOption option) { final InputStreamReader bufferedInputStreamReader = new InputStreamReader(bufferedStream); switch (option) { case ASYNCHRONOUS: return new AsynchronousLineReader(bufferedInputStreamReader); case SYNCHRONOUS: return new LineReader() { final LongLineBufferedReader reader = new LongLineBufferedReader(bufferedInputStreamReader); @Override public String readLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } @Override public void close() { CloserUtil.close(reader); } }; default: throw new TribbleException(String.format("Unrecognized LineReaderUtil option: %s.", option)); } }