daily pastebin goal
0%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example
  2.  
  3. import org.junit.Test
  4. import java.net.URI
  5. import java.net.URL
  6. import java.nio.file.FileSystem
  7. import java.nio.file.FileSystemNotFoundException
  8. import java.nio.file.FileSystems
  9. import java.nio.file.Files
  10. import java.nio.file.Paths
  11.  
  12. class FooBar {
  13.     @Test
  14.     fun `test walk`() {
  15.         fun dump(fs: FileSystem, uri: URI) = Files.walk(fs.provider().getPath(uri)).forEach(::println)
  16.  
  17.         // Filesystem walking...
  18.         run {
  19.             val cwd = Paths.get("")
  20.             val uri = cwd.toUri()
  21.             val fs = cwd.fileSystem
  22.  
  23.             println(uri.path)
  24.             dump(fs, uri)
  25.         }
  26.  
  27.         // You might imagine the URI above could be used in the logic below but for some reason the path
  28.         // component of the URI passed to FileSystems.getFileSystem(uri) must be "/" if the scheme is "file"
  29.         // but not if it's "jar". So it seems you can't easily write a scheme agnostic solution :(
  30.  
  31.         // Jar walking...
  32.         run {
  33.             val pkg = URL("jar:file:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar!/java/lang")
  34.             val uri = pkg.toURI()
  35.             val fs = try {
  36.                 FileSystems.getFileSystem(uri)
  37.             } catch (e: FileSystemNotFoundException) {
  38.                 FileSystems.newFileSystem(uri, mapOf<String, Any>())
  39.             }
  40.  
  41.             println(uri.path)
  42.             dump(fs, uri)
  43.         }
  44.        
  45.         // Note that there's no easy way to get a URL for a package rather than a class.
  46.         println(this.javaClass.getResource("/java/lang/String.class"))
  47.         // The above is fine but the following all return null:
  48.         println(this.javaClass.getResource("/java/lang/"))
  49.         println(this.javaClass.getResource("/java/lang"))
  50.         println(this.javaClass.getResource("java/lang/"))
  51.         println(this.javaClass.getResource("java/lang"))
  52.         println(this.javaClass.classLoader.getResource("/java/lang/"))
  53.         println(this.javaClass.classLoader.getResource("/java/lang"))
  54.         println(this.javaClass.classLoader.getResource("java/lang/"))
  55.         println(this.javaClass.classLoader.getResource("java/lang"))
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top