下面列出了java.net.URL#hashCode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* method to find a file relative to the calling class. primarily
* useful when writing tests that need access to external data.
* this lets you put the data relative to the test class file.
*
* @param path the path, relative to caller's location
* @return URL a URL referencing the file
* @throws ClassNotFoundException if the calling class can not be found
* (i.e., should not happen)
* @throws IOException if the specified file in an archive (eg. jar) and
* it cannot be copied to a temporary location
*/
public static URL findTestData(String path) throws ClassNotFoundException, IOException {
Throwable t = new Throwable();
StackTraceElement[] ste = t.getStackTrace();
String className = ste[1].getClassName();
Class clazz = Class.forName(className);
URL ret = clazz.getResource(path);
if (ret.toString().contains("!")) { // file is from an archive
String tempPath = "/tmp/" + filePrefix + ret.hashCode();
InputStream input = clazz.getResourceAsStream(path);
OutputStream output = new FileOutputStream(tempPath);
IOUtils.copy(input, output);
return new File(tempPath).toURI().toURL();
}
return ret;
}
/**
* method to find a file relative to the calling class. primarily
* useful when writing tests that need access to external data.
* this lets you put the data relative to the test class file.
*
* @param path the path, relative to caller's location
* @return URL a URL referencing the file
* @throws ClassNotFoundException if the calling class can not be found
* (i.e., should not happen)
* @throws IOException if the specified file in an archive (eg. jar) and
* it cannot be copied to a temporary location
*/
public static URL findTestData(String path) throws ClassNotFoundException, IOException {
Throwable t = new Throwable();
StackTraceElement[] ste = t.getStackTrace();
String className = ste[1].getClassName();
Class clazz = Class.forName(className);
URL ret = clazz.getResource(path);
if (ret.toString().contains("!")) { // file is from an archive
String tempPath = "/tmp/" + filePrefix + ret.hashCode();
InputStream input = clazz.getResourceAsStream(path);
OutputStream output = new FileOutputStream(tempPath);
IOUtils.copy(input, output);
return new File(tempPath).toURI().toURL();
}
return ret;
}
/**
* Provides the hash calculation
* @return an <tt>int</tt> suitable for hash table indexing
*/
@Override
protected int hashCode(URL u) {
int h = 0;
if (PROTOCOL.equals(u.getProtocol())) {
final String host = u.getHost();
if (host != null)
h = host.hashCode();
final String file = u.getFile();
if (file != null)
h += file.hashCode();
h += u.getPort();
} else {
h = u.hashCode();
}
return h;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((anchor == null) ? 0 : anchor.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((topic == null) ? 0 : topic.hashCode());
URL helpURL = getURL();
result = prime * result + ((helpURL == null) ? 0 : helpURL.hashCode());
return result;
}
public void testURLNoInetAccess() throws MalformedURLException, IOException {
URL url1 = new URL ("nbinst://test-module/modules/test.txt"); // NOI18N
URL url2 = new URL ("nbinst://foo-module/modules/test.txt"); // NOI18N
SecurityManager defaultManager = System.getSecurityManager();
System.setSecurityManager(new InetSecurityManager());
try {
// make sure we do not try to resolve host name
url1.hashCode();
url1.equals(url2);
testURLConnection();
} finally {
System.setSecurityManager(defaultManager);
}
}
public void testHashCodeAndEqualsDoesNotPerformNetworkIo() throws Exception {
final BlockGuard.Policy oldPolicy = BlockGuard.getThreadPolicy();
BlockGuard.setThreadPolicy(new BlockGuard.Policy() {
@Override
public void onWriteToDisk() {
fail("Blockguard.Policy.onWriteToDisk");
}
@Override
public void onReadFromDisk() {
fail("Blockguard.Policy.onReadFromDisk");
}
@Override
public void onNetwork() {
fail("Blockguard.Policy.onNetwork");
}
@Override
public int getPolicyMask() {
return 0;
}
});
try {
URL url = new URL("http://www.google.com/");
URL url2 = new URL("http://www.nest.com/");
url.equals(url2);
url2.hashCode();
} finally {
BlockGuard.setThreadPolicy(oldPolicy);
}
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
public static int hashCode(URL url) {
int i = url.hashCode();
ourURLCache.putIfAbsent(i, url);
return i;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
static int f(URL u) {
return u.hashCode();
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}
protected String getTempFileName(URL url, String template) {
String tempFilename = template;
// Replace all but last occurrance of "%s" with timestamp to insure
// uniqueness. There's a subtle behavior here: if there is anything
// _after_ the last "%s" we need to append it so that unusual launch
// strings that have the datafile in the middle can still be used.
int wildcard = tempFilename.lastIndexOf("%s");
String prefix = tempFilename.substring(0, wildcard);
String suffix = "";
if (wildcard < tempFilename.length() - 2) {
suffix = tempFilename.substring(wildcard + 2);
}
long timestamp = System.currentTimeMillis()/1000;
int argIndex = 0;
while ((argIndex = prefix.indexOf("%s")) >= 0) {
prefix = prefix.substring(0, argIndex)
+ timestamp
+ prefix.substring(argIndex + 2);
}
// Add a file name and file-extension if known
String filename = url.getFile();
String extension = "";
int dot = filename.lastIndexOf('.');
// BugId 4084826: Temp MIME file names not always valid.
// Fix: don't allow slashes in the file name or extension.
if (dot >= 0 && dot > filename.lastIndexOf('/')) {
extension = filename.substring(dot);
}
filename = "HJ" + url.hashCode();
tempFilename = prefix + filename + timestamp + extension + suffix;
return tempFilename;
}