001    /**
002     * ===========================================================
003     * LibRepository : a free Java content repository access layer
004     * ===========================================================
005     *
006     * Project Info:  http://jfreereport.pentaho.org/librepository/
007     *
008     * (C) Copyright 2006, by Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * ZipEntryOutputStream.java
027     * ------------
028     * (C) Copyright 2006, by Pentaho Corporation.
029     */
030    
031    package org.jfree.repository.zipwriter;
032    
033    import java.io.ByteArrayInputStream;
034    import java.io.ByteArrayOutputStream;
035    import java.io.IOException;
036    import java.io.OutputStream;
037    import java.util.zip.DeflaterOutputStream;
038    import java.util.zip.InflaterInputStream;
039    import java.util.zip.ZipEntry;
040    
041    /**
042     * Creation-Date: 01.12.2006, 21:26:23
043     *
044     * @author Thomas Morgner
045     */
046    public class ZipEntryOutputStream extends OutputStream
047    {
048      private ByteArrayOutputStream outputStream;
049      private DeflaterOutputStream deflaterOutputStream;
050      private boolean closed;
051      private ZipContentItem item;
052    
053      public ZipEntryOutputStream(final ZipContentItem item)
054      {
055        this.item = item;
056        this.outputStream = new ByteArrayOutputStream();
057        this.deflaterOutputStream = new DeflaterOutputStream(outputStream);
058      }
059    
060      public void write(final int b)
061          throws IOException
062      {
063        if (closed)
064        {
065          throw new IOException("Already closed");
066        }
067        deflaterOutputStream.write(b);
068      }
069    
070      public void write(final byte[] b, final int off, final int len)
071          throws IOException
072      {
073        if (closed)
074        {
075          throw new IOException("Already closed");
076        }
077        deflaterOutputStream.write(b, off, len);
078      }
079    
080      public void close()
081          throws IOException
082      {
083        if (closed)
084        {
085          throw new IOException("Already closed");
086        }
087    
088        deflaterOutputStream.close();
089        final byte[] data = outputStream.toByteArray();
090        final ByteArrayInputStream bin = new ByteArrayInputStream(data);
091        final InflaterInputStream infi = new InflaterInputStream(bin);
092    
093        final ZipRepository repository = (ZipRepository) item.getRepository();
094    
095        final String contentId = (String) item.getContentId();
096        repository.writeContent(new ZipEntry(contentId), infi);
097        infi.close();
098    
099        outputStream = null;
100        deflaterOutputStream = null;
101      }
102    
103      public void write(final byte[] b)
104          throws IOException
105      {
106        if (closed)
107        {
108          throw new IOException("Already closed");
109        }
110        deflaterOutputStream.write(b);
111      }
112    
113      public void flush()
114          throws IOException
115      {
116        if (closed)
117        {
118          throw new IOException("Already closed");
119        }
120        deflaterOutputStream.flush();
121      }
122    }