├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── LICENSE.txt
├── README.md
├── README.third_party
├── build.gradle
├── checkstyle.xml
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
└── src
├── main
└── java
│ └── com
│ └── bumptech
│ └── glide
│ └── disklrucache
│ ├── DiskLruCache.java
│ ├── StrictLineReader.java
│ └── Util.java
└── test
└── java
└── com
└── bumptech
└── glide
└── disklrucache
├── DiskLruCacheTest.java
└── StrictLineReaderTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | #Eclipse
2 | .project
3 | .classpath
4 | .settings
5 | .checkstyle
6 |
7 | #IntelliJ IDEA
8 | .idea
9 | *.iml
10 | *.ipr
11 | *.iws
12 |
13 | #Maven
14 | target
15 | release.properties
16 | pom.xml.*
17 |
18 | #OSX
19 | .DS_Store
20 |
21 | #gradle
22 | build/**
23 | .gradle/**
24 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | sudo: false
3 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | Change Log
2 | ==========
3 |
4 | Version 2.0.2 *(2013-06-18)*
5 | ----------------------------
6 |
7 | * Fix: Prevent exception trying to delete a non-existent file.
8 |
9 |
10 | Version 2.0.1 *(2013-04-27)*
11 | ----------------------------
12 |
13 | * Fix: Do not throw runtime exceptions for racy file I/O.
14 | * Fix: Synchronize calls to `isClosed`.
15 |
16 |
17 | Version 2.0.0 *(2013-04-13)*
18 | ----------------------------
19 |
20 | The package name is now `com.jakewharton.disklrucache`.
21 |
22 | * New: Automatically flush the cache when an edit is completed.
23 | * Fix: Ensure file handles are not held when a file is not found.
24 | * Fix: Correct journal rebuilds on Windows.
25 | * Fix: Ensure file writer uses the appropriate encoding.
26 |
27 |
28 | Version 1.3.1 *(2013-01-02)*
29 | ----------------------------
30 |
31 | * Fix: Correct logic around detecting whether a journal rebuild is required.
32 | *(Thanks Jonathan Gerbaud)*
33 |
34 |
35 | Version 1.3.0 *(2012-12-24)*
36 | ----------------------------
37 |
38 | * Re-allow dash in cache key (now `[a-z0-9_-]{1,64}`).
39 | * New: `getLength` method on `Snapshot`. *(Thanks Edward Dale)*
40 | * Performance improvements reading journal lines.
41 |
42 |
43 | Version 1.2.1 *(2012-10-08)*
44 | ----------------------------
45 |
46 | * Fix: Ensure library references Java 5-compatible version of
47 | `Arrays.copyOfRange`. *(Thanks Edward Dale)*
48 |
49 |
50 | Version 1.2.0 *(2012-09-30)*
51 | ----------------------------
52 |
53 | * New API for cache size adjustment.
54 | * Keys are now enforced to match `[a-z0-9_]{1,64}` *(Thanks Brian Langel)*
55 | * Fix: Cache will gracefully recover if directory is deleted at runtime.
56 |
57 |
58 | Version 1.1.0 *(2012-01-07)*
59 | ----------------------------
60 |
61 | * New API for editing an existing snapshot. *(Thanks Jesse Wilson)*
62 |
63 |
64 | Version 1.0.0 *(2012-01-04)*
65 | ----------------------------
66 |
67 | Initial version.
68 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2012 Jake Wharton
2 | Copyright 2011 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 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 2012 Jake Wharton
191 | Copyright 2011 The Android Open Source Project
192 |
193 | Licensed under the Apache License, Version 2.0 (the "License");
194 | you may not use this file except in compliance with the License.
195 | You may obtain a copy of the License at
196 |
197 | http://www.apache.org/licenses/LICENSE-2.0
198 |
199 | Unless required by applicable law or agreed to in writing, software
200 | distributed under the License is distributed on an "AS IS" BASIS,
201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202 | See the License for the specific language governing permissions and
203 | limitations under the License.
204 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Disk LRU Cache
2 | ==============
3 |
4 | A cache that uses a bounded amount of space on a filesystem. Each cache entry
5 | has a string key and a fixed number of values. Each key must match the regex
6 | `[a-z0-9_-]{1,64}`. Values are byte sequences, accessible as streams or files.
7 | Each value must be between `0` and `Integer.MAX_VALUE` bytes in length.
8 |
9 | The cache stores its data in a directory on the filesystem. This directory must
10 | be exclusive to the cache; the cache may delete or overwrite files from its
11 | directory. It is an error for multiple processes to use the same cache
12 | directory at the same time.
13 |
14 | This cache limits the number of bytes that it will store on the filesystem.
15 | When the number of stored bytes exceeds the limit, the cache will remove
16 | entries in the background until the limit is satisfied. The limit is not
17 | strict: the cache may temporarily exceed it while waiting for files to be
18 | deleted. The limit does not include filesystem overhead or the cache journal so
19 | space-sensitive applications should set a conservative limit.
20 |
21 | Clients call `edit` to create or update the values of an entry. An entry may
22 | have only one editor at one time; if a value is not available to be edited then
23 | `edit` will return null.
24 |
25 | * When an entry is being **created** it is necessary to supply a full set of
26 | values; the empty value should be used as a placeholder if necessary.
27 | * When an entry is being **edited**, it is not necessary to supply data for
28 | every value; values default to their previous value.
29 |
30 | Every `edit` call must be matched by a call to `Editor.commit` or
31 | `Editor.abort`. Committing is atomic: a read observes the full set of values as
32 | they were before or after the commit, but never a mix of values.
33 |
34 | Clients call `get` to read a snapshot of an entry. The read will observe the
35 | value at the time that `get` was called. Updates and removals after the call do
36 | not impact ongoing reads.
37 |
38 | This class is tolerant of some I/O errors. If files are missing from the
39 | filesystem, the corresponding entries will be dropped from the cache. If an
40 | error occurs while writing a cache value, the edit will fail silently. Callers
41 | should handle other problems by catching `IOException` and responding
42 | appropriately.
43 |
44 | *Note: This implementation specifically targets Android compatibility.*
45 |
46 | License
47 | =======
48 |
49 | Copyright 2012 Jake Wharton
50 | Copyright 2011 The Android Open Source Project
51 |
52 | Licensed under the Apache License, Version 2.0 (the "License");
53 | you may not use this file except in compliance with the License.
54 | You may obtain a copy of the License at
55 |
56 | http://www.apache.org/licenses/LICENSE-2.0
57 |
58 | Unless required by applicable law or agreed to in writing, software
59 | distributed under the License is distributed on an "AS IS" BASIS,
60 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61 | See the License for the specific language governing permissions and
62 | limitations under the License.
63 |
64 |
--------------------------------------------------------------------------------
/README.third_party:
--------------------------------------------------------------------------------
1 | URL: https://github.com/JakeWharton/DiskLruCache/tarball/7a1ecbd38d2ad0873fb843e911d60235b7434acb
2 | Version: 7a1ecbd38d2ad0873fb843e911d60235b7434acb
3 | License: Apache 2.0
4 | License File: LICENSE
5 |
6 | Description:
7 | Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.
8 |
9 | Local Modifications:
10 | Exposed File objects directly to gets, removed key validation, removed test sources.
11 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 | apply plugin: 'checkstyle'
3 |
4 | repositories {
5 | jcenter()
6 | }
7 |
8 | checkstyle {
9 | toolVersion = "6.6"
10 | }
11 |
12 | checkstyle {
13 | configFile = new File(projectDir, 'checkstyle.xml')
14 | }
15 |
16 | dependencies {
17 | def junitVersion = hasProperty('JUNIT_VERSION') ? JUNIT_VERSION : '4.11';
18 | testCompile "junit:junit:${junitVersion}"
19 | testCompile 'commons-io:commons-io:2.1'
20 | testCompile 'org.easytesting:fest-assert-core:2.0M10'
21 | }
22 |
23 | def uploaderScript = "${rootProject.projectDir}/scripts/upload.gradle"
24 | if (file(uploaderScript).exists()) {
25 | apply from: uploaderScript
26 | }
27 |
--------------------------------------------------------------------------------
/checkstyle.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
The cache stores its data in a directory on the filesystem. This 50 | * directory must be exclusive to the cache; the cache may delete or overwrite 51 | * files from its directory. It is an error for multiple processes to use the 52 | * same cache directory at the same time. 53 | * 54 | *
This cache limits the number of bytes that it will store on the 55 | * filesystem. When the number of stored bytes exceeds the limit, the cache will 56 | * remove entries in the background until the limit is satisfied. The limit is 57 | * not strict: the cache may temporarily exceed it while waiting for files to be 58 | * deleted. The limit does not include filesystem overhead or the cache 59 | * journal so space-sensitive applications should set a conservative limit. 60 | * 61 | *
Clients call {@link #edit} to create or update the values of an entry. An 62 | * entry may have only one editor at one time; if a value is not available to be 63 | * edited then {@link #edit} will return null. 64 | *
Clients call {@link #get} to read a snapshot of an entry. The read will 77 | * observe the value at the time that {@link #get} was called. Updates and 78 | * removals after the call do not impact ongoing reads. 79 | * 80 | *
This class is tolerant of some I/O errors. If files are missing from the
81 | * filesystem, the corresponding entries will be dropped from the cache. If
82 | * an error occurs while writing a cache value, the edit will fail silently.
83 | * Callers should handle other problems by catching {@code IOException} and
84 | * responding appropriately.
85 | */
86 | public final class DiskLruCache implements Closeable {
87 | static final String JOURNAL_FILE = "journal";
88 | static final String JOURNAL_FILE_TEMP = "journal.tmp";
89 | static final String JOURNAL_FILE_BACKUP = "journal.bkp";
90 | static final String MAGIC = "libcore.io.DiskLruCache";
91 | static final String VERSION_1 = "1";
92 | static final long ANY_SEQUENCE_NUMBER = -1;
93 | private static final String CLEAN = "CLEAN";
94 | private static final String DIRTY = "DIRTY";
95 | private static final String REMOVE = "REMOVE";
96 | private static final String READ = "READ";
97 |
98 | /*
99 | * This cache uses a journal file named "journal". A typical journal file
100 | * looks like this:
101 | * libcore.io.DiskLruCache
102 | * 1
103 | * 100
104 | * 2
105 | *
106 | * CLEAN 3400330d1dfc7f3f7f4b8d4d803dfcf6 832 21054
107 | * DIRTY 335c4c6028171cfddfbaae1a9c313c52
108 | * CLEAN 335c4c6028171cfddfbaae1a9c313c52 3934 2342
109 | * REMOVE 335c4c6028171cfddfbaae1a9c313c52
110 | * DIRTY 1ab96a171faeeee38496d8b330771a7a
111 | * CLEAN 1ab96a171faeeee38496d8b330771a7a 1600 234
112 | * READ 335c4c6028171cfddfbaae1a9c313c52
113 | * READ 3400330d1dfc7f3f7f4b8d4d803dfcf6
114 | *
115 | * The first five lines of the journal form its header. They are the
116 | * constant string "libcore.io.DiskLruCache", the disk cache's version,
117 | * the application's version, the value count, and a blank line.
118 | *
119 | * Each of the subsequent lines in the file is a record of the state of a
120 | * cache entry. Each line contains space-separated values: a state, a key,
121 | * and optional state-specific values.
122 | * o DIRTY lines track that an entry is actively being created or updated.
123 | * Every successful DIRTY action should be followed by a CLEAN or REMOVE
124 | * action. DIRTY lines without a matching CLEAN or REMOVE indicate that
125 | * temporary files may need to be deleted.
126 | * o CLEAN lines track a cache entry that has been successfully published
127 | * and may be read. A publish line is followed by the lengths of each of
128 | * its values.
129 | * o READ lines track accesses for LRU.
130 | * o REMOVE lines track entries that have been deleted.
131 | *
132 | * The journal file is appended to as cache operations occur. The journal may
133 | * occasionally be compacted by dropping redundant lines. A temporary file named
134 | * "journal.tmp" will be used during compaction; that file should be deleted if
135 | * it exists when the cache is opened.
136 | */
137 |
138 | private final File directory;
139 | private final File journalFile;
140 | private final File journalFileTmp;
141 | private final File journalFileBackup;
142 | private final int appVersion;
143 | private long maxSize;
144 | private final int valueCount;
145 | private long size = 0;
146 | private Writer journalWriter;
147 | private final LinkedHashMap This class is used for buffered reading of lines. For purposes of this class, a line ends
31 | * with "\n" or "\r\n". End of input is reported by throwing {@code EOFException}. Unterminated
32 | * line at end of input is invalid and will be ignored, the caller may use {@code
33 | * hasUnterminatedLine()} to detect it after catching the {@code EOFException}.
34 | *
35 | * This class is intended for reading input that strictly consists of lines, such as line-based
36 | * cache entries or cache journal. Unlike the {@link java.io.BufferedReader} which in conjunction
37 | * with {@link java.io.InputStreamReader} provides similar functionality, this class uses different
38 | * end-of-input reporting and a more restrictive definition of a line.
39 | *
40 | * This class supports only charsets that encode '\r' and '\n' as a single byte with value 13
41 | * and 10, respectively, and the representation of no other character contains these values.
42 | * We currently check in constructor that the charset is one of US-ASCII, UTF-8 and ISO-8859-1.
43 | * The default charset is US_ASCII.
44 | */
45 | class StrictLineReader implements Closeable {
46 | private static final byte CR = (byte) '\r';
47 | private static final byte LF = (byte) '\n';
48 |
49 | private final InputStream in;
50 | private final Charset charset;
51 |
52 | /*
53 | * Buffered data is stored in {@code buf}. As long as no exception occurs, 0 <= pos <= end
54 | * and the data in the range [pos, end) is buffered for reading. At end of input, if there is
55 | * an unterminated line, we set end == -1, otherwise end == pos. If the underlying
56 | * {@code InputStream} throws an {@code IOException}, end may remain as either pos or -1.
57 | */
58 | private byte[] buf;
59 | private int pos;
60 | private int end;
61 |
62 | /**
63 | * Constructs a new {@code LineReader} with the specified charset and the default capacity.
64 | *
65 | * @param in the {@code InputStream} to read data from.
66 | * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
67 | * supported.
68 | * @throws NullPointerException if {@code in} or {@code charset} is null.
69 | * @throws IllegalArgumentException if the specified charset is not supported.
70 | */
71 | public StrictLineReader(InputStream in, Charset charset) {
72 | this(in, 8192, charset);
73 | }
74 |
75 | /**
76 | * Constructs a new {@code LineReader} with the specified capacity and charset.
77 | *
78 | * @param in the {@code InputStream} to read data from.
79 | * @param capacity the capacity of the buffer.
80 | * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
81 | * supported.
82 | * @throws NullPointerException if {@code in} or {@code charset} is null.
83 | * @throws IllegalArgumentException if {@code capacity} is negative or zero
84 | * or the specified charset is not supported.
85 | */
86 | public StrictLineReader(InputStream in, int capacity, Charset charset) {
87 | if (in == null || charset == null) {
88 | throw new NullPointerException();
89 | }
90 | if (capacity < 0) {
91 | throw new IllegalArgumentException("capacity <= 0");
92 | }
93 | if (!(charset.equals(Util.US_ASCII))) {
94 | throw new IllegalArgumentException("Unsupported encoding");
95 | }
96 |
97 | this.in = in;
98 | this.charset = charset;
99 | buf = new byte[capacity];
100 | }
101 |
102 | /**
103 | * Closes the reader by closing the underlying {@code InputStream} and
104 | * marking this reader as closed.
105 | *
106 | * @throws IOException for errors when closing the underlying {@code InputStream}.
107 | */
108 | public void close() throws IOException {
109 | synchronized (in) {
110 | if (buf != null) {
111 | buf = null;
112 | in.close();
113 | }
114 | }
115 | }
116 |
117 | /**
118 | * Reads the next line. A line ends with {@code "\n"} or {@code "\r\n"},
119 | * this end of line marker is not included in the result.
120 | *
121 | * @return the next line from the input.
122 | * @throws IOException for underlying {@code InputStream} errors.
123 | * @throws EOFException for the end of source stream.
124 | */
125 | public String readLine() throws IOException {
126 | synchronized (in) {
127 | if (buf == null) {
128 | throw new IOException("LineReader is closed");
129 | }
130 |
131 | // Read more data if we are at the end of the buffered data.
132 | // Though it's an error to read after an exception, we will let {@code fillBuf()}
133 | // throw again if that happens; thus we need to handle end == -1 as well as end == pos.
134 | if (pos >= end) {
135 | fillBuf();
136 | }
137 | // Try to find LF in the buffered data and return the line if successful.
138 | for (int i = pos; i != end; ++i) {
139 | if (buf[i] == LF) {
140 | int lineEnd = (i != pos && buf[i - 1] == CR) ? i - 1 : i;
141 | String res = new String(buf, pos, lineEnd - pos, charset.name());
142 | pos = i + 1;
143 | return res;
144 | }
145 | }
146 |
147 | // Let's anticipate up to 80 characters on top of those already read.
148 | ByteArrayOutputStream out = new ByteArrayOutputStream(end - pos + 80) {
149 | @Override
150 | public String toString() {
151 | int length = (count > 0 && buf[count - 1] == CR) ? count - 1 : count;
152 | try {
153 | return new String(buf, 0, length, charset.name());
154 | } catch (UnsupportedEncodingException e) {
155 | throw new AssertionError(e); // Since we control the charset this will never happen.
156 | }
157 | }
158 | };
159 |
160 | while (true) {
161 | out.write(buf, pos, end - pos);
162 | // Mark unterminated line in case fillBuf throws EOFException or IOException.
163 | end = -1;
164 | fillBuf();
165 | // Try to find LF in the buffered data and return the line if successful.
166 | for (int i = pos; i != end; ++i) {
167 | if (buf[i] == LF) {
168 | if (i != pos) {
169 | out.write(buf, pos, i - pos);
170 | }
171 | pos = i + 1;
172 | return out.toString();
173 | }
174 | }
175 | }
176 | }
177 | }
178 |
179 | public boolean hasUnterminatedLine() {
180 | return end == -1;
181 | }
182 |
183 | /**
184 | * Reads new input data into the buffer. Call only with pos == end or end == -1,
185 | * depending on the desired outcome if the function throws.
186 | */
187 | private void fillBuf() throws IOException {
188 | int result = in.read(buf, 0, buf.length);
189 | if (result == -1) {
190 | throw new EOFException();
191 | }
192 | pos = 0;
193 | end = result;
194 | }
195 | }
196 |
197 |
--------------------------------------------------------------------------------
/src/main/java/com/bumptech/glide/disklrucache/Util.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 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.bumptech.glide.disklrucache;
18 |
19 | import java.io.Closeable;
20 | import java.io.File;
21 | import java.io.IOException;
22 | import java.io.Reader;
23 | import java.io.StringWriter;
24 | import java.nio.charset.Charset;
25 |
26 | /** Junk drawer of utility methods. */
27 | final class Util {
28 | static final Charset US_ASCII = Charset.forName("US-ASCII");
29 | static final Charset UTF_8 = Charset.forName("UTF-8");
30 |
31 | private Util() {
32 | }
33 |
34 | static String readFully(Reader reader) throws IOException {
35 | try {
36 | StringWriter writer = new StringWriter();
37 | char[] buffer = new char[1024];
38 | int count;
39 | while ((count = reader.read(buffer)) != -1) {
40 | writer.write(buffer, 0, count);
41 | }
42 | return writer.toString();
43 | } finally {
44 | reader.close();
45 | }
46 | }
47 |
48 | /**
49 | * Deletes the contents of {@code dir}. Throws an IOException if any file
50 | * could not be deleted, or if {@code dir} is not a readable directory.
51 | */
52 | static void deleteContents(File dir) throws IOException {
53 | File[] files = dir.listFiles();
54 | if (files == null) {
55 | throw new IOException("not a readable directory: " + dir);
56 | }
57 | for (File file : files) {
58 | if (file.isDirectory()) {
59 | deleteContents(file);
60 | }
61 | if (!file.delete()) {
62 | throw new IOException("failed to delete file: " + file);
63 | }
64 | }
65 | }
66 |
67 | static void closeQuietly(/*Auto*/Closeable closeable) {
68 | if (closeable != null) {
69 | try {
70 | closeable.close();
71 | } catch (RuntimeException rethrown) {
72 | throw rethrown;
73 | } catch (Exception ignored) {
74 | }
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/test/java/com/bumptech/glide/disklrucache/DiskLruCacheTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.bumptech.glide.disklrucache;
18 |
19 | import static com.bumptech.glide.disklrucache.DiskLruCache.JOURNAL_FILE;
20 | import static com.bumptech.glide.disklrucache.DiskLruCache.JOURNAL_FILE_BACKUP;
21 | import static com.bumptech.glide.disklrucache.DiskLruCache.MAGIC;
22 | import static com.bumptech.glide.disklrucache.DiskLruCache.VERSION_1;
23 | import static org.fest.assertions.api.Assertions.assertThat;
24 | import static org.hamcrest.core.IsNot.not;
25 | import static org.junit.Assume.assumeThat;
26 |
27 | import org.apache.commons.io.FileUtils;
28 | import org.hamcrest.core.StringStartsWith;
29 | import org.junit.After;
30 | import org.junit.Assert;
31 | import org.junit.Before;
32 | import org.junit.BeforeClass;
33 | import org.junit.Rule;
34 | import org.junit.Test;
35 | import org.junit.rules.TemporaryFolder;
36 | import org.junit.runner.RunWith;
37 | import org.junit.runners.JUnit4;
38 |
39 | import java.io.BufferedReader;
40 | import java.io.File;
41 | import java.io.FileReader;
42 | import java.io.FileWriter;
43 | import java.io.Reader;
44 | import java.io.StringWriter;
45 | import java.io.Writer;
46 | import java.util.ArrayList;
47 | import java.util.Arrays;
48 | import java.util.List;
49 | import java.util.concurrent.TimeUnit;
50 |
51 | @RunWith(JUnit4.class)
52 | public final class DiskLruCacheTest {
53 | private final int appVersion = 100;
54 | private File cacheDir;
55 | private File journalFile;
56 | private File journalBkpFile;
57 | private DiskLruCache cache;
58 |
59 | @Rule public TemporaryFolder tempDir = new TemporaryFolder();
60 |
61 | @BeforeClass
62 | public static void setUpClass() {
63 | assumeThat(System.getProperty("os.name"), not(StringStartsWith.startsWith("Windows")));
64 | }
65 |
66 | @Before public void setUp() throws Exception {
67 | cacheDir = tempDir.newFolder("DiskLruCacheTest");
68 | journalFile = new File(cacheDir, JOURNAL_FILE);
69 | journalBkpFile = new File(cacheDir, JOURNAL_FILE_BACKUP);
70 | for (File file : cacheDir.listFiles()) {
71 | file.delete();
72 | }
73 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
74 | }
75 |
76 | @After public void tearDown() throws Exception {
77 | cache.close();
78 | }
79 |
80 | @Test public void emptyCache() throws Exception {
81 | cache.close();
82 | assertJournalEquals();
83 | }
84 |
85 | @Test public void writeAndReadEntry() throws Exception {
86 | DiskLruCache.Editor creator = cache.edit("k1");
87 | creator.set(0, "ABC");
88 | creator.set(1, "DE");
89 | assertThat(creator.getString(0)).isNull();
90 | assertThat(creator.getString(1)).isNull();
91 | creator.commit();
92 |
93 | DiskLruCache.Value value = cache.get("k1");
94 | assertThat(value.getString(0)).isEqualTo("ABC");
95 | assertThat(value.getLength(0)).isEqualTo(3);
96 | assertThat(value.getString(1)).isEqualTo("DE");
97 | assertThat(value.getLength(1)).isEqualTo(2);
98 | }
99 |
100 | @Test public void readAndWriteEntryAcrossCacheOpenAndClose() throws Exception {
101 | DiskLruCache.Editor creator = cache.edit("k1");
102 | creator.set(0, "A");
103 | creator.set(1, "B");
104 | creator.commit();
105 | cache.close();
106 |
107 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
108 | DiskLruCache.Value value = cache.get("k1");
109 | assertThat(value.getString(0)).isEqualTo("A");
110 | assertThat(value.getLength(0)).isEqualTo(1);
111 | assertThat(value.getString(1)).isEqualTo("B");
112 | assertThat(value.getLength(1)).isEqualTo(1);
113 | }
114 |
115 | @Test public void readAndWriteEntryWithoutProperClose() throws Exception {
116 | DiskLruCache.Editor creator = cache.edit("k1");
117 | creator.set(0, "A");
118 | creator.set(1, "B");
119 | creator.commit();
120 |
121 | // Simulate a dirty close of 'cache' by opening the cache directory again.
122 | DiskLruCache cache2 = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
123 | DiskLruCache.Value value = cache2.get("k1");
124 | assertThat(value.getString(0)).isEqualTo("A");
125 | assertThat(value.getLength(0)).isEqualTo(1);
126 | assertThat(value.getString(1)).isEqualTo("B");
127 | assertThat(value.getLength(1)).isEqualTo(1);
128 | cache2.close();
129 | }
130 |
131 | @Test public void journalWithEditAndPublish() throws Exception {
132 | DiskLruCache.Editor creator = cache.edit("k1");
133 | assertJournalEquals("DIRTY k1"); // DIRTY must always be flushed.
134 | creator.set(0, "AB");
135 | creator.set(1, "C");
136 | creator.commit();
137 | cache.close();
138 | assertJournalEquals("DIRTY k1", "CLEAN k1 2 1");
139 | }
140 |
141 | @Test public void revertedNewFileIsRemoveInJournal() throws Exception {
142 | DiskLruCache.Editor creator = cache.edit("k1");
143 | assertJournalEquals("DIRTY k1"); // DIRTY must always be flushed.
144 | creator.set(0, "AB");
145 | creator.set(1, "C");
146 | creator.abort();
147 | cache.close();
148 | assertJournalEquals("DIRTY k1", "REMOVE k1");
149 | }
150 |
151 | @Test public void unterminatedEditIsRevertedOnClose() throws Exception {
152 | cache.edit("k1");
153 | cache.close();
154 | assertJournalEquals("DIRTY k1", "REMOVE k1");
155 | }
156 |
157 | @Test public void journalDoesNotIncludeReadOfYetUnpublishedValue() throws Exception {
158 | DiskLruCache.Editor creator = cache.edit("k1");
159 | assertThat(cache.get("k1")).isNull();
160 | creator.set(0, "A");
161 | creator.set(1, "BC");
162 | creator.commit();
163 | cache.close();
164 | assertJournalEquals("DIRTY k1", "CLEAN k1 1 2");
165 | }
166 |
167 | @Test public void journalWithEditAndPublishAndRead() throws Exception {
168 | DiskLruCache.Editor k1Creator = cache.edit("k1");
169 | k1Creator.set(0, "AB");
170 | k1Creator.set(1, "C");
171 | k1Creator.commit();
172 | DiskLruCache.Editor k2Creator = cache.edit("k2");
173 | k2Creator.set(0, "DEF");
174 | k2Creator.set(1, "G");
175 | k2Creator.commit();
176 | DiskLruCache.Value k1Value = cache.get("k1");
177 | cache.close();
178 | assertJournalEquals("DIRTY k1", "CLEAN k1 2 1", "DIRTY k2", "CLEAN k2 3 1", "READ k1");
179 | }
180 |
181 | @Test public void cannotOperateOnEditAfterPublish() throws Exception {
182 | DiskLruCache.Editor editor = cache.edit("k1");
183 | editor.set(0, "A");
184 | editor.set(1, "B");
185 | editor.commit();
186 | assertInoperable(editor);
187 | }
188 |
189 | @Test public void cannotOperateOnEditAfterRevert() throws Exception {
190 | DiskLruCache.Editor editor = cache.edit("k1");
191 | editor.set(0, "A");
192 | editor.set(1, "B");
193 | editor.abort();
194 | assertInoperable(editor);
195 | }
196 |
197 | @Test public void explicitRemoveAppliedToDiskImmediately() throws Exception {
198 | DiskLruCache.Editor editor = cache.edit("k1");
199 | editor.set(0, "ABC");
200 | editor.set(1, "B");
201 | editor.commit();
202 | File k1 = getCleanFile("k1", 0);
203 | assertThat(readFile(k1)).isEqualTo("ABC");
204 | cache.remove("k1");
205 | assertThat(k1.exists()).isFalse();
206 | }
207 |
208 | @Test public void openWithDirtyKeyDeletesAllFilesForThatKey() throws Exception {
209 | cache.close();
210 | File cleanFile0 = getCleanFile("k1", 0);
211 | File cleanFile1 = getCleanFile("k1", 1);
212 | File dirtyFile0 = getDirtyFile("k1", 0);
213 | File dirtyFile1 = getDirtyFile("k1", 1);
214 | writeFile(cleanFile0, "A");
215 | writeFile(cleanFile1, "B");
216 | writeFile(dirtyFile0, "C");
217 | writeFile(dirtyFile1, "D");
218 | createJournal("CLEAN k1 1 1", "DIRTY k1");
219 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
220 | assertThat(cleanFile0.exists()).isFalse();
221 | assertThat(cleanFile1.exists()).isFalse();
222 | assertThat(dirtyFile0.exists()).isFalse();
223 | assertThat(dirtyFile1.exists()).isFalse();
224 | assertThat(cache.get("k1")).isNull();
225 | }
226 |
227 | @Test public void openWithInvalidVersionClearsDirectory() throws Exception {
228 | cache.close();
229 | generateSomeGarbageFiles();
230 | createJournalWithHeader(MAGIC, "0", "100", "2", "");
231 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
232 | assertGarbageFilesAllDeleted();
233 | }
234 |
235 | @Test public void openWithInvalidAppVersionClearsDirectory() throws Exception {
236 | cache.close();
237 | generateSomeGarbageFiles();
238 | createJournalWithHeader(MAGIC, "1", "101", "2", "");
239 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
240 | assertGarbageFilesAllDeleted();
241 | }
242 |
243 | @Test public void openWithInvalidValueCountClearsDirectory() throws Exception {
244 | cache.close();
245 | generateSomeGarbageFiles();
246 | createJournalWithHeader(MAGIC, "1", "100", "1", "");
247 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
248 | assertGarbageFilesAllDeleted();
249 | }
250 |
251 | @Test public void openWithInvalidBlankLineClearsDirectory() throws Exception {
252 | cache.close();
253 | generateSomeGarbageFiles();
254 | createJournalWithHeader(MAGIC, "1", "100", "2", "x");
255 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
256 | assertGarbageFilesAllDeleted();
257 | }
258 |
259 | @Test public void openWithInvalidJournalLineClearsDirectory() throws Exception {
260 | cache.close();
261 | generateSomeGarbageFiles();
262 | createJournal("CLEAN k1 1 1", "BOGUS");
263 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
264 | assertGarbageFilesAllDeleted();
265 | assertThat(cache.get("k1")).isNull();
266 | }
267 |
268 | @Test public void openWithInvalidFileSizeClearsDirectory() throws Exception {
269 | cache.close();
270 | generateSomeGarbageFiles();
271 | createJournal("CLEAN k1 0000x001 1");
272 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
273 | assertGarbageFilesAllDeleted();
274 | assertThat(cache.get("k1")).isNull();
275 | }
276 |
277 | @Test public void openWithTruncatedLineDiscardsThatLine() throws Exception {
278 | cache.close();
279 | writeFile(getCleanFile("k1", 0), "A");
280 | writeFile(getCleanFile("k1", 1), "B");
281 | Writer writer = new FileWriter(journalFile);
282 | writer.write(MAGIC + "\n" + VERSION_1 + "\n100\n2\n\nCLEAN k1 1 1"); // no trailing newline
283 | writer.close();
284 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
285 | assertThat(cache.get("k1")).isNull();
286 |
287 | // The journal is not corrupt when editing after a truncated line.
288 | set("k1", "C", "D");
289 | cache.close();
290 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
291 | assertValue("k1", "C", "D");
292 | }
293 |
294 | @Test public void openWithTooManyFileSizesClearsDirectory() throws Exception {
295 | cache.close();
296 | generateSomeGarbageFiles();
297 | createJournal("CLEAN k1 1 1 1");
298 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
299 | assertGarbageFilesAllDeleted();
300 | assertThat(cache.get("k1")).isNull();
301 | }
302 |
303 | @Test public void nullKeyThrows() throws Exception {
304 | try {
305 | cache.edit(null);
306 | Assert.fail();
307 | } catch (NullPointerException expected) {
308 | }
309 | }
310 |
311 | @Test public void createNewEntryWithTooFewValuesFails() throws Exception {
312 | DiskLruCache.Editor creator = cache.edit("k1");
313 | creator.set(1, "A");
314 | try {
315 | creator.commit();
316 | Assert.fail();
317 | } catch (IllegalStateException expected) {
318 | }
319 |
320 | assertThat(getCleanFile("k1", 0).exists()).isFalse();
321 | assertThat(getCleanFile("k1", 1).exists()).isFalse();
322 | assertThat(getDirtyFile("k1", 0).exists()).isFalse();
323 | assertThat(getDirtyFile("k1", 1).exists()).isFalse();
324 | assertThat(cache.get("k1")).isNull();
325 |
326 | DiskLruCache.Editor creator2 = cache.edit("k1");
327 | creator2.set(0, "B");
328 | creator2.set(1, "C");
329 | creator2.commit();
330 | }
331 |
332 | @Test public void revertWithTooFewValues() throws Exception {
333 | DiskLruCache.Editor creator = cache.edit("k1");
334 | creator.set(1, "A");
335 | creator.abort();
336 | assertThat(getCleanFile("k1", 0).exists()).isFalse();
337 | assertThat(getCleanFile("k1", 1).exists()).isFalse();
338 | assertThat(getDirtyFile("k1", 0).exists()).isFalse();
339 | assertThat(getDirtyFile("k1", 1).exists()).isFalse();
340 | assertThat(cache.get("k1")).isNull();
341 | }
342 |
343 | @Test public void updateExistingEntryWithTooFewValuesReusesPreviousValues() throws Exception {
344 | DiskLruCache.Editor creator = cache.edit("k1");
345 | creator.set(0, "A");
346 | creator.set(1, "B");
347 | creator.commit();
348 |
349 | DiskLruCache.Editor updater = cache.edit("k1");
350 | updater.set(0, "C");
351 | updater.commit();
352 |
353 | DiskLruCache.Value value = cache.get("k1");
354 | assertThat(value.getString(0)).isEqualTo("C");
355 | assertThat(value.getLength(0)).isEqualTo(1);
356 | assertThat(value.getString(1)).isEqualTo("B");
357 | assertThat(value.getLength(1)).isEqualTo(1);
358 | }
359 |
360 | @Test public void growMaxSize() throws Exception {
361 | cache.close();
362 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
363 | set("a", "a", "aaa"); // size 4
364 | set("b", "bb", "bbbb"); // size 6
365 | cache.setMaxSize(20);
366 | set("c", "c", "c"); // size 12
367 | assertThat(cache.size()).isEqualTo(12);
368 | }
369 |
370 | @Test public void shrinkMaxSizeEvicts() throws Exception {
371 | cache.close();
372 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 20);
373 | set("a", "a", "aaa"); // size 4
374 | set("b", "bb", "bbbb"); // size 6
375 | set("c", "c", "c"); // size 12
376 | cache.setMaxSize(10);
377 | cache.executorService.shutdown();
378 | cache.executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
379 | assertThat(cache.size()).isEqualTo(8 /* 12 - 4 */);
380 | }
381 |
382 | @Test public void evictOnInsert() throws Exception {
383 | cache.close();
384 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
385 |
386 | set("a", "a", "aaa"); // size 4
387 | set("b", "bb", "bbbb"); // size 6
388 | assertThat(cache.size()).isEqualTo(10);
389 |
390 | // Cause the size to grow to 12 should evict 'A'.
391 | set("c", "c", "c");
392 | cache.flush();
393 | assertThat(cache.size()).isEqualTo(8);
394 | assertAbsent("a");
395 | assertValue("b", "bb", "bbbb");
396 | assertValue("c", "c", "c");
397 |
398 | // Causing the size to grow to 10 should evict nothing.
399 | set("d", "d", "d");
400 | cache.flush();
401 | assertThat(cache.size()).isEqualTo(10);
402 | assertAbsent("a");
403 | assertValue("b", "bb", "bbbb");
404 | assertValue("c", "c", "c");
405 | assertValue("d", "d", "d");
406 |
407 | // Causing the size to grow to 18 should evict 'B' and 'C'.
408 | set("e", "eeee", "eeee");
409 | cache.flush();
410 | assertThat(cache.size()).isEqualTo(10);
411 | assertAbsent("a");
412 | assertAbsent("b");
413 | assertAbsent("c");
414 | assertValue("d", "d", "d");
415 | assertValue("e", "eeee", "eeee");
416 | }
417 |
418 | @Test public void evictOnUpdate() throws Exception {
419 | cache.close();
420 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
421 |
422 | set("a", "a", "aa"); // size 3
423 | set("b", "b", "bb"); // size 3
424 | set("c", "c", "cc"); // size 3
425 | assertThat(cache.size()).isEqualTo(9);
426 |
427 | // Causing the size to grow to 11 should evict 'A'.
428 | set("b", "b", "bbbb");
429 | cache.flush();
430 | assertThat(cache.size()).isEqualTo(8);
431 | assertAbsent("a");
432 | assertValue("b", "b", "bbbb");
433 | assertValue("c", "c", "cc");
434 | }
435 |
436 | @Test public void evictionHonorsLruFromCurrentSession() throws Exception {
437 | cache.close();
438 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
439 | set("a", "a", "a");
440 | set("b", "b", "b");
441 | set("c", "c", "c");
442 | set("d", "d", "d");
443 | set("e", "e", "e");
444 | cache.get("b"); // 'B' is now least recently used.
445 |
446 | // Causing the size to grow to 12 should evict 'A'.
447 | set("f", "f", "f");
448 | // Causing the size to grow to 12 should evict 'C'.
449 | set("g", "g", "g");
450 | cache.flush();
451 | assertThat(cache.size()).isEqualTo(10);
452 | assertAbsent("a");
453 | assertValue("b", "b", "b");
454 | assertAbsent("c");
455 | assertValue("d", "d", "d");
456 | assertValue("e", "e", "e");
457 | assertValue("f", "f", "f");
458 | }
459 |
460 | @Test public void evictionHonorsLruFromPreviousSession() throws Exception {
461 | set("a", "a", "a");
462 | set("b", "b", "b");
463 | set("c", "c", "c");
464 | set("d", "d", "d");
465 | set("e", "e", "e");
466 | set("f", "f", "f");
467 | cache.get("b"); // 'B' is now least recently used.
468 | assertThat(cache.size()).isEqualTo(12);
469 | cache.close();
470 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
471 |
472 | set("g", "g", "g");
473 | cache.flush();
474 | assertThat(cache.size()).isEqualTo(10);
475 | assertAbsent("a");
476 | assertValue("b", "b", "b");
477 | assertAbsent("c");
478 | assertValue("d", "d", "d");
479 | assertValue("e", "e", "e");
480 | assertValue("f", "f", "f");
481 | assertValue("g", "g", "g");
482 | }
483 |
484 | @Test public void cacheSingleEntryOfSizeGreaterThanMaxSize() throws Exception {
485 | cache.close();
486 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
487 | set("a", "aaaaa", "aaaaaa"); // size=11
488 | cache.flush();
489 | assertAbsent("a");
490 | }
491 |
492 | @Test public void cacheSingleValueOfSizeGreaterThanMaxSize() throws Exception {
493 | cache.close();
494 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
495 | set("a", "aaaaaaaaaaa", "a"); // size=12
496 | cache.flush();
497 | assertAbsent("a");
498 | }
499 |
500 | @Test public void constructorDoesNotAllowZeroCacheSize() throws Exception {
501 | try {
502 | DiskLruCache.open(cacheDir, appVersion, 2, 0);
503 | Assert.fail();
504 | } catch (IllegalArgumentException expected) {
505 | }
506 | }
507 |
508 | @Test public void constructorDoesNotAllowZeroValuesPerEntry() throws Exception {
509 | try {
510 | DiskLruCache.open(cacheDir, appVersion, 0, 10);
511 | Assert.fail();
512 | } catch (IllegalArgumentException expected) {
513 | }
514 | }
515 |
516 | @Test public void removeAbsentElement() throws Exception {
517 | cache.remove("a");
518 | }
519 |
520 | @Test public void readingTheSameFileMultipleTimes() throws Exception {
521 | set("a", "a", "b");
522 | DiskLruCache.Value value = cache.get("a");
523 | assertThat(value.getFile(0)).isSameAs(value.getFile(0));
524 | }
525 |
526 | @Test public void rebuildJournalOnRepeatedReads() throws Exception {
527 | set("a", "a", "a");
528 | set("b", "b", "b");
529 | long lastJournalLength = 0;
530 | while (true) {
531 | long journalLength = journalFile.length();
532 | assertValue("a", "a", "a");
533 | assertValue("b", "b", "b");
534 | if (journalLength < lastJournalLength) {
535 | System.out
536 | .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength,
537 | journalLength);
538 | break; // Test passed!
539 | }
540 | lastJournalLength = journalLength;
541 | }
542 | }
543 |
544 | @Test public void rebuildJournalOnRepeatedEdits() throws Exception {
545 | long lastJournalLength = 0;
546 | while (true) {
547 | long journalLength = journalFile.length();
548 | set("a", "a", "a");
549 | set("b", "b", "b");
550 | if (journalLength < lastJournalLength) {
551 | System.out
552 | .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength,
553 | journalLength);
554 | break;
555 | }
556 | lastJournalLength = journalLength;
557 | }
558 |
559 | // Sanity check that a rebuilt journal behaves normally.
560 | assertValue("a", "a", "a");
561 | assertValue("b", "b", "b");
562 | }
563 |
564 | /** @see Issue #28 */
565 | @Test public void rebuildJournalOnRepeatedReadsWithOpenAndClose() throws Exception {
566 | set("a", "a", "a");
567 | set("b", "b", "b");
568 | long lastJournalLength = 0;
569 | while (true) {
570 | long journalLength = journalFile.length();
571 | assertValue("a", "a", "a");
572 | assertValue("b", "b", "b");
573 | cache.close();
574 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
575 | if (journalLength < lastJournalLength) {
576 | System.out
577 | .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength,
578 | journalLength);
579 | break; // Test passed!
580 | }
581 | lastJournalLength = journalLength;
582 | }
583 | }
584 |
585 | /** @see Issue #28 */
586 | @Test public void rebuildJournalOnRepeatedEditsWithOpenAndClose() throws Exception {
587 | long lastJournalLength = 0;
588 | while (true) {
589 | long journalLength = journalFile.length();
590 | set("a", "a", "a");
591 | set("b", "b", "b");
592 | cache.close();
593 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
594 | if (journalLength < lastJournalLength) {
595 | System.out
596 | .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength,
597 | journalLength);
598 | break;
599 | }
600 | lastJournalLength = journalLength;
601 | }
602 | }
603 |
604 | @Test public void restoreBackupFile() throws Exception {
605 | DiskLruCache.Editor creator = cache.edit("k1");
606 | creator.set(0, "ABC");
607 | creator.set(1, "DE");
608 | creator.commit();
609 | cache.close();
610 |
611 | assertThat(journalFile.renameTo(journalBkpFile)).isTrue();
612 | assertThat(journalFile.exists()).isFalse();
613 |
614 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
615 |
616 | DiskLruCache.Value value = cache.get("k1");
617 | assertThat(value.getString(0)).isEqualTo("ABC");
618 | assertThat(value.getLength(0)).isEqualTo(3);
619 | assertThat(value.getString(1)).isEqualTo("DE");
620 | assertThat(value.getLength(1)).isEqualTo(2);
621 |
622 | assertThat(journalBkpFile.exists()).isFalse();
623 | assertThat(journalFile.exists()).isTrue();
624 | }
625 |
626 | @Test public void journalFileIsPreferredOverBackupFile() throws Exception {
627 | DiskLruCache.Editor creator = cache.edit("k1");
628 | creator.set(0, "ABC");
629 | creator.set(1, "DE");
630 | creator.commit();
631 | cache.flush();
632 |
633 | FileUtils.copyFile(journalFile, journalBkpFile);
634 |
635 | creator = cache.edit("k2");
636 | creator.set(0, "F");
637 | creator.set(1, "GH");
638 | creator.commit();
639 | cache.close();
640 |
641 | assertThat(journalFile.exists()).isTrue();
642 | assertThat(journalBkpFile.exists()).isTrue();
643 |
644 | cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
645 |
646 | DiskLruCache.Value valueA = cache.get("k1");
647 | assertThat(valueA.getString(0)).isEqualTo("ABC");
648 | assertThat(valueA.getLength(0)).isEqualTo(3);
649 | assertThat(valueA.getString(1)).isEqualTo("DE");
650 | assertThat(valueA.getLength(1)).isEqualTo(2);
651 |
652 | DiskLruCache.Value valueB = cache.get("k2");
653 | assertThat(valueB.getString(0)).isEqualTo("F");
654 | assertThat(valueB.getLength(0)).isEqualTo(1);
655 | assertThat(valueB.getString(1)).isEqualTo("GH");
656 | assertThat(valueB.getLength(1)).isEqualTo(2);
657 |
658 | assertThat(journalBkpFile.exists()).isFalse();
659 | assertThat(journalFile.exists()).isTrue();
660 | }
661 |
662 | @Test public void openCreatesDirectoryIfNecessary() throws Exception {
663 | cache.close();
664 | File dir = tempDir.newFolder("testOpenCreatesDirectoryIfNecessary");
665 | cache = DiskLruCache.open(dir, appVersion, 2, Integer.MAX_VALUE);
666 | set("a", "a", "a");
667 | assertThat(new File(dir, "a.0").exists()).isTrue();
668 | assertThat(new File(dir, "a.1").exists()).isTrue();
669 | assertThat(new File(dir, "journal").exists()).isTrue();
670 | }
671 |
672 | @Test public void fileDeletedExternally() throws Exception {
673 | set("a", "a", "a");
674 | getCleanFile("a", 1).delete();
675 | assertThat(cache.get("a")).isNull();
676 | }
677 |
678 | @Test public void editSameVersion() throws Exception {
679 | set("a", "a", "a");
680 | DiskLruCache.Value value = cache.get("a");
681 | DiskLruCache.Editor editor = value.edit();
682 | editor.set(1, "a2");
683 | editor.commit();
684 | assertValue("a", "a", "a2");
685 | }
686 |
687 | @Test public void editSnapshotAfterChangeAborted() throws Exception {
688 | set("a", "a", "a");
689 | DiskLruCache.Value value = cache.get("a");
690 | DiskLruCache.Editor toAbort = value.edit();
691 | toAbort.set(0, "b");
692 | toAbort.abort();
693 | DiskLruCache.Editor editor = value.edit();
694 | editor.set(1, "a2");
695 | editor.commit();
696 | assertValue("a", "a", "a2");
697 | }
698 |
699 | @Test public void editSnapshotAfterChangeCommitted() throws Exception {
700 | set("a", "a", "a");
701 | DiskLruCache.Value value = cache.get("a");
702 | DiskLruCache.Editor toAbort = value.edit();
703 | toAbort.set(0, "b");
704 | toAbort.commit();
705 | assertThat(value.edit()).isNull();
706 | }
707 |
708 | @Test public void editSinceEvicted() throws Exception {
709 | cache.close();
710 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
711 | set("a", "aa", "aaa"); // size 5
712 | DiskLruCache.Value value = cache.get("a");
713 | set("b", "bb", "bbb"); // size 5
714 | set("c", "cc", "ccc"); // size 5; will evict 'A'
715 | cache.flush();
716 | assertThat(value.edit()).isNull();
717 | }
718 |
719 | @Test public void editSinceEvictedAndRecreated() throws Exception {
720 | cache.close();
721 | cache = DiskLruCache.open(cacheDir, appVersion, 2, 10);
722 | set("a", "aa", "aaa"); // size 5
723 | DiskLruCache.Value value = cache.get("a");
724 | set("b", "bb", "bbb"); // size 5
725 | set("c", "cc", "ccc"); // size 5; will evict 'A'
726 | set("a", "a", "aaaa"); // size 5; will evict 'B'
727 | cache.flush();
728 | assertThat(value.edit()).isNull();
729 | }
730 |
731 | /** @see Issue #2 */
732 | @Test public void aggressiveClearingHandlesWrite() throws Exception {
733 | FileUtils.deleteDirectory(cacheDir);
734 | set("a", "a", "a");
735 | assertValue("a", "a", "a");
736 | }
737 |
738 | /** @see Issue #2 */
739 | @Test public void aggressiveClearingHandlesEdit() throws Exception {
740 | set("a", "a", "a");
741 | DiskLruCache.Editor a = cache.get("a").edit();
742 | FileUtils.deleteDirectory(cacheDir);
743 | a.set(1, "a2");
744 | a.commit();
745 | }
746 |
747 | @Test public void removeHandlesMissingFile() throws Exception {
748 | set("a", "a", "a");
749 | getCleanFile("a", 0).delete();
750 | cache.remove("a");
751 | }
752 |
753 | /** @see Issue #2 */
754 | @Test public void aggressiveClearingHandlesPartialEdit() throws Exception {
755 | set("a", "a", "a");
756 | set("b", "b", "b");
757 | DiskLruCache.Editor a = cache.get("a").edit();
758 | a.set(0, "a1");
759 | FileUtils.deleteDirectory(cacheDir);
760 | a.set(1, "a2");
761 | a.commit();
762 | assertThat(cache.get("a")).isNull();
763 | }
764 |
765 | /** @see Issue #2 */
766 | @Test public void aggressiveClearingHandlesRead() throws Exception {
767 | FileUtils.deleteDirectory(cacheDir);
768 | assertThat(cache.get("a")).isNull();
769 | }
770 |
771 | private void assertJournalEquals(String... expectedBodyLines) throws Exception {
772 | List