use of java.io.Writer in project guava by google.
the class CharSink method writeFrom.
/**
* 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);
// https://code.google.com/p/guava-libraries/issues/detail?id=1330
out.flush();
return written;
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
use of java.io.Writer in project guava by google.
the class CharSource method copyTo.
/**
* 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();
}
}
use of java.io.Writer in project guava by google.
the class AppendableWriterTest method testAppendMethods.
public void testAppendMethods() throws IOException {
StringBuilder builder = new StringBuilder();
Writer writer = new AppendableWriter(builder);
writer.append("Hello,");
writer.append(' ');
writer.append("The World Wide Web", 4, 9);
writer.append("!");
assertEquals("Hello, World!", builder.toString());
}
use of java.io.Writer in project guava by google.
the class CharSinkTest method testOpenBufferedStream.
public void testOpenBufferedStream() throws IOException {
Writer writer = sink.openBufferedStream();
assertTrue(sink.wasStreamOpened());
assertFalse(sink.wasStreamClosed());
writer.write(STRING);
writer.close();
assertTrue(sink.wasStreamClosed());
assertEquals(STRING, sink.getString());
}
use of java.io.Writer in project guava by google.
the class AppendableWriterTest method testCloseFlush.
public void testCloseFlush() throws IOException {
SpyAppendable spy = new SpyAppendable();
Writer writer = new AppendableWriter(spy);
writer.write("Hello");
assertFalse(spy.flushed);
assertFalse(spy.closed);
writer.flush();
assertTrue(spy.flushed);
assertFalse(spy.closed);
writer.close();
assertTrue(spy.flushed);
assertTrue(spy.closed);
}
Aggregations