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
020package org.crsh.text;
021
022import org.crsh.shell.ScreenContext;
023
024import java.io.Closeable;
025import java.io.IOException;
026import java.io.Writer;
027import java.nio.CharBuffer;
028
029public class RenderWriter extends Writer implements ScreenContext {
030
031  /** . */
032  private final ScreenContext out;
033
034  /** . */
035  private final Closeable closeable;
036
037  /** . */
038  private boolean closed;
039
040  /** . */
041  private boolean empty;
042
043  public RenderWriter(ScreenContext out) throws NullPointerException {
044    this(out, null);
045  }
046
047  public RenderWriter(ScreenContext out, Closeable closeable) throws NullPointerException {
048    if (out == null) {
049      throw new NullPointerException("No null appendable expected");
050    }
051
052    //
053    this.out = out;
054    this.empty = true;
055    this.closeable = closeable;
056  }
057
058  public boolean isEmpty() {
059    return empty;
060  }
061
062  public int getWidth() {
063    return out.getWidth();
064  }
065
066  public int getHeight() {
067    return out.getHeight();
068  }
069
070  public Class<Chunk> getConsumedType() {
071    return Chunk.class;
072  }
073
074  public void write(Chunk chunk) throws IOException {
075    provide(chunk);
076  }
077
078  public void provide(Chunk element) throws IOException {
079    if (element instanceof Text) {
080      Text text = (Text)element;
081      empty &= text.getText().length() == 0;
082    }
083    out.write(element);
084  }
085
086  @Override
087  public void write(char[] cbuf, int off, int len) throws IOException {
088    if (closed) {
089      throw new IOException("Already closed");
090    }
091    if (len > 0) {
092      provide(Text.create(new String(cbuf, off, len)));
093    }
094  }
095
096  @Override
097  public void flush() throws IOException {
098    if (closed) {
099      throw new IOException("Already closed");
100    }
101    out.flush();
102  }
103
104  @Override
105  public void close() throws IOException {
106    if (!closed) {
107      closed = true;
108      if (closeable != null) {
109        closeable.close();
110      }
111    }
112  }
113}