Debian Codegraph @

src/org/tukaani/xz/ArrayCache.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 /**
8 * Caches large arrays for reuse (base class and a dummy cache implementation).
9 * <p>
10 * When compressing or decompressing many (very) small files in a row, the
11 * time spent in construction of new compressor or decompressor objects
12 * can be longer than the time spent in actual compression or decompression.
13 * A large part of this initialization overhead comes from allocation and
14 * garbage collection of large arrays.
15 * <p>
16 * The {@code ArrayCache} API provides a way to cache large array allocations
17 * for reuse. It can give a major performance improvement when compressing or
18 * decompressing many tiny files. If you are only (de)compressing one or two
19 * files or the files a very big, array caching won't improve anything,
20 * although it won't make anything slower either.
21 * <p>
22 * <b>Important: The users of ArrayCache don't return the allocated arrays
23 * back to the cache in all situations.</b>
24 * This a reason why it's called a cache instead of a pool.
25 * If it is important to be able to return every array back to a cache,
26 * {@link ResettableArrayCache} can be useful.
27 * <p>
28 * In compressors (OutputStreams) the arrays are returned to the cache
29 * when a call to {@code finish()} or {@code close()} returns
30 * successfully (no exceptions are thrown).
31 * <p>
32 * In decompressors (InputStreams) the arrays are returned to the cache when
33 * the decompression is successfully finished ({@code read} returns {@code -1})
34 * or {@code close()} or {@code close(boolean)} is called. This is true even
35 * if closing throws an exception.
36 * <p>
37 * Raw decompressors don't support {@code close(boolean)}. With raw
38 * decompressors, if one wants to put the arrays back to the cache without
39 * closing the underlying {@code InputStream}, one can wrap the
40 * {@code InputStream} into {@link CloseIgnoringInputStream} when creating
41 * the decompressor instance. Then one can use {@code close()}.
42 * <p>
43 * Different cache implementations can be extended from this base class.
44 * All cache implementations must be thread safe.
45 * <p>
46 * This class also works as a dummy cache that simply calls {@code new}
47 * to allocate new arrays and doesn't try to cache anything. A statically
48 * allocated dummy cache is available via {@link #getDummyCache()}.
49 * <p>
50 * If no {@code ArrayCache} is specified when constructing a compressor or
51 * decompressor, the default {@code ArrayCache} implementation is used.
52 * See {@link #getDefaultCache()} and {@link #setDefaultCache(ArrayCache)}.
53 * Since 1.10, the default can also be set using the system property
54 * {@code org.tukaani.xz.ArrayCache}. Supported values are {@code Dummy}
55 * (the default) and {@code Basic} (to use {@code BasicArrayCache}).
56 * <p>
57 * This is a class instead of an interface because it's possible that in the
58 * future we may want to cache other array types too. New methods can be
59 * added to this class without breaking existing cache implementations.
60 *
61 * @since 1.7
62 *
63 * @see BasicArrayCache
64 */
65 public class ArrayCache {
66 /**
67 * Global dummy cache instance that is returned by {@code getDummyCache()}.
68 */
69 private static final ArrayCache dummyCache = new ArrayCache();
70
71 /**
72 * Global default {@code ArrayCache} that is used when no other cache has
73 * been specified.
74 */
75 private static volatile ArrayCache defaultCache;
76
77 static {
78 String cacheType = System.getProperty("org.tukaani.xz.ArrayCache");
79 if (cacheType == null)
80 cacheType = "Dummy";
81
82 switch (cacheType) {
83 case "Dummy":
84 defaultCache = dummyCache;
85 break;
86
87 case "Basic":
88 defaultCache = BasicArrayCache.getInstance();
89 break;
90
91 default:
92 throw new Error("Unsupported value '" + cacheType +
93 "' in the system property " +
94 "org.tukaani.xz.ArrayCache. " +
95 "Supported values: Dummy, Basic");
96 }
97 }
98
99 /**
100 * Returns a statically-allocated {@code ArrayCache} instance.
101 * It can be shared by all code that needs a dummy cache.
102 */
103 public static ArrayCache getDummyCache() {
104 return dummyCache;
105 }
106
107 /**
108 * Gets the default {@code ArrayCache} instance.
109 * This is a global cache that is used when the application
110 * specifies nothing else. The default is a dummy cache
111 * (see {@link #getDummyCache()}).
112 */
113 public static ArrayCache getDefaultCache() {
114 // It's volatile so no need for synchronization.
115 return defaultCache;
116 }
117
118 /**
119 * Sets the default {@code ArrayCache} instance.
120 * Use with care. Other libraries using this package probably shouldn't
121 * call this function as libraries cannot know if there are other users
122 * of the xz package in the same application.
123 */
124 public static void setDefaultCache(ArrayCache arrayCache) {
125 if (arrayCache == null)
126 throw new NullPointerException();
127
128 // It's volatile so no need for synchronization.
129 defaultCache = arrayCache;
130 }
131
132 /**
133 * Creates a new {@code ArrayCache} that does no caching
134 * (a dummy cache). If you need a dummy cache, you may want to call
135 * {@link #getDummyCache()} instead.
136 */
137 public ArrayCache() {}
138
139 /**
140 * Allocates a new byte array.
141 * <p>
142 * This implementation simply returns {@code new byte[size]}.
143 *
144 * @param size the minimum size of the array to allocate;
145 * an implementation may return an array that
146 * is larger than the given {@code size}
147 *
148 * @param fillWithZeros if true, the caller expects that the first
149 * {@code size} elements in the array are zero;
150 * if false, the array contents can be anything,
151 * which speeds things up when reusing a cached
152 * array
153 */
154 public byte[] getByteArray(int size, boolean fillWithZeros) {
155 return new byte[size];
156 }
157
158 /**
159 * Puts the given byte array to the cache. The caller must no longer
160 * use the array.
161 * <p>
162 * This implementation does nothing.
163 */
164 public void putArray(byte[] array) {}
165
166 /**
167 * Allocates a new int array.
168 * <p>
169 * This implementation simply returns {@code new int[size]}.
170 *
171 * @param size the minimum size of the array to allocate;
172 * an implementation may return an array that
173 * is larger than the given {@code size}
174 *
175 * @param fillWithZeros if true, the caller expects that the first
176 * {@code size} elements in the array are zero;
177 * if false, the array contents can be anything,
178 * which speeds things up when reusing a cached
179 * array
180 */
181 public int[] getIntArray(int size, boolean fillWithZeros) {
182 return new int[size];
183 }
184
185 /**
186 * Puts the given int array to the cache. The caller must no longer
187 * use the array.
188 * <p>
189 * This implementation does nothing.
190 */
191 public void putArray(int[] array) {}
192 }