/** * 在Reader与Writer间复制内容 * * @see {@link CharStreams#copy} */ public static long copy(final Reader input, final Writer output) throws IOException { return CharStreams.copy(input, output); }
/** * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance. * Does not close the {@code Readable}. * * @param r the object to read from * @return a {@link StringBuilder} containing all the characters * @throws IOException if an I/O error occurs */ private static StringBuilder toStringBuilder(Readable r) throws IOException { StringBuilder sb = new StringBuilder(); if (r instanceof Reader) { copyReaderToBuilder((Reader) r, sb); } else { copy(r, sb); } return sb; }
/** * 在Reader与Writer间复制内容 * * @see {@link CharStreams#copy} */ public static long copy(final Reader input, final Writer output) throws IOException { return CharStreams.copy(input, output); }
/** * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance. * Does not close the {@code Readable}. * * @param r the object to read from * @return a {@link StringBuilder} containing all the characters * @throws IOException if an I/O error occurs */ private static StringBuilder toStringBuilder(Readable r) throws IOException { StringBuilder sb = new StringBuilder(); if (r instanceof Reader) { copyReaderToBuilder((Reader) r, sb); } else { copy(r, sb); } return sb; }
@Override public void accept(Reader input) { try { hasData = true; CharStreams.copy(input, writer); } catch (IOException e) { throw new IllegalStateException(String.format("Can't read line hashes of file '%s'", componentKey), e); } }
/** * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance. * Does not close the {@code Readable}. * * @param r the object to read from * @return a {@link StringBuilder} containing all the characters * @throws IOException if an I/O error occurs */ private static StringBuilder toStringBuilder(Readable r) throws IOException { StringBuilder sb = new StringBuilder(); if (r instanceof Reader) { copyReaderToBuilder((Reader) r, sb); } else { copy(r, sb); } return sb; }
/** * 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(); } }
/** * Writes all the text from the given {@link Readable} (such as a {@link Reader}) to this sink. * Does not close {@code readable} if it is {@code Closeable}. * * @return the number of characters written * @throws IOException if an I/O error occurs while reading from {@code readable} or writing to * this sink */ @CanIgnoreReturnValue public long writeFrom(Readable readable) throws IOException { checkNotNull(readable); Closer closer = Closer.create(); try { Writer out = closer.register(openStream()); long written = CharStreams.copy(readable, out); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 return written; } 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(); } }
/** * Writes all the text from the given {@link Readable} (such as a {@link Reader}) to this sink. * Does not close {@code readable} if it is {@code Closeable}. * * @return the number of characters written * @throws IOException if an I/O error occurs while reading from {@code readable} or writing to * this sink */ @CanIgnoreReturnValue public long writeFrom(Readable readable) throws IOException { checkNotNull(readable); Closer closer = Closer.create(); try { Writer out = closer.register(openStream()); long written = CharStreams.copy(readable, out); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 return written; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } }
public String loadContent(String fromFile) { BufferedReader reader = null; StringBuilder sb = new StringBuilder(); try { InputStream is = getResource2(fromFile); reader = new BufferedReader(new InputStreamReader(is)); CharStreams.copy(reader, sb); } catch( IOException e ){ Log.err(e); } finally { Closeables.closeQuietly(reader); } return sb.toString(); } private final String _jarPath;
public void testCopy_toStringBuilder_fromReader() throws IOException { StringBuilder builder = new StringBuilder(); long copied = CharStreams.copy(new StringReader(ASCII), builder); assertEquals(ASCII, builder.toString()); assertEquals(ASCII.length(), copied); StringBuilder builder2 = new StringBuilder(); copied = CharStreams.copy(new StringReader(I18N), builder2); assertEquals(I18N, builder2.toString()); assertEquals(I18N.length(), copied); }
public void testCopy_toWriter_fromReader() throws IOException { StringWriter writer = new StringWriter(); long copied = CharStreams.copy(new StringReader(ASCII), writer); assertEquals(ASCII, writer.toString()); assertEquals(ASCII.length(), copied); StringWriter writer2 = new StringWriter(); copied = CharStreams.copy(new StringReader(I18N), writer2); assertEquals(I18N, writer2.toString()); assertEquals(I18N.length(), copied); }
public void testCopy_toWriter_fromReadable() throws IOException { StringWriter writer = new StringWriter(); long copied = CharStreams.copy(wrapAsGenericReadable(new StringReader(ASCII)), writer); assertEquals(ASCII, writer.toString()); assertEquals(ASCII.length(), copied); StringWriter writer2 = new StringWriter(); copied = CharStreams.copy(wrapAsGenericReadable(new StringReader(I18N)), writer2); assertEquals(I18N, writer2.toString()); assertEquals(I18N.length(), copied); }
public void testCopy_toStringBuilder_fromReadable() throws IOException { StringBuilder builder = new StringBuilder(); long copied = CharStreams.copy(wrapAsGenericReadable(new StringReader(ASCII)), builder); assertEquals(ASCII, builder.toString()); assertEquals(ASCII.length(), copied); StringBuilder builder2 = new StringBuilder(); copied = CharStreams.copy(wrapAsGenericReadable(new StringReader(I18N)), builder2); assertEquals(I18N, builder2.toString()); assertEquals(I18N.length(), copied); }
/** * 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(); } }
/** * Writes all the text from the given {@link Readable} (such as a {@link Reader}) to this sink. * Does not close {@code readable} if it is {@code Closeable}. * * @return the number of characters written * @throws IOException if an I/O error occurs while reading from {@code readable} or writing to * this sink */ @CanIgnoreReturnValue public long writeFrom(Readable readable) throws IOException { checkNotNull(readable); Closer closer = Closer.create(); try { Writer out = closer.register(openStream()); long written = CharStreams.copy(readable, out); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 return written; } 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(); } }
/** * Test for Guava issue 1061: http://code.google.com/p/guava-libraries/issues/detail?id=1061 * * <p>CharStreams.copy was failing to clear its CharBuffer after each read call, which effectively * reduced the available size of the buffer each time a call to read didn't fill up the available * space in the buffer completely. In general this is a performance problem since the buffer size * is permanently reduced, but with certain Reader implementations it could also cause the buffer * size to reach 0, causing an infinite loop. */ public void testCopyWithReaderThatDoesNotFillBuffer() throws IOException { // need a long enough string for the buffer to hit 0 remaining before the copy completes String string = Strings.repeat("0123456789", 100); StringBuilder b = new StringBuilder(); // the main assertion of this test is here... the copy will fail if the buffer size goes down // each time it is not filled completely long copied = CharStreams.copy(newNonBufferFillingReader(new StringReader(string)), b); assertEquals(string, b.toString()); assertEquals(string.length(), copied); }
public void testCopy() throws IOException { StringBuilder builder = new StringBuilder(); long copied = CharStreams.copy( wrapAsGenericReadable(new StringReader(ASCII)), wrapAsGenericAppendable(builder)); assertEquals(ASCII, builder.toString()); assertEquals(ASCII.length(), copied); StringBuilder builder2 = new StringBuilder(); copied = CharStreams.copy( wrapAsGenericReadable(new StringReader(I18N)), wrapAsGenericAppendable(builder2)); assertEquals(I18N, builder2.toString()); assertEquals(I18N.length(), copied); }