More or less the same as sameFile(URL, URL) but without resolving the host to an IP address for comparing. Additionally we do some path normalization. @param u1 a URL object @param u2 a URL object @return true if u1 and u2 refer to the same file
(final URL u1, final URL u2)
| 1215 | * @return true if u1 and u2 refer to the same file |
| 1216 | */ |
| 1217 | public static boolean sameFile(final URL u1, final URL u2) { |
| 1218 | if (u1 == u2) { |
| 1219 | return true; |
| 1220 | } |
| 1221 | if (u1 == null || u2 == null) { |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | // Compare the protocols. |
| 1226 | final String p1 = u1.getProtocol(); |
| 1227 | final String p2 = u2.getProtocol(); |
| 1228 | if (!(p1 == p2 || (p1 != null && p1.equalsIgnoreCase(p2)))) { |
| 1229 | return false; |
| 1230 | } |
| 1231 | |
| 1232 | // Compare the ports. |
| 1233 | final int port1 = (u1.getPort() == -1) ? u1.getDefaultPort() : u1.getPort(); |
| 1234 | final int port2 = (u2.getPort() == -1) ? u2.getDefaultPort() : u2.getPort(); |
| 1235 | if (port1 != port2) { |
| 1236 | return false; |
| 1237 | } |
| 1238 | |
| 1239 | // Compare the hosts. |
| 1240 | final String h1 = u1.getHost(); |
| 1241 | final String h2 = u2.getHost(); |
| 1242 | if (!(h1 == h2 || (h1 != null && h1.equalsIgnoreCase(h2)))) { |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
| 1246 | // Compare the files. |
| 1247 | String f1 = u1.getFile(); |
| 1248 | if (f1.isEmpty()) { |
| 1249 | f1 = "/"; |
| 1250 | } |
| 1251 | String f2 = u2.getFile(); |
| 1252 | if (f2.isEmpty()) { |
| 1253 | f2 = "/"; |
| 1254 | } |
| 1255 | if (f1.indexOf('.') > 0 || f2.indexOf('.') > 0) { |
| 1256 | try { |
| 1257 | f1 = u1.toURI().normalize().toURL().getFile(); |
| 1258 | f2 = u2.toURI().normalize().toURL().getFile(); |
| 1259 | } |
| 1260 | catch (final RuntimeException e) { |
| 1261 | throw e; |
| 1262 | } |
| 1263 | catch (final Exception ignored) { |
| 1264 | // ignore |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | return Objects.equals(f1, f2); |
| 1269 | } |
| 1270 | |
| 1271 | /** |
| 1272 | * Helper that constructs a normalized url string |