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; 18 19 import com.android.tools.build.apkzlib.zip.compress.DeflateExecutionCompressor; 20 import com.android.tools.build.apkzlib.zip.utils.ByteTracker; 21 import java.util.function.Supplier; 22 import java.util.zip.Deflater; 23 import javax.annotation.Nonnull; 24 25 /** 26 * Options to create a {@link ZFile}. 27 */ 28 public class ZFileOptions { 29 30 /** 31 * The byte tracker. 32 */ 33 @Nonnull 34 private ByteTracker tracker; 35 36 /** 37 * The compressor to use. 38 */ 39 @Nonnull 40 private Compressor compressor; 41 42 /** 43 * Should timestamps be zeroed? 44 */ 45 private boolean noTimestamps; 46 47 /** 48 * The alignment rule to use. 49 */ 50 @Nonnull 51 private AlignmentRule alignmentRule; 52 53 /** 54 * Should the extra field be used to cover empty space? 55 */ 56 private boolean coverEmptySpaceUsingExtraField; 57 58 /** 59 * Should files be automatically sorted before update? 60 */ 61 private boolean autoSortFiles; 62 63 /** 64 * Factory creating verification logs to use. 65 */ 66 @Nonnull 67 private Supplier<VerifyLog> verifyLogFactory; 68 69 /** 70 * Creates a new options object. All options are set to their defaults. 71 */ ZFileOptions()72 public ZFileOptions() { 73 tracker = new ByteTracker(); 74 compressor = 75 new DeflateExecutionCompressor( 76 Runnable::run, 77 tracker, 78 Deflater.DEFAULT_COMPRESSION); 79 alignmentRule = AlignmentRules.compose(); 80 verifyLogFactory = VerifyLogs::devNull; 81 } 82 83 /** 84 * Obtains the ZFile's byte tracker. 85 * 86 * @return the byte tracker 87 */ 88 @Nonnull getTracker()89 public ByteTracker getTracker() { 90 return tracker; 91 } 92 93 /** 94 * Obtains the compressor to use. 95 * 96 * @return the compressor 97 */ 98 @Nonnull getCompressor()99 public Compressor getCompressor() { 100 return compressor; 101 } 102 103 /** 104 * Sets the compressor to use. 105 * 106 * @param compressor the compressor 107 */ setCompressor(@onnull Compressor compressor)108 public ZFileOptions setCompressor(@Nonnull Compressor compressor) { 109 this.compressor = compressor; 110 return this; 111 } 112 113 /** 114 * Obtains whether timestamps should be zeroed. 115 * 116 * @return should timestamps be zeroed? 117 */ getNoTimestamps()118 public boolean getNoTimestamps() { 119 return noTimestamps; 120 } 121 122 /** 123 * Sets whether timestamps should be zeroed. 124 * 125 * @param noTimestamps should timestamps be zeroed? 126 */ setNoTimestamps(boolean noTimestamps)127 public ZFileOptions setNoTimestamps(boolean noTimestamps) { 128 this.noTimestamps = noTimestamps; 129 return this; 130 } 131 132 /** 133 * Obtains the alignment rule. 134 * 135 * @return the alignment rule 136 */ 137 @Nonnull getAlignmentRule()138 public AlignmentRule getAlignmentRule() { 139 return alignmentRule; 140 } 141 142 /** 143 * Sets the alignment rule. 144 * 145 * @param alignmentRule the alignment rule 146 */ setAlignmentRule(@onnull AlignmentRule alignmentRule)147 public ZFileOptions setAlignmentRule(@Nonnull AlignmentRule alignmentRule) { 148 this.alignmentRule = alignmentRule; 149 return this; 150 } 151 152 /** 153 * Obtains whether the extra field should be used to cover empty spaces. See {@link ZFile} for 154 * an explanation on using the extra field for covering empty spaces. 155 * 156 * @return should the extra field be used to cover empty spaces? 157 */ getCoverEmptySpaceUsingExtraField()158 public boolean getCoverEmptySpaceUsingExtraField() { 159 return coverEmptySpaceUsingExtraField; 160 } 161 162 /** 163 * Sets whether the extra field should be used to cover empty spaces. See {@link ZFile} for an 164 * explanation on using the extra field for covering empty spaces. 165 * 166 * @param coverEmptySpaceUsingExtraField should the extra field be used to cover empty spaces? 167 */ setCoverEmptySpaceUsingExtraField(boolean coverEmptySpaceUsingExtraField)168 public ZFileOptions setCoverEmptySpaceUsingExtraField(boolean coverEmptySpaceUsingExtraField) { 169 this.coverEmptySpaceUsingExtraField = coverEmptySpaceUsingExtraField; 170 return this; 171 } 172 173 /** 174 * Obtains whether files should be automatically sorted before updating the zip file. See 175 * {@link ZFile} for an explanation on automatic sorting. 176 * 177 * @return should the file be automatically sorted? 178 */ getAutoSortFiles()179 public boolean getAutoSortFiles() { 180 return autoSortFiles; 181 } 182 183 /** 184 * Sets whether files should be automatically sorted before updating the zip file. See {@link 185 * ZFile} for an explanation on automatic sorting. 186 * 187 * @param autoSortFiles should the file be automatically sorted? 188 */ setAutoSortFiles(boolean autoSortFiles)189 public ZFileOptions setAutoSortFiles(boolean autoSortFiles) { 190 this.autoSortFiles = autoSortFiles; 191 return this; 192 } 193 194 /** 195 * Sets the verification log factory. 196 * 197 * @param verifyLogFactory verification log factory 198 */ setVerifyLogFactory(@onnull Supplier<VerifyLog> verifyLogFactory)199 public ZFileOptions setVerifyLogFactory(@Nonnull Supplier<VerifyLog> verifyLogFactory) { 200 this.verifyLogFactory = verifyLogFactory; 201 return this; 202 } 203 204 /** 205 * Obtains the verification log factory. By default, the verification log doesn't store 206 * anything and will always return an empty log. 207 * 208 * @return the verification log factory 209 */ 210 @Nonnull getVerifyLogFactory()211 public Supplier<VerifyLog> getVerifyLogFactory() { 212 return verifyLogFactory; 213 } 214 } 215