001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.io; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import java.io.BufferedWriter; 022import java.io.IOException; 023import java.io.Reader; 024import java.io.Writer; 025import java.nio.charset.Charset; 026 027/** 028 * A destination to which characters can be written, such as a text file. Unlike a {@link Writer}, a 029 * {@code CharSink} is not an open, stateful stream that can be written to and closed. Instead, it 030 * is an immutable <i>supplier</i> of {@code Writer} instances. 031 * 032 * <p>{@code CharSink} provides two kinds of methods: 033 * <ul> 034 * <li><b>Methods that return a writer:</b> These methods should return a <i>new</i>, 035 * independent instance each time they are called. The caller is responsible for ensuring that the 036 * returned writer is closed. 037 * <li><b>Convenience methods:</b> These are implementations of common operations that are 038 * typically implemented by opening a writer using one of the methods in the first category, 039 * doing something and finally closing the writer that was opened. 040 * </ul> 041 * 042 * <p>Any {@link ByteSink} may be viewed as a {@code CharSink} with a specific {@linkplain Charset 043 * character encoding} using {@link ByteSink#asCharSink(Charset)}. Characters written to the 044 * resulting {@code CharSink} will written to the {@code ByteSink} as encoded bytes. 045 * 046 * @since 14.0 047 * @author Colin Decker 048 */ 049public abstract class CharSink implements OutputSupplier<Writer> { 050 051 /** 052 * Constructor for use by subclasses. 053 */ 054 protected CharSink() {} 055 056 /** 057 * Opens a new {@link Writer} for writing to this sink. This method should return a new, 058 * independent writer each time it is called. 059 * 060 * <p>The caller is responsible for ensuring that the returned writer is closed. 061 * 062 * @throws IOException if an I/O error occurs in the process of opening the writer 063 */ 064 public abstract Writer openStream() throws IOException; 065 066 /** 067 * This method is a temporary method provided for easing migration from suppliers to sources and 068 * sinks. 069 * 070 * @since 15.0 071 * @deprecated This method is only provided for temporary compatibility with the 072 * {@link OutputSupplier} interface and should not be called directly. Use 073 * {@link #openStream} instead. This method is scheduled for removal in Guava 18.0. 074 */ 075 @Override 076 @Deprecated 077 public final Writer getOutput() throws IOException { 078 return openStream(); 079 } 080 081 /** 082 * Opens a new buffered {@link Writer} for writing to this sink. The returned stream is not 083 * required to be a {@link BufferedWriter} in order to allow implementations to simply delegate 084 * to {@link #openStream()} when the stream returned by that method does not benefit from 085 * additional buffering. This method should return a new, independent writer each time it is 086 * called. 087 * 088 * <p>The caller is responsible for ensuring that the returned writer is closed. 089 * 090 * @throws IOException if an I/O error occurs in the process of opening the writer 091 * @since 15.0 (in 14.0 with return type {@link BufferedWriter}) 092 */ 093 public Writer openBufferedStream() throws IOException { 094 Writer writer = openStream(); 095 return (writer instanceof BufferedWriter) 096 ? (BufferedWriter) writer 097 : new BufferedWriter(writer); 098 } 099 100 /** 101 * Writes the given character sequence to this sink. 102 * 103 * @throws IOException if an I/O error in the process of writing to this sink 104 */ 105 public void write(CharSequence charSequence) throws IOException { 106 checkNotNull(charSequence); 107 108 Closer closer = Closer.create(); 109 try { 110 Writer out = closer.register(openStream()); 111 out.append(charSequence); 112 out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 113 } catch (Throwable e) { 114 throw closer.rethrow(e); 115 } finally { 116 closer.close(); 117 } 118 } 119 120 /** 121 * Writes the given lines of text to this sink with each line (including the last) terminated with 122 * the operating system's default line separator. This method is equivalent to 123 * {@code writeLines(lines, System.getProperty("line.separator"))}. 124 * 125 * @throws IOException if an I/O error occurs in the process of writing to this sink 126 */ 127 public void writeLines(Iterable<? extends CharSequence> lines) throws IOException { 128 writeLines(lines, System.getProperty("line.separator")); 129 } 130 131 /** 132 * Writes the given lines of text to this sink with each line (including the last) terminated with 133 * the given line separator. 134 * 135 * @throws IOException if an I/O error occurs in the process of writing to this sink 136 */ 137 public void writeLines(Iterable<? extends CharSequence> lines, String lineSeparator) 138 throws IOException { 139 checkNotNull(lines); 140 checkNotNull(lineSeparator); 141 142 Closer closer = Closer.create(); 143 try { 144 Writer out = closer.register(openBufferedStream()); 145 for (CharSequence line : lines) { 146 out.append(line).append(lineSeparator); 147 } 148 out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 149 } catch (Throwable e) { 150 throw closer.rethrow(e); 151 } finally { 152 closer.close(); 153 } 154 } 155 156 /** 157 * Writes all the text from the given {@link Readable} (such as a {@link Reader}) to this sink. 158 * Does not close {@code readable} if it is {@code Closeable}. 159 * 160 * @throws IOException if an I/O error occurs in the process of reading from {@code readable} or 161 * writing to this sink 162 */ 163 public long writeFrom(Readable readable) throws IOException { 164 checkNotNull(readable); 165 166 Closer closer = Closer.create(); 167 try { 168 Writer out = closer.register(openStream()); 169 long written = CharStreams.copy(readable, out); 170 out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 171 return written; 172 } catch (Throwable e) { 173 throw closer.rethrow(e); 174 } finally { 175 closer.close(); 176 } 177 } 178}