1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.apkzlib.zip.compress;
import com.android.apkzlib.zip.CompressionResult;
import com.android.apkzlib.zip.utils.ByteTracker;
import com.android.apkzlib.zip.utils.CloseableByteSource;
import com.google.common.base.Preconditions;
import java.util.concurrent.Executor;
import java.util.zip.Deflater;
import javax.annotation.Nonnull;
/**
* Compressor that tries both the best and default compression algorithms and picks the default
* unless the best is at least a given percentage smaller.
*/
public class BestAndDefaultDeflateExecutorCompressor extends ExecutorCompressor {
/**
* Deflater using the default compression level.
*/
@Nonnull
private final DeflateExecutionCompressor defaultDeflater;
/**
* Deflater using the best compression level.
*/
@Nonnull
private final DeflateExecutionCompressor bestDeflater;
/**
* Minimum best compression size / default compression size ratio needed to pick the default
* compression size.
*/
private final double minRatio;
/**
* Creates a new compressor.
*
* @param executor the executor used to perform compression activities.
* @param tracker the byte tracker to keep track of allocated bytes
* @param minRatio the minimum best compression size / default compression size needed to pick
* the default compression size; if {@code 0.0} then the default compression is always picked,
* if {@code 1.0} then the best compression is always picked unless it produces the exact same
* size as the default compression.
*/
public BestAndDefaultDeflateExecutorCompressor(@Nonnull Executor executor,
@Nonnull ByteTracker tracker, double minRatio) {
super(executor);
Preconditions.checkArgument(minRatio >= 0.0, "minRatio < 0.0");
Preconditions.checkArgument(minRatio <= 1.0, "minRatio > 1.0");
defaultDeflater =
new DeflateExecutionCompressor(executor, tracker, Deflater.DEFAULT_COMPRESSION);
bestDeflater =
new DeflateExecutionCompressor(executor, tracker, Deflater.BEST_COMPRESSION);
this.minRatio = minRatio;
}
@Nonnull
@Override
protected CompressionResult immediateCompress(@Nonnull CloseableByteSource source)
throws Exception {
CompressionResult defaultResult = defaultDeflater.immediateCompress(source);
CompressionResult bestResult = bestDeflater.immediateCompress(source);
double sizeRatio = bestResult.getSize() / (double) defaultResult.getSize();
if (sizeRatio >= minRatio) {
return defaultResult;
} else {
return bestResult;
}
}
}
|