summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/apkzlib/zip/utils/ByteTracker.java
blob: 88ed9397400fbf503d0f002010ec3a537d0465df (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * 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.utils;

import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nonnull;

/**
 * Keeps track of used bytes allowing gauging memory usage.
 */
public class ByteTracker {

    /**
     * Number of bytes currently in use.
     */
    private long bytesUsed;

    /**
     * Maximum number of bytes used.
     */
    private long maxBytesUsed;

    /**
     * Creates a new byte source by fully reading an input stream.
     *
     * @param stream the input stream
     * @return a byte source containing the cached data from the given stream
     * @throws IOException failed to read the stream
     */
    public CloseableDelegateByteSource fromStream(@Nonnull InputStream stream) throws IOException {
        byte[] data = ByteStreams.toByteArray(stream);
        updateUsage(data.length);
        return new CloseableDelegateByteSource(ByteSource.wrap(data), data.length) {
            @Override
            public synchronized void innerClose() throws IOException {
                super.innerClose();
                updateUsage(-sizeNoException());
            }
        };
    }

    /**
     * Creates a new byte source by snapshotting the provided stream.
     *
     * @param stream the stream with the data
     * @return a byte source containing the cached data from the given stream
     * @throws IOException failed to read the stream
     */
    public CloseableDelegateByteSource fromStream(@Nonnull ByteArrayOutputStream stream)
            throws IOException {
        byte[] data = stream.toByteArray();
        updateUsage(data.length);
        return new CloseableDelegateByteSource(ByteSource.wrap(data), data.length) {
            @Override
            public synchronized void innerClose() throws IOException {
                super.innerClose();
                updateUsage(-sizeNoException());
            }
        };
    }

    /**
     * Creates a new byte source from another byte source.
     *
     * @param source the byte source to copy data from
     * @return the tracked byte source
     * @throws IOException failed to read data from the byte source
     */
    public CloseableDelegateByteSource fromSource(@Nonnull ByteSource source) throws IOException {
        return fromStream(source.openStream());
    }

    /**
     * Updates the memory used by this tracker.
     *
     * @param delta the number of bytes to add or remove, if negative
     */
    private synchronized void updateUsage(long delta) {
        bytesUsed += delta;
        if (maxBytesUsed < bytesUsed) {
            maxBytesUsed = bytesUsed;
        }
    }

    /**
     * Obtains the number of bytes currently used.
     *
     * @return the number of bytes
     */
    public synchronized long getBytesUsed() {
        return bytesUsed;
    }

    /**
     * Obtains the maximum number of bytes ever used by this tracker.
     *
     * @return the number of bytes
     */
    public synchronized long getMaxBytesUsed() {
        return maxBytesUsed;
    }
}