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 */
019package org.crsh.util;
020
021import java.io.ByteArrayOutputStream;
022import java.io.IOException;
023import java.io.InputStream;
024
025public class BytesOutputStream extends ByteArrayOutputStream {
026
027  /** . */
028  private boolean closed;
029
030  public BytesOutputStream() {
031  }
032
033  public BytesOutputStream(int size) {
034    super(size);
035  }
036
037  public InputStream getInputStream() {
038    if (!closed) {
039      throw new IllegalStateException("Not yet closed");
040    }
041    return new InputStream() {
042
043      /** . */
044      int read = 0;
045
046      @Override
047      public int read() throws IOException {
048        if (read < count) {
049          return buf[read++];
050        } else {
051          return -1;
052        }
053      }
054    };
055  }
056
057  @Override
058  public void close() throws IOException {
059    super.close();
060
061    //
062    closed = true;
063  }
064}