Source for org.jfree.repository.zipwriter.ZipEntryOutputStream

   1: /**
   2:  * ===========================================================
   3:  * LibRepository : a free Java content repository access layer
   4:  * ===========================================================
   5:  *
   6:  * Project Info:  http://jfreereport.pentaho.org/librepository/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * ZipEntryOutputStream.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository.zipwriter;
  32: 
  33: import java.io.ByteArrayInputStream;
  34: import java.io.ByteArrayOutputStream;
  35: import java.io.IOException;
  36: import java.io.OutputStream;
  37: import java.util.zip.DeflaterOutputStream;
  38: import java.util.zip.InflaterInputStream;
  39: import java.util.zip.ZipEntry;
  40: 
  41: /**
  42:  * Creation-Date: 01.12.2006, 21:26:23
  43:  *
  44:  * @author Thomas Morgner
  45:  */
  46: public class ZipEntryOutputStream extends OutputStream
  47: {
  48:   private ByteArrayOutputStream outputStream;
  49:   private DeflaterOutputStream deflaterOutputStream;
  50:   private boolean closed;
  51:   private ZipContentItem item;
  52: 
  53:   public ZipEntryOutputStream(final ZipContentItem item)
  54:   {
  55:     this.item = item;
  56:     this.outputStream = new ByteArrayOutputStream();
  57:     this.deflaterOutputStream = new DeflaterOutputStream(outputStream);
  58:   }
  59: 
  60:   public void write(final int b)
  61:       throws IOException
  62:   {
  63:     if (closed)
  64:     {
  65:       throw new IOException("Already closed");
  66:     }
  67:     deflaterOutputStream.write(b);
  68:   }
  69: 
  70:   public void write(final byte[] b, final int off, final int len)
  71:       throws IOException
  72:   {
  73:     if (closed)
  74:     {
  75:       throw new IOException("Already closed");
  76:     }
  77:     deflaterOutputStream.write(b, off, len);
  78:   }
  79: 
  80:   public void close()
  81:       throws IOException
  82:   {
  83:     if (closed)
  84:     {
  85:       throw new IOException("Already closed");
  86:     }
  87: 
  88:     deflaterOutputStream.close();
  89:     final byte[] data = outputStream.toByteArray();
  90:     final ByteArrayInputStream bin = new ByteArrayInputStream(data);
  91:     final InflaterInputStream infi = new InflaterInputStream(bin);
  92: 
  93:     final ZipRepository repository = (ZipRepository) item.getRepository();
  94: 
  95:     final String contentId = (String) item.getContentId();
  96:     repository.writeContent(new ZipEntry(contentId), infi);
  97:     infi.close();
  98: 
  99:     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: }