1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.archivepatcher.applier;
16 
17 import com.google.archivepatcher.shared.JreDeflateParameters;
18 import com.google.archivepatcher.shared.TypedRange;
19 
20 import java.util.List;
21 
22 /**
23  * A plan for transforming the old file prior to applying the delta and for recompressing the
24  * delta-friendly new file afterwards, along with information on the deltas to be applied.
25  * <p>
26  * The plan for uncompressing the old file is a {@link List} of {@link TypedRange} entries with void
27  * metadata. This describes the chunks of the old file that need to be uncompressed prior to
28  * applying the delta, in file order. The file produced by executing this plan is the
29  * "delta-friendly" old file.
30  * <p>
31  * The plan for recompressing the delta-friendly new file is a {@link List} of {@link TypedRange}
32  * entries with {@link JreDeflateParameters} metadata. This describes the chunks of the
33  * delta-friendly new file that need to be recompressed after diffing, again in file order.
34  * The {@link JreDeflateParameters} metadata indicate the settings to use during recompression. The
35  * file produced by executing this plan is the new file.
36  * <p>
37  * The "plan" for the deltas themselves is a {@link List} of {@link DeltaDescriptor} entries that
38  * describe the deltas present in the patch stream. Nominally, a {@link PatchReader} is used to
39  * read the stream up to the first byte of the deltas; the plan for the deltas is ordered in the
40  * same order as the patch stream and contains the byte length of each delta, so it is then trivial
41  * to read each delta in order and apply it.
42  */
43 public class PatchApplyPlan {
44   /**
45    * The plan for uncompressing the old file, in file order.
46    */
47   private final List<TypedRange<Void>> oldFileUncompressionPlan;
48 
49   /**
50    * The plan for recompressing the delta-friendly new file, in file order.
51    */
52   private final List<TypedRange<JreDeflateParameters>> deltaFriendlyNewFileRecompressionPlan;
53 
54   /**
55    * The expected size of the delta-friendly old file after executing the
56    * {@link #oldFileUncompressionPlan}.
57    */
58   private final long deltaFriendlyOldFileSize;
59 
60   /**
61    * The delta descriptors that describe how and what to do to the delta-friendly old file.
62    */
63   private final List<DeltaDescriptor> deltaDescriptors;
64 
65   /**
66    * Constructs a new plan.
67    * @param oldFileUncompressionPlan the plan for uncompressing the old file, in file order
68    * @param deltaFriendlyOldFileSize the expected size of the delta-friendly old file, after
69    * executing the plan in oldFileUncompressionPlan; this can be used to pre-allocate the necessary
70    * space to hold the delta-friendly old file
71    * @param deltaFriendlyNewFileRecompressionPlan the plan for recompressing the delta-friendly new
72    * file, in file order
73    * @param deltaDescriptors the descriptors for the deltas in the patch stream
74    */
PatchApplyPlan( List<TypedRange<Void>> oldFileUncompressionPlan, long deltaFriendlyOldFileSize, List<TypedRange<JreDeflateParameters>> deltaFriendlyNewFileRecompressionPlan, List<DeltaDescriptor> deltaDescriptors)75   public PatchApplyPlan(
76       List<TypedRange<Void>> oldFileUncompressionPlan,
77       long deltaFriendlyOldFileSize,
78       List<TypedRange<JreDeflateParameters>> deltaFriendlyNewFileRecompressionPlan,
79       List<DeltaDescriptor> deltaDescriptors) {
80     this.oldFileUncompressionPlan = oldFileUncompressionPlan;
81     this.deltaFriendlyOldFileSize = deltaFriendlyOldFileSize;
82     this.deltaFriendlyNewFileRecompressionPlan = deltaFriendlyNewFileRecompressionPlan;
83     this.deltaDescriptors = deltaDescriptors;
84   }
85 
86   /**
87    * Returns the old file uncompression plan.
88    * @return as described
89    */
getOldFileUncompressionPlan()90   public List<TypedRange<Void>> getOldFileUncompressionPlan() {
91     return oldFileUncompressionPlan;
92   }
93 
94   /**
95    * Returns the delta-friendly new file recompression plan.
96    * @return as described
97    */
getDeltaFriendlyNewFileRecompressionPlan()98   public List<TypedRange<JreDeflateParameters>> getDeltaFriendlyNewFileRecompressionPlan() {
99     return deltaFriendlyNewFileRecompressionPlan;
100   }
101 
102   /**
103    * Returns the expected size of the delta-friendly old file after executing the plan returned by
104    * {@link #getOldFileUncompressionPlan()}. This can be used to pre-allocate the necessary space to
105    * hold the delta-friendly old file.
106    * @return as described
107    */
getDeltaFriendlyOldFileSize()108   public long getDeltaFriendlyOldFileSize() {
109     return deltaFriendlyOldFileSize;
110   }
111 
112   /**
113    * Returns the delta descriptors that describe how and what to do to the delta-friendly old file.
114    * @return as described
115    */
getDeltaDescriptors()116   public List<DeltaDescriptor> getDeltaDescriptors() {
117     return deltaDescriptors;
118   }
119 }
120