/** * Opens a new {@link BufferedReader} for reading from this source. This method returns a new, * independent reader each time it is called. * * <p>The caller is responsible for ensuring that the returned reader is closed. * * @throws IOException if an I/O error occurs while of opening the reader */ public BufferedReader openBufferedStream() throws IOException { Reader reader = openStream(); return (reader instanceof BufferedReader) ? (BufferedReader) reader : new BufferedReader(reader); }
/** Closes the current reader and opens the next one, if any. */ private void advance() throws IOException { close(); if (it.hasNext()) { current = it.next().openStream(); } }
@Override public InputStream openStream() throws IOException { return decodingStream(encodedSource.openStream()); } };
@Override public InputStream openStream() throws IOException { return new ReaderInputStream(CharSource.this.openStream(), charset, 8192); }
/** * Reads the contents of this source as a string. * * @throws IOException if an I/O error occurs while reading from this source */ public String read() throws IOException { Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.toString(reader); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** Closes the current reader and opens the next one, if any. */ private void advance() throws IOException { close(); if (it.hasNext()) { current = it.next().openStream(); } }
@Override public Reader openStream() throws IOException { if (counter[0]++ != 0) { throw new IllegalStateException("More than one source open"); } return new FilterReader(source.openStream()) { @Override public void close() throws IOException { super.close(); counter[0]--; } }; } };
@Override public InputStream openStream() throws IOException { return decodingStream(encodedSource.openStream()); } };
@Override public InputStream openStream() throws IOException { return new ReaderInputStream(CharSource.this.openStream(), charset, 8192); }
/** * Returns whether the source has zero chars. The default implementation first checks {@link * #lengthIfKnown}, returning true if it's known to be zero and false if it's known to be * non-zero. If the length is not known, it falls back to opening a stream and checking for EOF. * * <p>Note that, in cases where {@code lengthIfKnown} returns zero, it is <i>possible</i> that * chars are actually available for reading. This means that a source may return {@code true} from * {@code isEmpty()} despite having readable content. * * @throws IOException if an I/O error occurs * @since 15.0 */ public boolean isEmpty() throws IOException { Optional<Long> lengthIfKnown = lengthIfKnown(); if (lengthIfKnown.isPresent()) { return lengthIfKnown.get() == 0L; } Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return reader.read() == -1; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Appends the contents of this source to the given {@link Appendable} (such as a {@link Writer}). * Does not close {@code appendable} if it is {@code Closeable}. * * @return the number of characters copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * appendable} */ @CanIgnoreReturnValue public long copyTo(Appendable appendable) throws IOException { checkNotNull(appendable); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.copy(reader, appendable); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Reads lines of text from this source, processing each line as it is read using the given {@link * LineProcessor processor}. Stops when all lines have been processed or the processor returns * {@code false} and returns the result produced by the processor. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code * processor} throws an {@code IOException} * @since 16.0 */ @Beta @CanIgnoreReturnValue // some processors won't return a useful result public <T> T readLines(LineProcessor<T> processor) throws IOException { checkNotNull(processor); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.readLines(reader, processor); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Copies the contents of this source to the given sink. * * @return the number of characters copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * sink} */ @CanIgnoreReturnValue public long copyTo(CharSink sink) throws IOException { checkNotNull(sink); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); Writer writer = closer.register(sink.openStream()); return CharStreams.copy(reader, writer); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void testOpenStream() throws IOException { Reader reader = source.openStream(); StringWriter writer = new StringWriter(); char[] buf = new char[64]; int read; while ((read = reader.read(buf)) != -1) { writer.write(buf, 0, read); } reader.close(); writer.close(); assertExpectedString(writer.toString()); }
public void testConcat_infiniteIterable() throws IOException { CharSource source = CharSource.wrap("abcd"); Iterable<CharSource> cycle = Iterables.cycle(ImmutableList.of(source)); CharSource concatenated = CharSource.concat(cycle); String expected = "abcdabcd"; // read the first 8 chars manually, since there's no equivalent to ByteSource.slice // TODO(cgdecker): Add CharSource.slice? StringBuilder builder = new StringBuilder(); Reader reader = concatenated.openStream(); // no need to worry about closing for (int i = 0; i < 8; i++) { builder.append((char) reader.read()); } assertEquals(expected, builder.toString()); }
public void testSkipZero() throws Exception { CharSource source = newCharSource("a"); Iterable<CharSource> list = ImmutableList.of(source, source); Reader joinedReader = CharSource.concat(list).openStream(); assertEquals(0, joinedReader.skip(0)); assertEquals('a', joinedReader.read()); } }
public void testSimple() throws Exception { String testString = "abcdefgh"; CharSource source = newCharSource(testString); Reader joinedReader = CharSource.concat(source, source).openStream(); String expectedString = testString + testString; assertEquals(expectedString, CharStreams.toString(joinedReader)); }
public void testReady() throws Exception { CharSource source = newCharSource("a"); Iterable<? extends CharSource> list = ImmutableList.of(source, source); Reader joinedReader = CharSource.concat(list).openStream(); assertTrue(joinedReader.ready()); assertEquals('a', joinedReader.read()); assertEquals('a', joinedReader.read()); assertEquals(-1, joinedReader.read()); assertFalse(joinedReader.ready()); }
public void testOnlyOneOpen() throws Exception { String testString = "abcdefgh"; final CharSource source = newCharSource(testString); final int[] counter = new int[1]; CharSource reader = new CharSource() { @Override public Reader openStream() throws IOException { if (counter[0]++ != 0) { throw new IllegalStateException("More than one source open"); } return new FilterReader(source.openStream()) { @Override public void close() throws IOException { super.close(); counter[0]--; } }; } }; Reader joinedReader = CharSource.concat(reader, reader, reader).openStream(); String result = CharStreams.toString(joinedReader); assertEquals(testString.length() * 3, result.length()); }
public void testSkip() throws Exception { String begin = "abcde"; String end = "fghij"; Reader joinedReader = CharSource.concat(newCharSource(begin), newCharSource(end)).openStream(); String expected = begin + end; assertEquals(expected.charAt(0), joinedReader.read()); CharStreams.skipFully(joinedReader, 1); assertEquals(expected.charAt(2), joinedReader.read()); CharStreams.skipFully(joinedReader, 4); assertEquals(expected.charAt(7), joinedReader.read()); CharStreams.skipFully(joinedReader, 1); assertEquals(expected.charAt(9), joinedReader.read()); assertEquals(-1, joinedReader.read()); }