Advertisement
Guest User

AppendableWriterAdapter.java

a guest
Jan 18th, 2014
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.io.Closeable;
  2. import java.io.Flushable;
  3. import java.io.IOException;
  4. import java.io.Writer;
  5.  
  6. public class AppendableWriterAdapter extends Writer {
  7.  
  8.     private Appendable appendable;
  9.    
  10.     public AppendableWriterAdapter(Appendable appendable) {
  11.         this.appendable = appendable;
  12.     }
  13.    
  14.     @Override
  15.     public void write(char[] cbuf, int off, int len) throws IOException {
  16.         appendable.append(String.valueOf(cbuf), off, len);
  17.     }
  18.  
  19.     @Override
  20.     public void flush() throws IOException {
  21.         if (appendable instanceof Flushable) {
  22.             ((Flushable) appendable).flush();
  23.         }
  24.     }
  25.  
  26.     @Override
  27.     public void close() throws IOException {
  28.         flush();
  29.         if (appendable instanceof Closeable) {
  30.             ((Closeable) appendable).close();
  31.         }
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement