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.utils.CloseableByteSource;
20 import com.google.common.io.Closer;
21 import java.io.Closeable;
22 import java.io.IOException;
23 import javax.annotation.Nonnull;
24 
25 /**
26  * Container that has two bytes sources: one representing raw data and another processed data.
27  * In case of compression, the raw data is the compressed data and the processed data is the
28  * uncompressed data. It is valid for a RaP ("Raw-and-Processed") to contain the same byte sources
29  * for both processed and raw data.
30  */
31 public class ProcessedAndRawByteSources implements Closeable {
32 
33     /**
34      * The processed byte source.
35      */
36     @Nonnull
37     private final CloseableByteSource processedSource;
38 
39     /**
40      * The processed raw source.
41      */
42     @Nonnull
43     private final CloseableByteSource rawSource;
44 
45     /**
46      * Creates a new container.
47      *
48      * @param processedSource the processed source
49      * @param rawSource the raw source
50      */
ProcessedAndRawByteSources(@onnull CloseableByteSource processedSource, @Nonnull CloseableByteSource rawSource)51     public ProcessedAndRawByteSources(@Nonnull CloseableByteSource processedSource,
52             @Nonnull CloseableByteSource rawSource) {
53         this.processedSource = processedSource;
54         this.rawSource = rawSource;
55     }
56 
57     /**
58      * Obtains a byte source that read the processed contents of the entry.
59      *
60      * @return a byte source
61      */
62     @Nonnull
getProcessedByteSource()63     public CloseableByteSource getProcessedByteSource() {
64         return processedSource;
65     }
66 
67     /**
68      * Obtains a byte source that reads the raw contents of an entry. This is the data that is
69      * ultimately stored in the file and, in the case of compressed files, is the same data in the
70      * source returned by {@link #getProcessedByteSource()}.
71      *
72      * @return a byte source
73      */
74     @Nonnull
getRawByteSource()75     public CloseableByteSource getRawByteSource() {
76         return rawSource;
77     }
78 
79     @Override
close()80     public void close() throws IOException {
81         Closer closer = Closer.create();
82         closer.register(processedSource);
83         closer.register(rawSource);
84         closer.close();
85     }
86 }
87