Package libcore.io

Class IoUtils


  • public final class IoUtils
    extends Object
    • 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
        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 whether path can 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).