public class IOUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
IOUtil.ClassResources
Helper compound associating a class instance and resource paths
to be
resolved at a later time. |
static class |
IOUtil.StreamMonitor |
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG |
static Pattern |
patternSpaceEnc |
static String |
tmpSubDir
Subdirectory within platform's temporary root directory where all JogAmp related temp files are being stored:
jogamp |
Modifier and Type | Method and Description |
---|---|
static String |
cleanPathString(String path) |
static void |
close(Closeable stream,
boolean throwRuntimeException) |
static IOException |
close(Closeable stream,
IOException[] saveOneIfFree,
PrintStream dumpExcess)
Helper to simplify closing
Closeable s. |
static byte[] |
copyStream2ByteArray(InputStream stream)
Copy the specified input stream to a byte array, which is being returned.
|
static ByteBuffer |
copyStream2ByteBuffer(InputStream stream)
Copy the specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned.
|
static ByteBuffer |
copyStream2ByteBuffer(InputStream stream,
int initialCapacity)
Copy the specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned.
|
static int |
copyStream2File(InputStream in,
File outFile,
int totalNumBytes)
Copy the specified input stream to the specified output file.
|
static int |
copyStream2Stream(InputStream in,
OutputStream out,
int totalNumBytes)
Copy the specified input stream to the specified output stream.
|
static int |
copyStream2Stream(int bufferSize,
InputStream in,
OutputStream out,
int totalNumBytes)
Copy the specified input stream to the specified output stream.
|
static int |
copyURLConn2File(URLConnection conn,
File outFile)
Copy the specified URL resource to the specified output file.
|
static File |
createTempFile(String prefix,
String suffix,
boolean executable)
Utilizing
File.createTempFile(String, String, File) using
getTempDir(boolean) as the directory parameter, ie. |
static String |
getBasename(String fname)
Returns the basename of the given fname w/o directory part
|
static String |
getClassFileName(String clazzBinName) |
static URL |
getClassURL(String clazzBinName,
ClassLoader cl) |
static String |
getDirname(String fname)
Returns unified '/' dirname including the last '/'
|
static FileOutputStream |
getFileOutputStream(File file,
boolean allowOverwrite) |
static String |
getFileSuffix(File file)
Returns the lowercase suffix of the given file name (the text
after the last '.' in the file name).
|
static String |
getFileSuffix(String filename)
Returns the lowercase suffix of the given file name (the text
after the last '.' in the file name).
|
static String |
getParentOf(String path) |
static String |
getRelativeOf(File baseLocation,
String relativeFile)
Generates a path for the 'relativeFile' relative to the 'baseLocation'.
|
static URLConnection |
getResource(Class<?> context,
String resourcePath)
Locating a resource using
getResource(String, ClassLoader) :
relative: context 's package name-path plus resourcePath via context 's ClassLoader. |
static URLConnection |
getResource(String resourcePath,
ClassLoader cl)
Locating a resource using the ClassLoader's facilities.
|
static File |
getTempDir(boolean executable)
Returns a platform independent writable directory for temporary files
consisting of the platform's
temp-root + tmpSubDir ,
e.g. |
static String |
getUriFilePathOrASCII(Uri uri)
|
static URLConnection |
openURL(URL url)
Returns the connected URLConnection, or null if not url is not available
|
static URLConnection |
openURL(URL url,
String dbgmsg)
Returns the connected URLConnection, or null if not url is not available
|
static String |
slashify(String path,
boolean startWithSlash,
boolean endWithSlash) |
static File |
testDir(File dir,
boolean create,
boolean executable)
Returns the directory
dir , which is processed and tested as described below. |
static boolean |
testDirExec(File dir)
Returns true if the given
dir
exists, and
is a directory, and
is writeable, and
files can be executed from the directory
|
static boolean |
testFile(File file,
boolean shallBeDir,
boolean shallBeWritable)
Test whether
file exists and matches the given requirements |
public static final boolean DEBUG
public static final String tmpSubDir
jogamp
public static final Pattern patternSpaceEnc
public static int copyURLConn2File(URLConnection conn, File outFile) throws IOException
conn
- the open URLConnectionoutFile
- the destinationIOException
public static int copyStream2File(InputStream in, File outFile, int totalNumBytes) throws IOException
in
- the sourceoutFile
- the destinationtotalNumBytes
- informal number of expected bytes, maybe used for user feedback while processing. -1 if unknownIOException
public static int copyStream2Stream(InputStream in, OutputStream out, int totalNumBytes) throws IOException
in
- the sourceout
- the destinationtotalNumBytes
- informal number of expected bytes, maybe used for user feedback while processing. -1 if unknownIOException
public static int copyStream2Stream(int bufferSize, InputStream in, OutputStream out, int totalNumBytes) throws IOException
bufferSize
- the intermediate buffer size, should be MachineDataInfo.pageSizeInBytes()
for best performance.in
- the sourceout
- the destinationtotalNumBytes
- informal number of expected bytes, maybe used for user feedback while processing. -1 if unknownIOException
public static byte[] copyStream2ByteArray(InputStream stream) throws IOException
IOException
public static ByteBuffer copyStream2ByteBuffer(InputStream stream) throws IOException
The implementation creates the ByteBuffer w/ copyStream2ByteArray(InputStream)
's returned byte array.
stream
- input stream, which will be wrapped into a BufferedInputStream, if not already done.IOException
public static ByteBuffer copyStream2ByteBuffer(InputStream stream, int initialCapacity) throws IOException
The implementation creates the ByteBuffer w/ copyStream2ByteArray(InputStream)
's returned byte array.
stream
- input stream, which will be wrapped into a BufferedInputStream, if not already done.initialCapacity
- initial buffer capacity in bytes, if > available bytesIOException
public static String slashify(String path, boolean startWithSlash, boolean endWithSlash) throws URISyntaxException
path
- startWithSlash
- endWithSlash
- URISyntaxException
- if path is empty or has no parent directory available while resolving ../
public static String getFileSuffix(File file)
file
- name of the fileNullPointerException
- if file is nullpublic static String getFileSuffix(String filename)
filename
- name of the fileNullPointerException
- if filename is nullpublic static FileOutputStream getFileOutputStream(File file, boolean allowOverwrite) throws IOException
file
- allowOverwrite
- IOException
- if the file already exists and allowOverwrite
is false,
the class FileOutputStream
is not accessible or
the user does not have sufficient rights to access the local filesystem.public static URL getClassURL(String clazzBinName, ClassLoader cl) throws IOException
clazzBinName
- com.jogamp.common.util.cache.TempJarCachecl
- ClassLoader to locate the JarFileIOException
- if the jar file could not been found by the ClassLoaderpublic static String getBasename(String fname) throws URISyntaxException
URISyntaxException
- if path is empty or has no parent directory available while resolving ../
public static String getDirname(String fname) throws URISyntaxException
URISyntaxException
- if path is empty or has no parent directory available while resolving ../
public static URLConnection getResource(Class<?> context, String resourcePath)
getResource(String, ClassLoader)
:
context
's package name-path plus resourcePath
via context
's ClassLoader.
This allows locations relative to JAR- and other URLs.
The resourcePath
may start with ../
to navigate to parent folder.context
's ClassLoader and the resourcePath
as is (filesystem)Returns the resolved and open URLConnection or null if not found.
public static URLConnection getResource(String resourcePath, ClassLoader cl)
Returns the resolved and connected URLConnection or null if not found.
public static String getRelativeOf(File baseLocation, String relativeFile) throws URISyntaxException
baseLocation
- denotes a directoryrelativeFile
- denotes a relative file to the baseLocationURISyntaxException
- if path is empty or has no parent directory available while resolving ../
public static String getParentOf(String path) throws URISyntaxException
path
- assuming a slashified path beginning with "/" as it's root directory, either denotes a file or directory.URISyntaxException
- if path is empty or has no parent directory availablepublic static String cleanPathString(String path) throws URISyntaxException
path
- assuming a slashified path beginning with "/" as it's root directory, either denotes a file or directory.../
and ./
is resolved.URISyntaxException
- if path is empty or has no parent directory available while resolving ../
public static String getUriFilePathOrASCII(Uri uri)
uri
is a file scheme
implementation returns Uri.toFile()
.File.getPath()
.
Otherwise it returns the URI#toASCIIString()
encoded URI.
public static URLConnection openURL(URL url)
public static URLConnection openURL(URL url, String dbgmsg)
public static boolean testFile(File file, boolean shallBeDir, boolean shallBeWritable)
file
exists and matches the given requirementsfile
- shallBeDir
- shallBeWritable
- public static boolean testDirExec(File dir) throws SecurityException
dir
dir
- SecurityException
- if file creation and process execution is not allowed within the current security contextpublic static File testDir(File dir, boolean create, boolean executable) throws SecurityException
dir
, which is processed and tested as described below.
create
is true
and the directory does not exist yet, it is created incl. all sub-directories.dirName
exists, but is not a directory, null
is being returned.null
is being returned.executable
is true
and files cannot be executed from the directory, null
is being returned.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.SecurityException
- if file creation and process execution is not allowed within the current security contextpublic static File getTempDir(boolean executable) throws SecurityException, IOException
temp-root
+ tmpSubDir
,
e.g. /tmp/jogamp_0000/
.
On standard Java the temp-root
folder is specified by java.io.tempdir
.
On Android the temp-root
folder is relative to the applications local folder
(see Context#getDir(String, int)
) is returned, if
the Android application/activity has registered it's Application Context
via StaticContext.init(..)
.
This allows using the temp folder w/o the need for sdcard
access, which would be the java.io.tempdir
location on Android!
In case temp-root
is the users home folder,
a dot is being prepended to tmpSubDir
, i.e.: /home/user/.jogamp_0000/
.
executable
- true if the user intents to launch executables from the temporary directory, otherwise false.IOException
- if no temporary directory could be determinedSecurityException
- if access to java.io.tmpdir
is not allowed within the current security contextPropertyAccess.getProperty(String, boolean)
,
Context#getDir(String, int)
public static File createTempFile(String prefix, String suffix, boolean executable) throws IllegalArgumentException, IOException, SecurityException
File.createTempFile(String, String, File)
using
getTempDir(boolean)
as the directory parameter, ie. location
of the root temp folder.prefix
- suffix
- executable
- true if the temporary root folder needs to hold executable files, otherwise false.IllegalArgumentException
IOException
- if no temporary directory could be determined or temp file could not be createdSecurityException
File.createTempFile(String, String)
,
File.createTempFile(String, String, File)
,
getTempDir(boolean)
public static void close(Closeable stream, boolean throwRuntimeException) throws RuntimeException
RuntimeException
public static IOException close(Closeable stream, IOException[] saveOneIfFree, PrintStream dumpExcess)
Closeable
s.stream
- the Closeable
instance to closesaveOneIfFree
- cache for one IOException
to store, if not already used (excess)dumpExcess
- dump the excess IOException
on this PrintStream
IOException
or null
.