diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java new file mode 100644 index 0000000000..178e988e59 --- /dev/null +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -0,0 +1,141 @@ +package hudson.util; + +import hudson.Util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +/** + * Represents write-once read-many file that can be optiionally compressed + * to save disk space. This is used for console output and other bulky data. + * + *
+ * In this class, the data on the disk can be one of two states: + *
+ * Once the file is compressed, the original will be removed and
+ * the further reading will be done from the compressed stream.
+ */
+ public void compress() {
+ compressionThread.submit(new Runnable() {
+ public void run() {
+ try {
+ InputStream in = read();
+ OutputStream out = new GZIPOutputStream(new FileOutputStream(gz));
+ try {
+ Util.copyStream(in,out);
+ } finally {
+ in.close();
+ out.close();
+ }
+ // if the compressed file is created successfully, remove the original
+ file.delete();
+ } catch (IOException e) {
+ LOGGER.log(Level.WARNING, "Failed to compress "+file,e);
+ gz.delete(); // in case a processing is left in the middle
+ }
+ }
+ });
+ }
+
+ /**
+ * Executor used for compression. Limited up to one thread since
+ * this should be a fairly low-priority task.
+ */
+ private static final ExecutorService compressionThread = new ThreadPoolExecutor(
+ 0, 1, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue