下面列出了java.net.URL#sameFile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean isAlreadyLoadedByAncestor(
final URL url,
HtmlPage page ) {
WebWindow window = page.getEnclosingWindow();
while (window != null) {
if (url.sameFile(window.getEnclosedPage().getWebResponse().getWebRequest().getUrl())) {
return true;
}
if (window == window.getParentWindow()) {
window = null;
} else {
window = window.getParentWindow();
}
}
return false;
}
/**
* Copy sourceURL to destinationFile without doing any byte conversion.
*
* @param sourceURL
* The source URL
* @param destinationFile
* The destination File.
* @return true if the file was copied, false if the file was not copied
* because the sourceURL and the destinationFile refer to the same
* file.
* @exception IOException
* If the source file does not exist.
*/
public static boolean binaryCopyURLToFile(URL sourceURL, File destinationFile) throws IOException {
URL destinationURL = destinationFile.getCanonicalFile().toURI().toURL();
if (sourceURL.sameFile(destinationURL)) {
return false;
}
// If sourceURL is of the form file:./foo, then we need to try again.
File sourceFile = new File(sourceURL.getFile());
// If the sourceURL is not a jar URL, then check to see if we
// have the same file.
// FIXME: should we check for !/ and !\ everywhere?
if ((sourceFile.getPath().indexOf("!/") == -1) && (sourceFile.getPath().indexOf("!\\") == -1)) {
try {
if (sourceFile.getCanonicalFile().toURI().toURL().sameFile(destinationURL)) {
return false;
}
} catch (IOException ex) {
// JNLP Jar urls sometimes throw an exception here.
// IOException constructor does not take a cause
IOException ioException = new IOException("Cannot find canonical file name of '" + sourceFile + "'");
ioException.initCause(ex);
throw ioException;
}
}
_binaryCopyStream(sourceURL.openStream(), destinationFile);
return true;
}
/**
* Check if this jar:file: resource is contained in the
* named resource. Eg <code>jar:file:///a/b/c/foo.jar!/x.html</code> isContainedIn <code>file:///a/b/c/foo.jar</code>
* @param resource
* @return true if resource is contained in the named resource
* @throws MalformedURLException
*/
@Override
public boolean isContainedIn (Resource resource)
throws MalformedURLException
{
String string = _urlString;
int index = string.indexOf("!/");
if (index > 0)
string = string.substring(0,index);
if (string.startsWith("jar:"))
string = string.substring(4);
URL url = new URL(string);
return url.sameFile(resource.getURL());
}
/**
* Check if this jar:file: resource is contained in the
* named resource. Eg <code>jar:file:///a/b/c/foo.jar!/x.html</code> isContainedIn <code>file:///a/b/c/foo.jar</code>
* @param resource
* @return true if resource is contained in the named resource
* @throws MalformedURLException
*/
@Override
public boolean isContainedIn (Resource resource)
throws MalformedURLException
{
String string = _urlString;
int index = string.indexOf("!/");
if (index > 0)
string = string.substring(0,index);
if (string.startsWith("jar:"))
string = string.substring(4);
URL url = new URL(string);
return url.sameFile(resource.getURL());
}
@Override
public void setPage(final URL newPage) throws IOException
{
fireGeneralEvent(new GeneralEvent(this, EventType.page_loading_begin,
newPage, null));
if (newPage == null)
{
// TODO fire general event here
throw new IOException("invalid url");
}
final URL oldPage = getPage();
Object postData = getPostData();
if ((oldPage == null) || !oldPage.sameFile(newPage) || (postData != null))
{
// different url or POST method, load the new content
final InputStream in = getStream(newPage);
// editor kit is set according to content type
EditorKit kit = getEditorKit();
if (kit == null)
{
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
else
{
document = createDocument(kit, newPage);
int p = getAsynchronousLoadPriority(document);
if (p < 0)
{
// load synchro
loadPage(newPage, oldPage, in, document);
}
else
{
// load asynchro
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
loadPage(newPage, oldPage, in, document);
}
});
t.setDaemon(true);
t.start();
}
}
}
else if (oldPage.sameFile(newPage))
{
if (newPage.getRef() != null)
{
final String reference = newPage.getRef();
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
scrollToReference(reference);
}
});
}
}
}