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
24 import org.codehaus.plexus.archiver.ArchiverException;
25 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public abstract class Compressor {
30 private final Logger logger = LoggerFactory.getLogger(getClass());
31
32 protected Logger getLogger() {
33 return logger;
34 }
35
36 private File destFile;
37
38 private PlexusIoResource source;
39
40 /**
41 * the required destination file.
42 *
43 * @param compressFile
44 */
45 public void setDestFile(File compressFile) {
46 this.destFile = compressFile;
47 }
48
49 public File getDestFile() {
50 return destFile;
51 }
52
53 /**
54 * The resource to compress; required.
55 */
56 public void setSource(PlexusIoResource source) {
57 this.source = source;
58 }
59
60 /**
61 * The resource to compress; required.
62 */
63 public PlexusIoResource getSource() {
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) throws IOException {
76 byte[] buffer = new byte[8 * 1024];
77 int count = 0;
78 do {
79 zOut.write(buffer, 0, count);
80 count = in.read(buffer, 0, buffer.length);
81 } while (count != -1);
82 }
83
84 /**
85 * compress a resource to an output stream
86 */
87 protected void compress(PlexusIoResource resource, OutputStream zOut) throws IOException {
88 try (InputStream in = Streams.bufferedInputStream(resource.getContents())) {
89 compressFile(in, zOut);
90 }
91 }
92
93 /**
94 * subclasses must implement this method to do their compression
95 *
96 * this is public so the process of compression and closing can be dealt with separately.
97 */
98 public abstract void compress() throws ArchiverException;
99
100 /**
101 * subclasses must implement this method to cleanup after compression
102 *
103 * this is public so the process of compression and closing can be dealt with separately.
104 */
105 public abstract void close() throws ArchiverException;
106 }