Class IO


  • public class IO
    extends Object
    Input/Output utilities
    • Method Detail

      • readFully

        public static final byte[] readFully​(File path)
                                      throws FileNotFoundException,
                                             IOException
        Read an entire local file into memory as a byte array.
        Parameters:
        path - location of the file to read.
        Returns:
        complete contents of the requested local file.
        Throws:
        FileNotFoundException - the file does not exist.
        IOException - the file exists, but its contents cannot be read.
      • readSome

        public static final byte[] readSome​(File path,
                                            int limit)
                                     throws FileNotFoundException,
                                            IOException
        Read at most limit bytes from the local file into memory as a byte array.
        Parameters:
        path - location of the file to read.
        limit - maximum number of bytes to read, if the file is larger than only the first limit number of bytes are returned
        Returns:
        complete contents of the requested local file. If the contents exceeds the limit, then only the limit is returned.
        Throws:
        FileNotFoundException - the file does not exist.
        IOException - the file exists, but its contents cannot be read.
      • readFully

        public static final byte[] readFully​(File path,
                                             int max)
                                      throws FileNotFoundException,
                                             IOException
        Read an entire local file into memory as a byte array.
        Parameters:
        path - location of the file to read.
        max - maximum number of bytes to read, if the file is larger than this limit an IOException is thrown.
        Returns:
        complete contents of the requested local file.
        Throws:
        FileNotFoundException - the file does not exist.
        IOException - the file exists, but its contents cannot be read.
      • readWholeStream

        public static ByteBuffer readWholeStream​(InputStream in,
                                                 int sizeHint)
                                          throws IOException
        Read an entire input stream into memory as a ByteBuffer. Note: The stream is read to its end and is not usable after calling this method. The caller is responsible for closing the stream.
        Parameters:
        in - input stream to be read.
        sizeHint - a hint on the approximate number of bytes contained in the stream, used to allocate temporary buffers more efficiently
        Returns:
        complete contents of the input stream. The ByteBuffer always has a writable backing array, with position() == 0 and limit() equal to the actual length read. Callers may rely on obtaining the underlying array for efficient data access. If sizeHint was too large, the array may be over-allocated, resulting in limit() < array().length.
        Throws:
        IOException - there was an error reading from the stream.
      • readFully

        public static void readFully​(InputStream fd,
                                     byte[] dst,
                                     int off,
                                     int len)
                              throws IOException
        Read the entire byte array into memory, or throw an exception.
        Parameters:
        fd - input stream to read the data from.
        dst - buffer that must be fully populated, [off, off+len).
        off - position within the buffer to start writing to.
        len - number of bytes that must be read.
        Throws:
        EOFException - the stream ended before dst was fully populated.
        IOException - there was an error reading from the stream.
      • read

        public static int read​(ReadableByteChannel channel,
                               byte[] dst,
                               int off,
                               int len)
                        throws IOException
        Read as much of the array as possible from a channel.
        Parameters:
        channel - channel to read data from.
        dst - buffer that must be fully populated, [off, off+len).
        off - position within the buffer to start writing to.
        len - number of bytes that should be read.
        Returns:
        number of bytes actually read.
        Throws:
        IOException - there was an error reading from the channel.
      • readFully

        public static int readFully​(InputStream fd,
                                    byte[] dst,
                                    int off)
                             throws IOException
        Read the entire byte array into memory, unless input is shorter
        Parameters:
        fd - input stream to read the data from.
        dst - buffer that must be fully populated, [off, off+len).
        off - position within the buffer to start writing to.
        Returns:
        number of bytes read
        Throws:
        IOException - there was an error reading from the stream.
      • skipFully

        public static void skipFully​(InputStream fd,
                                     long toSkip)
                              throws IOException
        Skip an entire region of an input stream.

        The input stream's position is moved forward by the number of requested bytes, discarding them from the input. This method does not return until the exact number of bytes requested has been skipped.

        Parameters:
        fd - the stream to skip bytes from.
        toSkip - total number of bytes to be discarded. Must be >= 0.
        Throws:
        EOFException - the stream ended before the requested number of bytes were skipped.
        IOException - there was an error reading from the stream.
      • readLines

        public static List<String> readLines​(String s)
        Divides the given string into lines.
        Parameters:
        s - the string to read
        Returns:
        the string divided into lines
        Since:
        2.0
      • readLine

        public static String readLine​(Reader in,
                                      int sizeHint)
                               throws IOException
        Read the next line from a reader.

        Like BufferedReader.readLine(), but only treats \n as end-of-line, and includes the trailing newline.

        Parameters:
        in - the reader to read from.
        sizeHint - hint for buffer sizing; 0 or negative for default.
        Returns:
        the next line from the input, always ending in \n unless EOF was reached.
        Throws:
        IOException - there was an error reading from the stream.
        Since:
        4.1