Home | History | Annotate | Download | only in compress
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tools.build.apkzlib.zip.compress;
     18 
     19 import com.android.tools.build.apkzlib.zip.CompressionMethod;
     20 import com.android.tools.build.apkzlib.zip.CompressionResult;
     21 import com.android.tools.build.apkzlib.zip.utils.ByteTracker;
     22 import com.android.tools.build.apkzlib.zip.utils.CloseableByteSource;
     23 import java.io.ByteArrayOutputStream;
     24 import java.util.concurrent.Executor;
     25 import java.util.zip.Deflater;
     26 import java.util.zip.DeflaterOutputStream;
     27 import javax.annotation.Nonnull;
     28 
     29 /**
     30  * Compressor that uses deflate with an executor.
     31  */
     32 public class DeflateExecutionCompressor extends ExecutorCompressor {
     33 
     34 
     35     /**
     36      * Deflate compression level.
     37      */
     38     private final int level;
     39 
     40     /**
     41      * Byte tracker to use to create byte sources.
     42      */
     43     @Nonnull
     44     private final ByteTracker tracker;
     45 
     46     /**
     47      * Creates a new compressor.
     48      *
     49      * @param executor the executor to run deflation tasks
     50      * @param tracker the byte tracker to use to keep track of memory usage
     51      * @param level the compression level
     52      */
     53     public DeflateExecutionCompressor(
     54             @Nonnull Executor executor,
     55             @Nonnull ByteTracker tracker,
     56             int level) {
     57         super(executor);
     58 
     59         this.level = level;
     60         this.tracker = tracker;
     61     }
     62 
     63     @Nonnull
     64     @Override
     65     protected CompressionResult immediateCompress(@Nonnull CloseableByteSource source)
     66             throws Exception {
     67         ByteArrayOutputStream output = new ByteArrayOutputStream();
     68         Deflater deflater = new Deflater(level, true);
     69 
     70         try (DeflaterOutputStream dos = new DeflaterOutputStream(output, deflater)) {
     71             dos.write(source.read());
     72         }
     73 
     74         CloseableByteSource result = tracker.fromStream(output);
     75         if (result.size() >= source.size()) {
     76             return new CompressionResult(source, CompressionMethod.STORE, source.size());
     77         } else {
     78             return new CompressionResult(result, CompressionMethod.DEFLATE, result.size());
     79         }
     80     }
     81 }
     82