Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.io;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.io.Reader;
 import java.io.Writer;
A destination to which characters can be written, such as a text file. Unlike a java.io.Writer, a CharSink is not an open, stateful stream that can be written to and closed. Instead, it is an immutable supplier of Writer instances.

CharSink provides two kinds of methods:

  • Methods that return a writer: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned writer is closed.
  • Convenience methods: These are implementations of common operations that are typically implemented by opening a writer using one of the methods in the first category, doing something and finally closing the writer that was opened.

Any ByteSink may be viewed as a CharSink with a specific character encoding using ByteSink.asCharSink(java.nio.charset.Charset). Characters written to the resulting CharSink will written to the ByteSink as encoded bytes.

Author(s):
Colin Decker
Since:
14.0
 
 public abstract class CharSink {

  
Constructor for use by subclasses.
 
   protected CharSink() {}

  
Opens a new java.io.Writer for writing to this sink. This method should return a new, independent writer each time it is called.

The caller is responsible for ensuring that the returned writer is closed.

Throws:
java.io.IOException if an I/O error occurs in the process of opening the writer
 
   public abstract Writer openStream() throws IOException;

  
Opens a new buffered java.io.Writer for writing to this sink. The returned stream is not required to be a java.io.BufferedWriter in order to allow implementations to simply delegate to openStream() when the stream returned by that method does not benefit from additional buffering. This method should return a new, independent writer each time it is called.

The caller is responsible for ensuring that the returned writer is closed.

Throws:
java.io.IOException if an I/O error occurs in the process of opening the writer
Since:
15.0 (in 14.0 with return type java.io.BufferedWriter)
 
   public Writer openBufferedStream() throws IOException {
     Writer writer = openStream();
     return (writer instanceof BufferedWriter)
         ? (BufferedWriterwriter
         : new BufferedWriter(writer);
   }

  
Writes the given character sequence to this sink.

Throws:
java.io.IOException if an I/O error in the process of writing to this sink
 
   public void write(CharSequence charSequencethrows IOException {
     checkNotNull(charSequence);
 
     Closer closer = Closer.create();
     try {
       Writer out = closer.register(openStream());
       out.append(charSequence);
       out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330
     } catch (Throwable e) {
       throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Writes the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator. This method is equivalent to writeLines(lines, System.getProperty("line.separator")).

Throws:
java.io.IOException if an I/O error occurs in the process of writing to this sink
  public void writeLines(Iterable<? extends CharSequencelinesthrows IOException {
    writeLines(lines, System.getProperty("line.separator"));
  }

  
Writes the given lines of text to this sink with each line (including the last) terminated with the given line separator.

Throws:
java.io.IOException if an I/O error occurs in the process of writing to this sink
  public void writeLines(Iterable<? extends CharSequencelinesString lineSeparator)
      throws IOException {
    checkNotNull(lines);
    checkNotNull(lineSeparator);
    Closer closer = Closer.create();
    try {
      Writer out = closer.register(openBufferedStream());
      for (CharSequence line : lines) {
        out.append(line).append(lineSeparator);
      }
      out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }

  
Writes all the text from the given java.lang.Readable (such as a java.io.Reader) to this sink. Does not close readable if it is Closeable.

Throws:
java.io.IOException if an I/O error occurs in the process of reading from readable or writing to this sink
  public long writeFrom(Readable readablethrows IOException {
    checkNotNull(readable);
    Closer closer = Closer.create();
    try {
      Writer out = closer.register(openStream());
      long written = CharStreams.copy(readableout);
      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();
    }
  }
New to GrepCode? Check out our FAQ X