Reputation: 1272
I am using DiskLRUCache by Jake Wharton and am experiencing an issue with creating keys and formatting them to the libraries specific regex.
1) According to the library, my keys must conform to [a-z0-9_-]{1,64}.
2) The code I am using to format my keys:
String key = url.replace(".", "-")
.replaceAll("^[a-z0-9_-]", "")
.toLowerCase();
if(key.length() > 63)
key = key.substring(key.length() - 63, key.length());
Log.d(TAG, "key: " + key);
DiskLruCache.Snapshot snapshot = mDiskCache.get(key);
3) The code DiskLRUCache is using to validate my keys
static final String STRING_KEY_PATTERN = "[a-z0-9_-]{1,120}";
static final Pattern LEGAL_KEY_PATTERN = Pattern.compile(STRING_KEY_PATTERN);
private void validateKey(String key) {
Matcher matcher = LEGAL_KEY_PATTERN.matcher(key);
if (!matcher.matches()) {
throw new IllegalArgumentException("keys must match regex "
+ STRING_KEY_PATTERN + ": \"" + key + "\"");
}
}
4) And here are some of the keys my users are getting that are throwing the IAException:
54 | 00:00:02:227 | D/asset manager key: a9e9b46-e476-fdd1rx0y0w500h325r_330000_000-mp4
59 | 00:00:07:132 | D/asset manager key: 5f54b52-2ef9-3420rx0y0w135h156r_00cc00_120-mp4
64 | 00:00:08:665 | D/asset manager key: 6c705f7-c0b8-1235rx0y0w395h314r_000033_240-mp4
69 | 00:00:08:984 | D/asset manager key: 7cdb2b3-f0f7-2c76rx0y0w500h153r_cccccc_000-mp4
74 | 00:00:09:477 | D/asset manager key: 5f54b52-2ef9-3420rx0y0w135h156r_00cc00_120-mp4
79 | 00:00:14:999 | D/asset manager key: a91dcba-07a4-58ccrx0y0w227h199r_663300_040-mp4
84 | 00:00:18:176 | D/asset manager key: 5aad806-70a0-6426rx0y0w500h200r_333300_060-mp4
89 | 00:00:25:499 | D/asset manager key: 8e7b5b5-eb57-2f6erx0y0w500h333r_cccc99_060-mp4
94 | 00:00:26:262 | D/asset manager key: a1fa904-3f87-4ee7rx0y0w498h281r_330066_280-mp4
99 | 00:00:29:370 | D/asset manager key: a1fa904-b01f-8ca8rx0y0w404h273r_663333_000-mp4
104 | 00:00:33:981 | D/asset manager key: 0d8298c-7922-8914rx0y0w242h140r_3366cc_220-mp4
109 | 00:00:36:839 | D/asset manager key: 2577a4b-88ea-8282rx0y0w500h372r_333300_060-mp4
114 | 00:00:40:294 | D/asset manager key: 6c20180-1454-b7e4rx0y0w500h273r_330066_280-mp4
119 | 00:00:44:582 | D/asset manager key: toilet_flush_ani_ef1405f6-c75a-aa6f-a5f29f1085e8a151-gif
124 | 00:00:45:149 | D/asset manager key: a5097fb-7f16-fea5rx0y0w299h296r_996633_040-mp4
129 | 00:00:48:491 | D/asset manager key: 25773d1-7499-5fecrx0y0w500h210r_996633_040-mp4
134 | 00:00:52:509 | D/asset manager key: 2e78ac2-e675-95b5rx0y0w500h281r_cccccc_000-mp4
139 | 00:00:55:684 | D/asset manager key: a91dcba-c029-01a1rx0y0w380h177r_663300_040-mp4
144 | 00:00:56:848 | D/asset manager key: a215a90-996d-850erx0y0w500h250r_cccccc_000-mp4
196 | 00:00:57:476 | D/asset manager key: 82369cc-0a2f-4be8rx0y0w280h210r_330000_000-mp4
201 | 00:00:58:393 | D/asset manager key: a1fa904-09fe-25berx0y0w320h240r_330000_000-mp4
206 | 00:01:05:755 | D/asset manager key: a3f8891-f017-cf00rx0y0w500h250r_cc9933_040-mp4
211 | 00:01:07:640 | D/asset manager key: 6890e6e-4c44-fa27rx0y0w500h325r_006600_120-mp4
216 | 00:01:10:147 | D/asset manager key: a215a90-2e33-ab4erx0y0w500h375r_996633_040-mp4
221 | 00:01:12:938 | D/asset manager key: 2e78ac2-5541-5c23rx0y0w500h243r_cccccc_000-mp4
226 | 00:01:14:499 | D/asset manager key: 02a9b27-3e20-4a6drx0y0w325h429r_cccccc_000-mp4
231 | 00:01:16:610 | D/asset manager key: a9534a9-2abc-572frx0y0w500h349r_333300_060-mp4
236 | 00:01:18:263 | D/asset manager key: a9f17f7-dceb-46fbrx0y0w50h50r_cc9933_040-mp4
Upvotes: 2
Views: 1207
Reputation: 626794
The issue seems to be with the regex pattern used to sanitize the keys.
When you use .replaceAll("^[a-z0-9_-]", "")
, it removes the first symbol in string: a letter or a digit or an underscore or a hyphen.
To remove all characters other than those in this class, use a negated character class - just place the caret right after the opening square bracket:
String key = url.replace(".", "-")
.replaceAll("[^a-z0-9_-]", "");
Upvotes: 2