Debian Codegraph @

src/org/tukaani/xz/ResettableArrayCache.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4
5 package org.tukaani.xz;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 /**
11 * An ArrayCache wrapper that remembers what has been allocated
12 * and allows returning all allocations to the underlying cache at once.
13 *
14 * @since 1.7
15 */
16 public class ResettableArrayCache extends ArrayCache {
17 private final ArrayCache arrayCache;
18
19 // Lists of arrays that have been allocated from the arrayCache.
20 private final List<byte[]> byteArrays;
21 private final List<int[]> intArrays;
22
23 /**
24 * Creates a new ResettableArrayCache based on the given ArrayCache.
25 */
26 public ResettableArrayCache(ArrayCache arrayCache) {
27 this.arrayCache = arrayCache;
28
29 // Treat the dummy cache as a special case since it's a common case.
30 // With it we don't need to put the arrays back to the cache and
31 // thus we don't need to remember what has been allocated.
32 if (arrayCache == ArrayCache.getDummyCache()) {
33 byteArrays = null;
34 intArrays = null;
35 } else {
36 byteArrays = new ArrayList<byte[]>();
37 intArrays = new ArrayList<int[]>();
38 }
39 }
40
41 @Override
42 public byte[] getByteArray(int size, boolean fillWithZeros) {
43 byte[] array = arrayCache.getByteArray(size, fillWithZeros);
44
45 if (byteArrays != null) {
46 synchronized(byteArrays) {
47 byteArrays.add(array);
48 }
49 }
50
51 return array;
52 }
53
54 @Override
55 public void putArray(byte[] array) {
56 if (byteArrays != null) {
57 // The array is more likely to be near the end of the list so
58 // start the search from the end.
59 synchronized(byteArrays) {
60 int i = byteArrays.lastIndexOf(array);
61 if (i != -1)
62 byteArrays.remove(i);
63 }
64
65 arrayCache.putArray(array);
66 }
67 }
68
69 @Override
70 public int[] getIntArray(int size, boolean fillWithZeros) {
71 int[] array = arrayCache.getIntArray(size, fillWithZeros);
72
73 if (intArrays != null) {
74 synchronized(intArrays) {
75 intArrays.add(array);
76 }
77 }
78
79 return array;
80 }
81
82 @Override
83 public void putArray(int[] array) {
84 if (intArrays != null) {
85 synchronized(intArrays) {
86 int i = intArrays.lastIndexOf(array);
87 if (i != -1)
88 intArrays.remove(i);
89 }
90
91 arrayCache.putArray(array);
92 }
93 }
94
95 /**
96 * Puts all allocated arrays back to the underlying ArrayCache
97 * that haven't already been put there with a call to
98 * {@code putArray}.
99 */
100 public void reset() {
101 if (byteArrays != null) {
102 // Put the arrays to the cache in reverse order: the array that
103 // was allocated first is returned last.
104 synchronized(byteArrays) {
105 for (int i = byteArrays.size() - 1; i >= 0; --i)
106 arrayCache.putArray(byteArrays.get(i));
107
108 byteArrays.clear();
109 }
110
111 synchronized(intArrays) {
112 for (int i = intArrays.size() - 1; i >= 0; --i)
113 arrayCache.putArray(intArrays.get(i));
114
115 intArrays.clear();
116 }
117 }
118 }
119 }