|
53 | 53 | */
|
54 | 54 | public record NestedLocation(Path path, String nestedEntryName) {
|
55 | 55 |
|
56 |
| - private static final Map<String, NestedLocation> cache = new ConcurrentHashMap<>(); |
| 56 | + private static final Map<String, NestedLocation> locationCache = new ConcurrentHashMap<>(); |
| 57 | + |
| 58 | + private static final Map<String, Path> pathCache = new ConcurrentHashMap<>(); |
57 | 59 |
|
58 | 60 | public NestedLocation(Path path, String nestedEntryName) {
|
59 | 61 | if (path == null) {
|
@@ -89,35 +91,37 @@ public static NestedLocation fromUri(URI uri) {
|
89 | 91 | return parse(uri.getSchemeSpecificPart());
|
90 | 92 | }
|
91 | 93 |
|
92 |
| - static NestedLocation parse(String path) { |
93 |
| - if (path == null || path.isEmpty()) { |
94 |
| - throw new IllegalArgumentException("'path' must not be empty"); |
| 94 | + static NestedLocation parse(String location) { |
| 95 | + if (location == null || location.isEmpty()) { |
| 96 | + throw new IllegalArgumentException("'location' must not be empty"); |
95 | 97 | }
|
96 |
| - int index = path.lastIndexOf("/!"); |
97 |
| - return cache.computeIfAbsent(path, (l) -> create(index, l)); |
| 98 | + return locationCache.computeIfAbsent(location, (key) -> create(location)); |
98 | 99 | }
|
99 | 100 |
|
100 |
| - private static NestedLocation create(int index, String location) { |
| 101 | + private static NestedLocation create(String location) { |
| 102 | + int index = location.lastIndexOf("/!"); |
101 | 103 | String locationPath = (index != -1) ? location.substring(0, index) : location;
|
102 |
| - if (isWindows() && !isUncPath(location)) { |
103 |
| - while (locationPath.startsWith("/")) { |
104 |
| - locationPath = locationPath.substring(1, locationPath.length()); |
105 |
| - } |
106 |
| - } |
107 | 104 | String nestedEntryName = (index != -1) ? location.substring(index + 2) : null;
|
108 |
| - return new NestedLocation((!locationPath.isEmpty()) ? Path.of(locationPath) : null, nestedEntryName); |
| 105 | + return new NestedLocation((!locationPath.isEmpty()) ? asPath(locationPath) : null, nestedEntryName); |
109 | 106 | }
|
110 | 107 |
|
111 |
| - private static boolean isWindows() { |
112 |
| - return File.separatorChar == '\\'; |
| 108 | + private static Path asPath(String locationPath) { |
| 109 | + return pathCache.computeIfAbsent(locationPath, (key) -> { |
| 110 | + if (isWindows() && locationPath.length() > 2 && locationPath.charAt(2) == ':') { |
| 111 | + // Use the same logic as Java's internal WindowsUriSupport class |
| 112 | + return Path.of(locationPath.substring(1)); |
| 113 | + } |
| 114 | + return Path.of(locationPath); |
| 115 | + }); |
113 | 116 | }
|
114 | 117 |
|
115 |
| - private static boolean isUncPath(String input) { |
116 |
| - return !input.contains(":"); |
| 118 | + private static boolean isWindows() { |
| 119 | + return File.separatorChar == '\\'; |
117 | 120 | }
|
118 | 121 |
|
119 | 122 | static void clearCache() {
|
120 |
| - cache.clear(); |
| 123 | + locationCache.clear(); |
| 124 | + pathCache.clear(); |
121 | 125 | }
|
122 | 126 |
|
123 | 127 | }
|
0 commit comments