View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.codehaus.plexus.archiver.util;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import org.codehaus.plexus.archiver.ArchiverException;
24  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
25  import org.codehaus.plexus.logging.AbstractLogEnabled;
26  import org.codehaus.plexus.util.IOUtil;
27  
28  public abstract class Compressor
29      extends AbstractLogEnabled
30  {
31  
32      private File destFile;
33  
34      private PlexusIoResource source;
35  
36      /**
37       * the required destination file.
38       *
39       * @param compressFile
40       */
41      public void setDestFile( File compressFile )
42      {
43          this.destFile = compressFile;
44      }
45  
46      public File getDestFile()
47      {
48          return destFile;
49      }
50  
51      /**
52       * The resource to compress; required.
53       */
54      public void setSource( PlexusIoResource source )
55      {
56          this.source = source;
57      }
58  
59      /**
60       * The resource to compress; required.
61       */
62      public PlexusIoResource getSource()
63      {
64          return source;
65      }
66  
67      /**
68       * compress a stream to an output stream
69       *
70       * @param in
71       * @param zOut
72       *
73       * @throws IOException
74       */
75      private void compressFile( InputStream in, OutputStream zOut )
76          throws IOException
77      {
78          byte[] buffer = new byte[ 8 * 1024 ];
79          int count = 0;
80          do
81          {
82              zOut.write( buffer, 0, count );
83              count = in.read( buffer, 0, buffer.length );
84          }
85          while ( count != -1 );
86      }
87  
88      /**
89       * compress a resource to an output stream
90       */
91      protected void compress( PlexusIoResource resource, OutputStream zOut )
92          throws IOException
93      {
94          InputStream in = null;
95          try
96          {
97              in = Streams.bufferedInputStream( resource.getContents() );
98              compressFile( in, zOut );
99              in.close();
100             in = null;
101         }
102         finally
103         {
104             IOUtil.close( in );
105         }
106     }
107 
108     /**
109      * subclasses must implement this method to do their compression
110      *
111      * this is public so the process of compression and closing can be dealt with separately.
112      */
113     public abstract void compress()
114         throws ArchiverException;
115 
116     /**
117      * subclasses must implement this method to cleanup after compression
118      *
119      * this is public so the process of compression and closing can be dealt with separately.
120      */
121     public abstract void close()
122         throws ArchiverException;
123 
124 }