Package libcore.io
Class IoUtils
- java.lang.Object
-
- libcore.io.IoUtils
-
public final class IoUtils extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods 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.UsecreateTemporaryDirectory(java.lang.String)instead.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 Detail
-
close
public static void close(FileDescriptor fd) throws IOException
Calls close(2) on 'fd'. Also resets the internal int to -1. Does nothing if 'fd' is null or invalid.- Throws:
IOException
-
closeQuietly
public static void closeQuietly(AutoCloseable closeable)
Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
-
closeQuietly
public static void closeQuietly(FileDescriptor fd)
Closes 'fd', ignoring any exceptions. Does nothing if 'fd' is null or invalid.
-
closeQuietly
public static void closeQuietly(Socket socket)
Closes 'socket', ignoring any exceptions. Does nothing if 'socket' is null.
-
setBlocking
public static void setBlocking(FileDescriptor fd, boolean blocking) throws IOException
Sets 'fd' to be blocking or non-blocking, according to the state of 'blocking'.- Throws:
IOException
-
readFileAsByteArray
public static byte[] readFileAsByteArray(String absolutePath) throws IOException
Returns the contents of 'path' as a byte array.- Throws:
IOException
-
readFileAsString
public static String readFileAsString(String absolutePath) throws IOException
Returns the contents of 'path' as a string. The contents are assumed to be UTF-8.- Throws:
IOException
-
deleteContents
public static void deleteContents(File dir) throws IOException
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
public static File createTemporaryDirectory(String prefix)
Creates a unique new temporary directory under "java.io.tmpdir".
-
canOpenReadOnly
public static boolean canOpenReadOnly(String path)
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
public static void throwInterruptedIoException() throws InterruptedIOException- Throws:
InterruptedIOException
-
-