Package libcore.io
Class IoUtils
java.lang.Object
libcore.io.IoUtils
public final class IoUtils extends Object
-
Method Summary
Modifier and Type Method Description static booleancanOpenReadOnly(String path)Do not use.static voidclose(FileDescriptor fd)Calls close(2) on 'fd'.static voidcloseQuietly(FileDescriptor fd)Closes 'fd', ignoring any exceptions.static voidcloseQuietly(AutoCloseable closeable)Closes 'closeable', ignoring any checked exceptions.static voidcloseQuietly(Socket socket)Closes 'socket', ignoring any exceptions.static FilecreateTemporaryDirectory(String prefix)Creates a unique new temporary directory under "java.io.tmpdir".static voiddeleteContents(File dir)Deprecated.static byte[]readFileAsByteArray(String absolutePath)Returns the contents of 'path' as a byte array.static StringreadFileAsString(String absolutePath)Returns the contents of 'path' as a string.static voidsetBlocking(FileDescriptor fd, boolean blocking)Sets 'fd' to be blocking or non-blocking, according to the state of 'blocking'.static voidthrowInterruptedIoException()
-
Method Details
-
close
Calls close(2) on 'fd'. Also resets the internal int to -1. Does nothing if 'fd' is null or invalid.- Throws:
IOException
-
closeQuietly
Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null. -
closeQuietly
Closes 'fd', ignoring any exceptions. Does nothing if 'fd' is null or invalid. -
closeQuietly
Closes 'socket', ignoring any exceptions. Does nothing if 'socket' is null. -
setBlocking
Sets 'fd' to be blocking or non-blocking, according to the state of 'blocking'.- Throws:
IOException
-
readFileAsByteArray
Returns the contents of 'path' as a byte array.- Throws:
IOException
-
readFileAsString
Returns the contents of 'path' as a string. The contents are assumed to be UTF-8.- Throws:
IOException
-
deleteContents
Deprecated.UsecreateTemporaryDirectory(java.lang.String)instead.Do not use. Use createTemporaryDirectory instead. Used by frameworks/base unit tests to clean up a temporary directory. Deliberately ignores errors, on the assumption that test cleanup is only supposed to be best-effort.- Throws:
IOException
-
createTemporaryDirectory
Creates a unique new temporary directory under "java.io.tmpdir". -
canOpenReadOnly
Do not use. This is for System.loadLibrary use only. Checks whetherpathcan be opened read-only. Similar to File.exists, but doesn't require read permission on the parent, so it'll work in more cases, and allow you to remove read permission from more directories. Everyone else should just open(2) and then use the fd, but the loadLibrary API is broken by its need to ask ClassLoaders where to find a .so rather than just calling dlopen(3). -
throwInterruptedIoException
- Throws:
InterruptedIOException
-
createTemporaryDirectory(java.lang.String)instead.