Java Code Examples for java.net.URL#hashCode()

The following examples show how to use java.net.URL#hashCode() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: uyuni   File: TestUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
/**
 * 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;
}
 
Example 3
/**
 * 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;
}
 
Example 4
Source Project: ghidra   File: HelpLocation.java    License: Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 5
Source Project: netbeans   File: NbinstURLMapperTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source Project: j2objc   File: URLTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 7
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;
}
 
Example 8
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;
}
 
Example 9
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;
}
 
Example 10
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;
}
 
Example 11
Source Project: consulo   File: WebImageUrlCache.java    License: Apache License 2.0 4 votes vote down vote up
public static int hashCode(URL url) {
  int i = url.hashCode();
  ourURLCache.putIfAbsent(i, url);
  return i;
}
 
Example 12
Source Project: Bytecoder   File: MimeLauncher.java    License: Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 13
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;
}
 
Example 14
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;
}
 
Example 15
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;
}
 
Example 16
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;
}
 
Example 17
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;
}
 
Example 18
static int f(URL u) {
    return u.hashCode();
}
 
Example 19
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;
}
 
Example 20
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;
}