Tabnine Logo
URL.toURI
Code IndexAdd Tabnine to your IDE (free)

How to use
toURI
method
in
java.net.URL

Best Java code snippets using java.net.URL.toURI (Showing top 20 results out of 25,083)

Refine searchRefine arrow

  • File.<init>
  • File.exists
  • Test.<init>
  • URL.<init>
  • URL.getProtocol
  • File.getAbsolutePath
  • CodeSource.getLocation
  • ProtectionDomain.getCodeSource
  • Assert.assertEquals
origin: google/guava

 @VisibleForTesting
 static File toFile(URL url) {
  checkArgument(url.getProtocol().equals("file"));
  try {
   return new File(url.toURI()); // Accepts escaped characters like %20.
  } catch (URISyntaxException e) { // URL.toURI() doesn't escape chars.
   return new File(url.getPath()); // Accepts non-escaped chars like space.
  }
 }
}
origin: stackoverflow.com

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
origin: twosigma/beakerx

protected static boolean isValidURL(String urlString) {
 try {
  URL url = new URL(urlString);
  url.toURI();
  return true;
 } catch (Exception exception) {
  return false;
 }
}
origin: apache/flink

public static void checkJarFile(URL jar) throws IOException {
  File jarFile;
  try {
    jarFile = new File(jar.toURI());
  } catch (URISyntaxException e) {
    throw new IOException("JAR file path is invalid '" + jar + "'");
  }
  if (!jarFile.exists()) {
    throw new IOException("JAR file does not exist '" + jarFile.getAbsolutePath() + "'");
  }
  if (!jarFile.canRead()) {
    throw new IOException("JAR file can't be read '" + jarFile.getAbsolutePath() + "'");
  }
  // TODO: Check if proper JAR file
}
origin: commons-io/commons-io

@Test
public void testUTF8FileWindowsBreaks() throws URISyntaxException, IOException {
  final File testFileIso = new File(this.getClass().getResource("/test-file-utf8-win-linebr.bin").toURI());
  reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, UTF_8);
  assertFileWithShrinkingTestLines(reversedLinesFileReader);
}
origin: ReactiveX/RxJava

@Test
public void verify() throws Exception {
  URL u = NoAnonymousInnerClassesTest.class.getResource("/");
  File f = new File(u.toURI());
  String prefix = f.getAbsolutePath();
  int count = 0;
  while (!queue.isEmpty()) {
        for (String s : parts) {
          if (Character.isDigit(s.charAt(0))) {
            String n = f.getAbsolutePath().substring(prefix.length()).replace('\\', '.').replace('/', '.');
            if (n.startsWith(".")) {
              n = n.substring(1);
origin: HotswapProjects/HotswapAgent

  /**
   * Create all required directories in path for a file
   */
  public static void assertDirExists(URL targetFile) {
    File parent = null;
    try {
      parent = new File(targetFile.toURI()).getParentFile();
    } catch (URISyntaxException e) {
      throw new IllegalStateException(e);
    }

    if(!parent.exists() && !parent.mkdirs()){
      throw new IllegalStateException("Couldn't create dir: " + parent);
    }
  }
}
origin: jmxtrans/jmxtrans

private String filePath(String filename) {
  URL fileUrl = getClass().getClassLoader().getResource(filename);
  try {
    return new File(fileUrl.toURI()).getAbsolutePath();
  } catch (URISyntaxException e) {
    return fileUrl.getPath();
  }
}
origin: commons-io/commons-io

@Test
public void testDataIntegrityWithBufferedReader() throws URISyntaxException, IOException {
  final File testFileIso = new File(this.getClass().getResource("/" + fileName).toURI());
  reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, buffSize, encoding);
  final Stack<String> lineStack = new Stack<>();
  bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(testFileIso), encoding));
  String line = null;
  // read all lines in normal order
  while ((line = bufferedReader.readLine()) != null) {
    lineStack.push(line);
  }
  // read in reverse order and compare with lines from stack
  while ((line = reversedLinesFileReader.readLine()) != null) {
    final String lineFromBufferedReader = lineStack.pop();
    assertEquals(lineFromBufferedReader, line);
  }
}
origin: spring-projects/spring-framework

@Test
public void testFileSystemResourceWithFilePath() throws Exception {
  Path filePath = Paths.get(getClass().getResource("Resource.class").toURI());
  Resource resource = new FileSystemResource(filePath);
  doTestResource(resource);
  assertEquals(new FileSystemResource(filePath), resource);
}
origin: jenkinsci/jenkins

private static String getUnmigrationCommandLine(File jenkinsHome) {
  StringBuilder cp = new StringBuilder();
  for (Class<?> c : new Class<?>[] {RunIdMigrator.class, /* TODO how to calculate transitive dependencies automatically? */Charsets.class, WriterOutputStream.class, BuildException.class, FastDateFormat.class}) {
    URL location = c.getProtectionDomain().getCodeSource().getLocation();
    String locationS = location.toString();
    if (location.getProtocol().equals("file")) {
      try {
        locationS = new File(location.toURI()).getAbsolutePath();
      } catch (URISyntaxException x) {
        // never mind
      }
    }
    if (cp.length() > 0) {
      cp.append(File.pathSeparator);
    }
    cp.append(locationS);
  }
  return String.format("java -classpath \"%s\" %s \"%s\"", cp, RunIdMigrator.class.getName(), jenkinsHome);
}
origin: ehcache/ehcache3

@Test
public void testURL() throws Exception {
 URL url = new URL("http://www.cheese.com/asiago");
 Properties props = new Properties();
 props.put(DefaultConfigurationResolver.DEFAULT_CONFIG_PROPERTY_NAME, url);
 URI resolved = DefaultConfigurationResolver.resolveConfigURI(props);
 assertEquals(url.toURI(), resolved);
}
origin: micronaut-projects/micronaut-core

private Optional<? extends FileCustomizableResponseType> matchFile(String path) {
  Optional<URL> optionalUrl = staticResourceResolver.resolve(path);
  if (optionalUrl.isPresent()) {
    try {
      URL url = optionalUrl.get();
      if (url.getProtocol().equals("file")) {
        File file = Paths.get(url.toURI()).toFile();
        if (file.exists() && !file.isDirectory() && file.canRead()) {
          return Optional.of(new NettySystemFileCustomizableResponseType(file));
        }
      }
      return Optional.of(new NettyStreamedFileCustomizableResponseType(url));
    } catch (URISyntaxException e) {
      //no-op
    }
  }
  return Optional.empty();
}
origin: spring-projects/spring-framework

@Test
public void testFileSystemResourceWithImport() throws URISyntaxException {
  String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath();
  DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
  new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file));
  // comes from "resourceImport.xml"
  xbf.getBean("resource1", ResourceTestBean.class);
  // comes from "resource.xml"
  xbf.getBean("resource2", ResourceTestBean.class);
}
origin: wildfly/wildfly

@Override
public Path getFilePath() {
  if (url.getProtocol().equals("file")) {
    try {
      return Paths.get(url.toURI());
    } catch (URISyntaxException e) {
      return null;
    }
  }
  return null;
}
origin: stagemonitor/stagemonitor

  public static File getFile(String classPathLocation) throws IOException, URISyntaxException {
    URL resource = PropertyFileConfigurationSource.class.getClassLoader().getResource(classPathLocation);
    if (resource == null) {
      resource = new URL("file://" + classPathLocation);
    }
    if (!"file".equals(resource.getProtocol())) {
      throw new IOException("Saving to property files inside a war, ear or jar is not possible.");
    }
    return new File(resource.toURI());
  }
}
origin: dropwizard/dropwizard

  @Override
  public String toString() {
    final URL location = klass.getProtectionDomain().getCodeSource().getLocation();
    try {
      final String jar = new File(location.toURI()).getName();
      if (jar.endsWith(".jar")) {
        return jar;
      }
      return "project.jar";
    } catch (Exception ignored) {
      return "project.jar";
    }
  }
}
origin: commons-io/commons-io

@Test
public void testUTF16LEFile() throws URISyntaxException, IOException {
  final File testFileUTF16LE = new File(this.getClass().getResource("/test-file-utf16le.bin").toURI());
  reversedLinesFileReader = new ReversedLinesFileReader(testFileUTF16LE, testParamBlockSize, "UTF-16LE");
  assertFileWithShrinkingTestLines(reversedLinesFileReader);
}
origin: SonarSource/sonarqube

@Test
public void binary_file_with_unmappable_character() throws Exception {
 File woff = new File(this.getClass().getResource("glyphicons-halflings-regular.woff").toURI());
 Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(woff), StandardCharsets.UTF_8, woff.getAbsolutePath());
 assertThat(metadata.lines()).isEqualTo(135);
 assertThat(metadata.nonBlankLines()).isEqualTo(133);
 assertThat(metadata.hash()).isNotEmpty();
 assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("Invalid character encountered in file");
 assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains(
  "glyphicons-halflings-regular.woff at line 1 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.");
}
origin: ronmamo/reflections

/**try to get {@link java.io.File} from url*/
public static @Nullable java.io.File getFile(URL url) {
  java.io.File file;
  String path;
  try {
    path = url.toURI().getSchemeSpecificPart();
    if ((file = new java.io.File(path)).exists()) return file;
  } catch (URISyntaxException e) {
  }
  try {
    path = URLDecoder.decode(url.getPath(), "UTF-8");
    if (path.contains(".jar!")) path = path.substring(0, path.lastIndexOf(".jar!") + ".jar".length());
    if ((file = new java.io.File(path)).exists()) return file;
  } catch (UnsupportedEncodingException e) {
  }
  try {
    path = url.toExternalForm();
    if (path.startsWith("jar:")) path = path.substring("jar:".length());
    if (path.startsWith("wsjar:")) path = path.substring("wsjar:".length());
    if (path.startsWith("file:")) path = path.substring("file:".length());
    if (path.contains(".jar!")) path = path.substring(0, path.indexOf(".jar!") + ".jar".length());
    if (path.contains(".war!")) path = path.substring(0, path.indexOf(".war!") + ".war".length());
    if ((file = new java.io.File(path)).exists()) return file;
    path = path.replace("%20", " ");
    if ((file = new java.io.File(path)).exists()) return file;
  } catch (Exception e) {
  }
  return null;
}

java.netURLtoURI

Javadoc

Returns the URI equivalent to this URL.

Popular methods of URL

  • <init>
  • openStream
    Opens a connection to this URL and returns anInputStream for reading from that connection. This meth
  • openConnection
    Returns a new connection to the resource referred to by this URL.
  • toString
    Constructs a string representation of this URL. The string is created by calling the toExternalForm
  • getPath
    Gets the path part of this URL.
  • getProtocol
    Gets the protocol name of this URL.
  • getFile
    Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the c
  • toExternalForm
    Constructs a string representation of this URL. The string is created by calling the toExternalForm
  • getHost
    Gets the host name of this URL, if applicable. The format of the host conforms to RFC 2732, i.e. for
  • getPort
    Gets the port number of this URL.
  • getQuery
    Gets the query part of this URL.
  • equals
    Compares this URL for equality with another object. If the given object is not a URL then this metho
  • getQuery,
  • equals,
  • getRef,
  • getUserInfo,
  • getAuthority,
  • hashCode,
  • getDefaultPort,
  • getContent,
  • setURLStreamHandlerFactory

Popular in Java

  • Finding current android device location
  • getSharedPreferences (Context)
  • getResourceAsStream (ClassLoader)
  • setRequestProperty (URLConnection)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • Color (java.awt)
    The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary
  • Stack (java.util)
    Stack is a Last-In/First-Out(LIFO) data structure which represents a stack of objects. It enables u
  • ImageIO (javax.imageio)
  • JTextField (javax.swing)
  • Runner (org.openjdk.jmh.runner)
  • Top PhpStorm plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now