/** * Returns an Iterator for the lines in a <code>Reader</code>. * <p> * <code>LineIterator</code> holds a reference to the open * <code>Reader</code> specified here. When you have finished with the * iterator you should close the reader to free internal resources. * This can be done by closing the reader directly, or by calling * {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}. * <p> * The recommended usage pattern is: * <pre> * try { * LineIterator it = IOUtils.lineIterator(reader); * while (it.hasNext()) { * String line = it.nextLine(); * /// do something with line * } * } finally { * IOUtils.closeQuietly(reader); * } * </pre> * * @param reader the <code>Reader</code> to read from, not null * @return an Iterator of the lines in the reader, never null * @throws IllegalArgumentException if the reader is null * @since 1.2 */ public static LineIterator lineIterator(final Reader reader) { return new LineIterator(reader); }
/** * Return an Iterator for the lines in a <code>Reader</code>. * <p> * <code>LineIterator</code> holds a reference to the open * <code>Reader</code> specified here. When you have finished with the * iterator you should close the reader to free internal resources. * This can be done by closing the reader directly, or by calling * {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}. * <p> * The recommended usage pattern is: * <pre> * try { * LineIterator it = IOUtils.lineIterator(reader); * while (it.hasNext()) { * String line = it.nextLine(); * /// do something with line * } * } finally { * IOUtils.closeQuietly(reader); * } * </pre> * * @param reader the <code>Reader</code> to read from, not null * @return an Iterator of the lines in the reader, never null * @throws IllegalArgumentException if the reader is null * @since Commons IO 1.2 */ public static LineIterator lineIterator(Reader reader) { return new LineIterator(reader); }
return new LineIterator(new InputStreamReader(input, Charsets.toCharset(encoding)));
reader = new InputStreamReader(input, encoding); return new LineIterator(reader);
public static void main(String[] args) throws Exception { if(args.length != 3) Utils.croak("USAGE: java StringSorter inputfile internal_sort_size num_threads"); String input = args[0]; int internalSortSize = Integer.parseInt(args[1]); int numThreads = Integer.parseInt(args[2]); ExternalSorter<String> sorter = new ExternalSorter<String>(new StringSerializer(), internalSortSize, numThreads); @SuppressWarnings("unchecked") Iterator<String> it = new LineIterator(new BufferedReader(new FileReader(input), 10 * 1024 * 1024)); String separator = Utils.NEWLINE; BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out), 10 * 1024 * 1024); for(String line: sorter.sorted(it)) { writer.write(line); writer.write(separator); } }
@Test public void testConstructor() throws Exception { try { new LineIterator(null); fail(); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testNextWithException() throws Exception { final Reader reader = new BufferedReader(new StringReader("")) { @Override public String readLine() throws IOException { throw new IOException("hasNext"); } }; try { new LineIterator(reader).hasNext(); fail("Expected IllegalStateException"); } catch (final IllegalStateException e) { // expected } }
@Test public void testFirehose() throws Exception { final List<LineIterator> lineIterators = inputs.stream() .map(s -> new LineIterator(new StringReader(s))) .collect(Collectors.toList()); try (final FileIteratingFirehose firehose = new FileIteratingFirehose(lineIterators.iterator(), parser)) { final List<String> results = new ArrayList<>(); while (firehose.hasMore()) { final InputRow inputRow = firehose.nextRow(); if (inputRow == null) { results.add(null); } else { results.add(Joiner.on("|").join(inputRow.getDimension("x"))); } } Assert.assertEquals(expectedResults, results); } }
@Test(expected = RuntimeException.class) public void testClose() throws IOException { final LineIterator lineIterator = new LineIterator(new Reader() { @Override public int read(char[] cbuf, int off, int len) { System.arraycopy(LINE_CHARS, 0, cbuf, 0, LINE_CHARS.length); return LINE_CHARS.length; } @Override public void close() { throw new RuntimeException("close test for FileIteratingFirehose"); } }); final TestCloseable closeable = new TestCloseable(); final FileIteratingFirehose firehose = new FileIteratingFirehose( ImmutableList.of(lineIterator).iterator(), parser, closeable ); firehose.hasMore(); // initialize lineIterator firehose.close(); Assert.assertTrue(closeable.closed); }
public void write(final InputStream in, final OutputStream out) throws IOException { // Get an Iterator to the text of the InputStream. final BufferedReader reader = new BufferedReader(new InputStreamReader(in)); final Iterator iterator = new LineIterator(reader); write(iterator, out); }
public IteratorImpl() { try { this.iterImpl = new LineIterator(WpIOUtils.openBufferedReader(file)); } catch (IOException e) { throw new IllegalArgumentException(e); } }
public IteratorImpl() { try { this.iterImpl = new LineIterator(WpIOUtils.openBufferedReader(file)); } catch (IOException e) { throw new IllegalArgumentException(e); } }
public LineIteratorCreator(InputStream stream) { Assert.notNull(stream, "stream cannot be null"); lineIterator = new LineIterator(new InputStreamReader(stream)); }
private static Map<String, Long> loadMappingData(File mappingFile) throws FileNotFoundException { Map<String, Long> mapping = new HashMap<String, Long>(); log.info("Reading mapping data from {}", mappingFile.getAbsolutePath()); LineIterator itr = new LineIterator(Files.newReader(mappingFile, Charsets.UTF_8)); try { while (itr.hasNext()) { String line = itr.nextLine(); int indexOfBar = line.indexOf(SEPARATOR); checkState(indexOfBar > 0, "Malformed entry found [%s]", line); String length = line.substring(0, indexOfBar); String id = line.substring(indexOfBar + 1); mapping.put(id.trim(), Long.valueOf(length)); } log.info("Total {} mapping entries found", mapping.size()); } finally { LineIterator.closeQuietly(itr); } return mapping; }
private static Map<String, Long> loadMappingData(File mappingFile) throws FileNotFoundException { Map<String, Long> mapping = new HashMap<String, Long>(); log.info("Reading mapping data from {}", mappingFile.getAbsolutePath()); LineIterator itr = new LineIterator(Files.newReader(mappingFile, Charsets.UTF_8)); try { while (itr.hasNext()) { String line = itr.nextLine(); int indexOfBar = line.indexOf(SEPARATOR); checkState(indexOfBar > 0, "Malformed entry found [%s]", line); String length = line.substring(0, indexOfBar); String id = line.substring(indexOfBar + 1); mapping.put(id.trim(), Long.valueOf(length)); } log.info("Total {} mapping entries found", mapping.size()); } finally { LineIterator.closeQuietly(itr); } return mapping; }
private List<String> loadPatterns(File file, String type) { try (Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { LineIterator it = new LineIterator(r); List<String> patterns = new ArrayList<>(); while (it.hasNext()) { String pattern = it.next(); if (!pattern.startsWith("#")) { addPattern(type, patterns, pattern); } } return patterns; } catch (Exception ex) { throw new DuraCloudRuntimeException(ex); } }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals("app")) { appList.add(currentApp); currentApp = null; } if (currentApp == null) { return; } if (qName.equals("entities")) { LineIterator it = new LineIterator(new StringReader(entitiesBuf.toString())); while (it.hasNext()) { String entity = it.next().trim(); if (!entity.isEmpty()) { currentApp.getEntities().add(entity); } } // clear buffer without re-allocating it entitiesBuf.setLength(0); } }
private List<String> getExampleBenchmarkPaths(HttpServletRequest request) throws IOException { // TODO how to enumerate bundle resources? final String list = getBenchmarkText("/LIST.txt"); final LineIterator it = new LineIterator(new StringReader(list)); final List<String> result = new LinkedList<String>(); while(it.hasNext()) { result.add(getExampleBenchmarkPath(request, it.nextLine())); } return result; }
private static LineIterator lineItr(String seq) { Iterable<String> seqItr = Splitter.on(',').trimResults().split(seq); String lines = Joiner.on(StandardSystemProperty.LINE_SEPARATOR.value()).join(seqItr); return new LineIterator(new StringReader(lines)); }
@Override public Metrics parse(final HttpResponse response, final HttpEntity entity, final InputStreamReader reader) { final Metrics result = new Metrics(); final LineIterator lines = new LineIterator(reader); try { final long timestamp = System.currentTimeMillis(); while (lines.hasNext()) { parseLine(result, timestamp, lines.next()); } } finally { lines.close(); } return result; }