Class IOUtil
- java.lang.Object
-
- com.jogamp.common.util.IOUtil
-
public class IOUtil extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIOUtil.ClassResourcesHelper compound associating a class instance and resource paths to beresolvedat a later time.static classIOUtil.StreamMonitor
-
Field Summary
Fields Modifier and Type Field Description static booleanDEBUGstatic PatternpatternSpaceEncstatic StringtmpSubDirSubdirectory within platform's temporary root directory where all JogAmp related temp files are being stored:jogamp
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringBuilderappendCharStream(StringBuilder sb, Reader r)static StringcleanPathString(String path)static voidclose(Closeable stream, boolean throwRuntimeException)static IOExceptionclose(Closeable stream, IOException[] saveOneIfFree, PrintStream dumpExcess)Helper to simplify closingCloseables.static byte[]copyStream2ByteArray(InputStream stream)Copy the complete specified input stream to a byte array, which is being returned.static ByteBuffercopyStream2ByteBuffer(InputStream stream)Copy the complete specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned.static ByteBuffercopyStream2ByteBuffer(InputStream stream, int initialCapacity)Copy the complete specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned.static intcopyStream2File(InputStream in, File outFile)Copy the complete specified input stream to the specified output file.static intcopyStream2Stream(int bufferSize, InputStream in, OutputStream out)Copy the complete specified input stream to the specified output stream.static intcopyStream2Stream(InputStream in, OutputStream out)Copy the complete specified input stream to the specified output stream.static ByteBuffercopyStreamChunk2ByteBuffer(InputStream stream, int skipBytes, int byteCount)Copy the specified input stream chunk to a NIO ByteBuffer w/ native byte order, which is being returned.static intcopyURLConn2File(URLConnection conn, File outFile)Copy the complete specified URL resource to the specified output file.static FilecreateTempFile(String prefix, String suffix, boolean executable)UtilizingFile.createTempFile(String, String, File)usinggetTempDir(boolean)as the directory parameter, ie.static ArrayList<String>filesOf(List<String> paths, List<Pattern> excludes, List<Pattern> includes)Retrieve the list of all filenames traversing through given pathsstatic StringgetBasename(String fname)Returns the basename of the given fname w/o directory partstatic StringgetClassFileName(String clazzBinName)static URLgetClassURL(String clazzBinName, ClassLoader cl)static StringgetDirname(String fname)Returns unified '/' dirname including the last '/'static FileOutputStreamgetFileOutputStream(File file, boolean allowOverwrite)static StringgetFileSuffix(File file)Returns the lowercase suffix of the given file name (the text after the last '.' in the file name).static StringgetFileSuffix(String filename)Returns the lowercase suffix of the given file name (the text after the last '.' in the file name).static StringgetParentOf(String path)static StringgetRelativeOf(File baseLocation, String relativeFile)Generates a path for the 'relativeFile' relative to the 'baseLocation'.static URLConnectiongetResource(String resourcePath, ClassLoader cl)Locating a resource using the ClassLoader's facilities.static URLConnectiongetResource(String resourcePath, ClassLoader classLoader, Class<?> relContext)Locating a resource usinggetResource(String, ClassLoader): relative:relContext's package name-path plusresourcePathviaclassLoader.static FilegetTempDir(boolean executable)Returns a platform independent writable directory for temporary files consisting of the platform'stemp-root+tmpSubDir, e.g.static StringgetUriFilePathOrASCII(Uri uri)static URLConnectionopenURL(URL url)Returns the connected URLConnection, or null if not url is not availablestatic URLConnectionopenURL(URL url, String dbgmsg)Returns the connected URLConnection, or null if not url is not availablestatic Stringslashify(String path, boolean startWithSlash, boolean endWithSlash)static FiletestDir(File dir, boolean create, boolean executable)Returns the directorydir, which is processed and tested as described below.static booleantestDirExec(File dir)Returns true if the givendirexists, and is a directory, and is writeable, and files can be executed from the directorystatic booleantestFile(File file, boolean shallBeDir, boolean shallBeWritable)Test whetherfileexists and matches the given requirements
-
-
-
Field Detail
-
DEBUG
public static final boolean DEBUG
-
tmpSubDir
public static final String tmpSubDir
Subdirectory within platform's temporary root directory where all JogAmp related temp files are being stored:jogamp- See Also:
- Constant Field Values
-
patternSpaceEnc
public static final Pattern patternSpaceEnc
-
-
Method Detail
-
copyURLConn2File
public static int copyURLConn2File(URLConnection conn, File outFile) throws IOException
Copy the complete specified URL resource to the specified output file. The total number of bytes written is returned.- Parameters:
conn- the open URLConnectionoutFile- the destination- Returns:
- Throws:
IOException
-
copyStream2File
public static int copyStream2File(InputStream in, File outFile) throws IOException
Copy the complete specified input stream to the specified output file. The total number of bytes written is returned.- Parameters:
in- the sourceoutFile- the destination- Returns:
- Throws:
IOException
-
copyStream2Stream
public static int copyStream2Stream(InputStream in, OutputStream out) throws IOException
Copy the complete specified input stream to the specified output stream. The total number of bytes written is returned.- Parameters:
in- the sourceout- the destination- Returns:
- Throws:
IOException
-
copyStream2Stream
public static int copyStream2Stream(int bufferSize, InputStream in, OutputStream out) throws IOExceptionCopy the complete specified input stream to the specified output stream. The total number of bytes written is returned.- Parameters:
bufferSize- the intermediate buffer size, should beMachineDataInfo.pageSizeInBytes()for best performance.in- the sourceout- the destination- Returns:
- Throws:
IOException
-
appendCharStream
public static StringBuilder appendCharStream(StringBuilder sb, Reader r) throws IOException
- Throws:
IOException
-
copyStream2ByteArray
public static byte[] copyStream2ByteArray(InputStream stream) throws IOException
Copy the complete specified input stream to a byte array, which is being returned.- Throws:
IOException
-
copyStream2ByteBuffer
public static ByteBuffer copyStream2ByteBuffer(InputStream stream) throws IOException
Copy the complete specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned.- Parameters:
stream- input stream, which will be wrapped into a BufferedInputStream, if not already done.- Throws:
IOException
-
copyStream2ByteBuffer
public static ByteBuffer copyStream2ByteBuffer(InputStream stream, int initialCapacity) throws IOException
Copy the complete specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned.- Parameters:
stream- input stream, which will be wrapped into a BufferedInputStream, if not already done.initialCapacity- initial buffer capacity in bytes, if < currently available bytes, initial buffer capacity is set to currently available bytes.- Throws:
IOException
-
copyStreamChunk2ByteBuffer
public static ByteBuffer copyStreamChunk2ByteBuffer(InputStream stream, int skipBytes, int byteCount) throws IOException
Copy the specified input stream chunk to a NIO ByteBuffer w/ native byte order, which is being returned.- Parameters:
stream- input stream, which will be wrapped into a BufferedInputStream, if not already done.skipBytes- initial bytes to skip from input stream.byteCount- bytes to copy starting after skipBytes.- Throws:
IOException
-
slashify
public static String slashify(String path, boolean startWithSlash, boolean endWithSlash) throws URISyntaxException
- Parameters:
path-startWithSlash-endWithSlash-- Returns:
- Throws:
URISyntaxException- if path is empty or has no parent directory available while resolving../
-
getFileSuffix
public static String getFileSuffix(File file)
Returns the lowercase suffix of the given file name (the text after the last '.' in the file name). Returns null if the file name has no suffix. Only operates on the given file name; performs no I/O operations.- Parameters:
file- name of the file- Returns:
- lowercase suffix of the file name
- Throws:
NullPointerException- if file is null
-
getFileSuffix
public static String getFileSuffix(String filename)
Returns the lowercase suffix of the given file name (the text after the last '.' in the file name). Returns null if the file name has no suffix. Only operates on the given file name; performs no I/O operations.- Parameters:
filename- name of the file- Returns:
- lowercase suffix of the file name
- Throws:
NullPointerException- if filename is null
-
getFileOutputStream
public static FileOutputStream getFileOutputStream(File file, boolean allowOverwrite) throws IOException
- Parameters:
file-allowOverwrite-- Returns:
- outputStream The resulting output stream
- Throws:
IOException- if the file already exists andallowOverwriteis false, the classFileOutputStreamis not accessible or the user does not have sufficient rights to access the local filesystem.
-
getClassURL
public static URL getClassURL(String clazzBinName, ClassLoader cl) throws IOException
- Parameters:
clazzBinName- com.jogamp.common.util.cache.TempJarCachecl- ClassLoader to locate the JarFile- Returns:
- jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
- Throws:
IOException- if the jar file could not been found by the ClassLoader
-
getBasename
public static String getBasename(String fname) throws URISyntaxException
Returns the basename of the given fname w/o directory part- Throws:
URISyntaxException- if path is empty or has no parent directory available while resolving../
-
getDirname
public static String getDirname(String fname) throws URISyntaxException
Returns unified '/' dirname including the last '/'- Throws:
URISyntaxException- if path is empty or has no parent directory available while resolving../
-
getResource
public static URLConnection getResource(String resourcePath, ClassLoader classLoader, Class<?> relContext)
Locating a resource usinggetResource(String, ClassLoader):- relative:
relContext's package name-path plusresourcePathviaclassLoader. This allows locations relative to JAR- and other URLs. TheresourcePathmay start with../to navigate to parent folder. This attempt is skipped ifrelContextisnull. - absolute:
resourcePathas is viaclassLoader.
Returns the resolved and open URLConnection or null if not found.
- Parameters:
resourcePath- the resource path to locate relative or absoluteclassLoader- the optionalClassLoader, recommendedrelContext- relative context, i.e. position, of theresourcePath, to perform the relative lookup, if notnull.- See Also:
getResource(String, ClassLoader),ClassLoader.getResource(String),ClassLoader.getSystemResource(String)
- relative:
-
getResource
public static URLConnection getResource(String resourcePath, ClassLoader cl)
Locating a resource using the ClassLoader's facilities.Returns the resolved and connected URLConnection or null if not found.
-
getRelativeOf
public static String getRelativeOf(File baseLocation, String relativeFile) throws URISyntaxException
Generates a path for the 'relativeFile' relative to the 'baseLocation'.- Parameters:
baseLocation- denotes a directoryrelativeFile- denotes a relative file to the baseLocation- Throws:
URISyntaxException- if path is empty or has no parent directory available while resolving../
-
getParentOf
public static String getParentOf(String path) throws URISyntaxException
- Parameters:
path- assuming a slashified path, either denotes a file or directory, either relative or absolute.- Returns:
- parent of path
- Throws:
URISyntaxException- if path is empty or has no parent directory available
-
cleanPathString
public static String cleanPathString(String path) throws URISyntaxException
- Parameters:
path- assuming a slashified path, either denoting a file or directory, either relative or absolute.- Returns:
- clean path string where
./and../is resolved, while keeping a starting../at the beginning of a relative path. - Throws:
URISyntaxException- if path is empty or has no parent directory available while resolving../
-
getUriFilePathOrASCII
public static String getUriFilePathOrASCII(Uri uri)
Ifuriis a file scheme implementation returnsUri.toFile().File.getPath().Otherwise it returns the
URI#toASCIIString()encoded URI.
-
openURL
public static URLConnection openURL(URL url)
Returns the connected URLConnection, or null if not url is not available
-
openURL
public static URLConnection openURL(URL url, String dbgmsg)
Returns the connected URLConnection, or null if not url is not available
-
testFile
public static boolean testFile(File file, boolean shallBeDir, boolean shallBeWritable)
Test whetherfileexists and matches the given requirements- Parameters:
file-shallBeDir-shallBeWritable-- Returns:
-
testDirExec
public static boolean testDirExec(File dir) throws SecurityException
Returns true if the givendir- exists, and
- is a directory, and
- is writeable, and
- files can be executed from the directory
- Parameters:
dir-- Throws:
SecurityException- if file creation and process execution is not allowed within the current security context
-
testDir
public static File testDir(File dir, boolean create, boolean executable) throws SecurityException
Returns the directorydir, which is processed and tested as described below.- If
createistrueand the directory does not exist yet, it is created incl. all sub-directories. - If
dirNameexists, but is not a directory,nullis being returned. - If the directory does not exist or is not writeable,
nullis being returned. - If
executableistrueand files cannot be executed from the directory,nullis being returned.
- Parameters:
dir- the directory to processcreate- true if the directory shall be created if not existingexecutable- true if the user intents to launch executables from the temporary directory, otherwise false.- Throws:
SecurityException- if file creation and process execution is not allowed within the current security context
- If
-
getTempDir
public static File getTempDir(boolean executable) throws SecurityException, IOException
Returns a platform independent writable directory for temporary files consisting of the platform'stemp-root+tmpSubDir, e.g./tmp/jogamp_0000/.On standard Java the
temp-rootfolder is specified byjava.io.tempdir.On Android the
temp-rootfolder is relative to the applications local folder (seeContext#getDir(String, int)) is returned, if the Android application/activity has registered it's Application Context viaStaticContext.init(..). This allows using the temp folder w/o the need forsdcardaccess, which would be thejava.io.tempdirlocation on Android!In case
temp-rootis the users home folder, a dot is being prepended totmpSubDir, i.e.:/home/user/.jogamp_0000/.- Parameters:
executable- true if the user intents to launch executables from the temporary directory, otherwise false.- Throws:
IOException- if no temporary directory could be determinedSecurityException- if access tojava.io.tmpdiris not allowed within the current security context- See Also:
PropertyAccess.getProperty(String, boolean),Context#getDir(String, int)
-
createTempFile
public static File createTempFile(String prefix, String suffix, boolean executable) throws IllegalArgumentException, IOException, SecurityException
UtilizingFile.createTempFile(String, String, File)usinggetTempDir(boolean)as the directory parameter, ie. location of the root temp folder.- Parameters:
prefix-suffix-executable- true if the temporary root folder needs to hold executable files, otherwise false.- Returns:
- Throws:
IllegalArgumentExceptionIOException- if no temporary directory could be determined or temp file could not be createdSecurityException- See Also:
File.createTempFile(String, String),File.createTempFile(String, String, File),getTempDir(boolean)
-
close
public static void close(Closeable stream, boolean throwRuntimeException) throws RuntimeException
- Throws:
RuntimeException
-
close
public static IOException close(Closeable stream, IOException[] saveOneIfFree, PrintStream dumpExcess)
Helper to simplify closingCloseables.- Parameters:
stream- theCloseableinstance to closesaveOneIfFree- cache for oneIOExceptionto store, if not already used (excess)dumpExcess- dump the excessIOExceptionon thisPrintStream- Returns:
- the excess
IOExceptionornull.
-
filesOf
public static ArrayList<String> filesOf(List<String> paths, List<Pattern> excludes, List<Pattern> includes)
Retrieve the list of all filenames traversing through given paths- Parameters:
paths- list of paths to traverse through, containing directories and filesexcludes- optional list of excludePattern. Allmatchingfiles or directories will be omitted. Maybe be null or empty.includes- optional list of explicit includePattern. If given, onlymatchingfiles will be returned, otherwise all occurring.- Returns:
- list of unsorted filenames within given paths
-
-