public class IOUtil extends Object
Modifier and Type | Method and Description |
---|---|
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 int |
copyStream2Stream(InputStream in,
OutputStream out,
int totalNumBytes)
Copy the specified input stream to the specified output stream.
|
static int |
copyURL2File(URL url,
File outFile)
Copy the specified input stream to the specified output file.
|
static File |
createTempFile(String prefix,
String suffix)
Utilizing
File.createTempFile(String, String, File) using
getTempRoot() 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 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 |
getRelativeOf(File baseLocation,
String relativeFile)
Generates a path for the 'relativeFile' relative to the 'baseLocation'.
|
static String |
getRelativeOf(URL baseLocation,
String relativeFile)
Generates a path for the 'relativeFile' relative to the 'baseLocation'.
|
static URL |
getResource(Class<?> context,
String resourcePath)
Locating a resource using 'getResource(String path, ClassLoader cl)',
with the
context's package name-path plus the resourcePath (incl.
|
static URL |
getResource(String resourcePath,
ClassLoader cl)
Locating a resource using the ClassLoader's facility if not null,
the absolute URL and absolute file.
|
static File |
getTempDir(String tmpDirPrefix)
This methods finds [and creates] a temporary directory:
|
static File |
getTempRoot()
On standard Java, the folder specified by
java.io.tempdir
is returned. |
static String |
slashify(String path,
boolean startWithSlash,
boolean endWithSlash)
RESOURCE / FILE NAME STUFF
|
static URL |
toURL(File file)
Using the proper advertised conversion via File -> URI -> URL
|
static URL |
toURLSimple(File file)
Using the simple conversion via File -> URL, assuming proper characters.
|
static URL |
toURLSimple(String protocol,
String file,
boolean isDirectory) |
static boolean |
urlExists(URL url)
Returns true, if the URL exists and a connection could be opened.
|
static boolean |
urlExists(URL url,
String dbgmsg) |
public static int copyURL2File(URL url, File outFile) throws IOException
IOException
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 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.
IOException
public static String slashify(String path, boolean startWithSlash, boolean endWithSlash)
public static URL toURL(File file) throws MalformedURLException
MalformedURLException
public static URL toURLSimple(File file) throws MalformedURLException
MalformedURLException
public static URL toURLSimple(String protocol, String file, boolean isDirectory) throws MalformedURLException
MalformedURLException
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 String getClassFileName(String clazzBinName) throws IOException
IOException
public static URL getClassURL(String clazzBinName, ClassLoader cl) throws IOException
clazzBinName
- com.jogamp.common.util.cache.TempJarCachecl
- IOException
public static String getBasename(String fname)
public static String getDirname(String fname)
public static URL getResource(Class<?> context, String resourcePath)
getResource(String, ClassLoader)
public static URL getResource(String resourcePath, ClassLoader cl)
public static String getRelativeOf(File baseLocation, String relativeFile)
baseLocation
- denotes a directoryrelativeFile
- denotes a relative file to the baseLocationpublic static String getRelativeOf(URL baseLocation, String relativeFile)
baseLocation
- denotes a URL to a filerelativeFile
- denotes a relative file to the baseLocation's parent directorypublic static boolean urlExists(URL url)
public static File createTempFile(String prefix, String suffix) throws IllegalArgumentException, IOException, SecurityException
File.createTempFile(String, String, File)
using
getTempRoot()
as the directory parameter, ie. location
of the root temp folder.prefix
- suffix
- IllegalArgumentException
IOException
SecurityException
File.createTempFile(String, String)
,
File.createTempFile(String, String, File)
,
getTempRoot()
public static File getTempRoot() throws SecurityException
java.io.tempdir
is returned.
On Android a temp
folder 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#setContext(Context)
.
This allows using the temp folder w/o the need for sdcard
access, which would be the java.io.tempdir
location on Android!
The purpose of this wrapper
is to allow unique code to be used
for both platforms w/o the need to handle extra permissions.
SecurityException
StaticContext#setContext(Context)
,
Context#getDir(String, int)
public static File getTempDir(String tmpDirPrefix) throws IOException, SecurityException
for(tempBaseDir = tempRootDir + tmpDirPrefix + _ + [000000-999999]) { if(tempBaseDir.isDirectory()) { if(tempBaseDir.canWrite()) { return tempBaseDir; } } else { tempBaseDir.mkdir(); return tempBaseDir; } }The
tempRootDir
is retrieved by getTempRoot()
.
The iteration through [000000-999999] ensures that the code is multi-user save.
tmpDirPrefix
- IOException
SecurityException