nl_tokens: Given a URL check if its a jar url ( jar : <url > ! / archive ) and if it is extract the archive entry into the given dest directory and return a file URL to its location . If jarURL is not a jar url then it is simply returned as the URL for the jar .
pl_tokens: public static URL extractNestedJar ( URL jarURL , File dest ) throws IOException { // This may not be a jar URL so validate the protocol  if ( jarURL . getProtocol ( ) . equals ( "jar" ) == false ) return jarURL ; String destPath = dest . getAbsolutePath ( ) ; URLConnection urlConn = jarURL . openConnection ( ) ; JarURLConnection jarConn = ( JarURLConnection ) urlConn ; // Extract the archive to dest/jarName-contents/archive String parentArchiveName = jarConn . getJarFile ( ) . getName ( ) ; // Find the longest common prefix between destPath and parentArchiveName int length = Math . <mask> ( destPath . length ( ) , parentArchiveName . length ( ) ) ; int n = 0 ; while ( n < length ) { char a = destPath . charAt ( n ) ; char b = parentArchiveName . charAt ( n ) ; if ( a != b ) break ; n ++ ; } // Remove any common prefix from parentArchiveName parentArchiveName = parentArchiveName . substring ( n ) ; File archiveDir = new File ( dest , parentArchiveName + "-contents" ) ; if ( archiveDir . exists ( ) == false && archiveDir . mkdirs ( ) == false ) throw new IOException ( "Failed to create contents directory for archive, path=" + archiveDir . getAbsolutePath ( ) ) ; String archiveName = jarConn . getEntryName ( ) ; File archiveFile = new File ( archiveDir , archiveName ) ; File archiveParentDir = archiveFile . getParentFile ( ) ; if ( archiveParentDir . exists ( ) == false && archiveParentDir . mkdirs ( ) == false ) throw new IOException ( "Failed to create parent directory for archive, path=" + archiveParentDir . getAbsolutePath ( ) ) ; InputStream archiveIS = jarConn . getInputStream ( ) ; FileOutputStream fos = new FileOutputStream ( archiveFile ) ; BufferedOutputStream bos = new BufferedOutputStream ( fos ) ; byte [ ] buffer = new byte [ 4096 ] ; int read ; while ( ( read = archiveIS . read ( buffer ) ) > 0 ) { bos . write ( buffer , 0 , read ) ; } archiveIS . close ( ) ; bos . close ( ) ; // Return the file url to the extracted jar return archiveFile . toURL ( ) ; }