001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.util;
021    
022    import java.io.Closeable;
023    import java.io.Flushable;
024    import java.io.IOException;
025    import java.io.Writer;
026    
027    public class AppendableWriter extends Writer {
028    
029      /** . */
030      private final Appendable out;
031    
032      /** . */
033      private final Flushable flushable;
034    
035      /** . */
036      private final Closeable closeable;
037    
038      /** . */
039      private boolean closed;
040    
041      /** . */
042      private boolean empty;
043    
044      public AppendableWriter(Appendable out) throws NullPointerException {
045        this(out, null, null);
046      }
047    
048      public AppendableWriter(Appendable out, Flushable flushable) throws NullPointerException {
049        this(out, flushable, null);
050      }
051    
052      public AppendableWriter(Appendable out, Closeable closeable) throws NullPointerException {
053        this(out, null, closeable);
054      }
055    
056      public AppendableWriter(Appendable out, Flushable flushable, Closeable closeable) throws NullPointerException {
057        if (out == null) {
058          throw new NullPointerException("No null appendable expected");
059        }
060    
061        //
062        this.out = out;
063        this.empty = true;
064        this.flushable = flushable;
065        this.closeable = closeable;
066      }
067    
068      public boolean isEmpty() {
069        return empty;
070      }
071    
072      @Override
073      public void write(char[] cbuf, int off, int len) throws IOException {
074        if (closed) {
075          throw new IOException("Already closed");
076        }
077        if (len > 0) {
078          empty = false;
079          int end = off + len;
080          while (off < end) {
081            out.append(cbuf[off++]);
082          }
083        }
084      }
085    
086      @Override
087      public void flush() throws IOException {
088        if (closed) {
089          throw new IOException("Already closed");
090        }
091        if (flushable != null) {
092          flushable.flush();
093        }
094      }
095    
096      @Override
097      public void close() throws IOException {
098        if (!closed) {
099          closed = true;
100          if (closeable != null) {
101            closeable.close();
102          }
103        }
104      }
105    }