Class IOUtils

java.lang.Object
com.blackrook.gloop.opengl.struct.IOUtils

public final class IOUtils extends Object
Simple IO utility functions.
Author:
Matthew Tropiano
  • Method Details

    • openTextStream

      public static BufferedReader openTextStream(InputStream in) throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()))
      Parameters:
      in - the stream to read.
      Returns:
      an open buffered reader for the provided stream.
      Throws:
      IOException - if an error occurred opening the stream for reading.
      SecurityException - if you do not have permission for opening the stream.
    • openTextStream

      public static BufferedReader openTextStream(InputStream in, Charset encoding) throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(in, encoding))
      Parameters:
      in - the stream to read.
      encoding - the text encoding to use.
      Returns:
      an open buffered reader for the provided stream.
      Throws:
      IOException - if an error occurred opening the stream for reading.
      SecurityException - if you do not have permission for opening the stream.
    • openTextFile

      public static BufferedReader openTextFile(File file) throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.defaultCharset()))
      Parameters:
      file - the file to open.
      Returns:
      an open buffered reader for the provided file.
      Throws:
      IOException - if an error occurred opening the file for reading.
      SecurityException - if you do not have permission for opening the file.
    • openTextFile

      public static BufferedReader openTextFile(File file, Charset encoding) throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))
      Parameters:
      file - the file to open.
      encoding - the text encoding to use.
      Returns:
      an open buffered reader for the provided file.
      Throws:
      IOException - if an error occurred opening the file for reading.
      SecurityException - if you do not have permission for opening the file.
    • openTextFile

      public static BufferedReader openTextFile(String filePath) throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), Charset.defaultCharset()))
      Parameters:
      filePath - the path of the file to open.
      Returns:
      an open buffered reader for the provided path.
      Throws:
      IOException - if an error occurred opening the file for reading.
      SecurityException - if you do not have permission for opening the file.
    • openTextFile

      public static BufferedReader openTextFile(String filePath, Charset encoding) throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), encoding))
      Parameters:
      filePath - the path of the file to open.
      encoding - the text encoding to use.
      Returns:
      an open buffered reader for the provided path.
      Throws:
      IOException - if an error occurred opening the file for reading.
      SecurityException - if you do not have permission for opening the file.
    • openSystemIn

      public static BufferedReader openSystemIn() throws IOException
      Convenience method for new BufferedReader(new InputStreamReader(System.in))
      Returns:
      an open buffered reader for System.in.
      Throws:
      IOException - if an error occurred opening Standard IN.
      SecurityException - if you do not have permission for opening Standard IN.
    • openResource

      public static InputStream openResource(String pathString)
      Opens an InputStream to a resource using the current thread's ClassLoader.
      Parameters:
      pathString - the resource pathname.
      Returns:
      an open InputStream for reading the resource or null if not found.
      See Also:
    • openResource

      public static InputStream openResource(ClassLoader classLoader, String pathString)
      Opens an InputStream to a resource using a provided ClassLoader.
      Parameters:
      classLoader - the provided ClassLoader to use.
      pathString - the resource pathname.
      Returns:
      an open InputStream for reading the resource or null if not found.
      See Also:
    • getASCIIContents

      public static String getASCIIContents(File f) throws IOException
      Retrieves the ASCII contents of a file.
      Parameters:
      f - the file to use.
      Returns:
      a contiguous string (including newline characters) of the file's contents.
      Throws:
      FileNotFoundException - if the file cannot be found.
      IOException - if the read cannot be done.
    • getTextualContents

      public static String getTextualContents(File f) throws IOException
      Retrieves the textual contents of a file in the system's current encoding.
      Parameters:
      f - the file to use.
      Returns:
      a contiguous string (including newline characters) of the file's contents.
      Throws:
      IOException - if the read cannot be done.
    • getTextualContents

      public static String getTextualContents(InputStream in) throws IOException
      Retrieves the textual contents of a stream in the system's current encoding.
      Parameters:
      in - the input stream to use.
      Returns:
      a contiguous string (including newline characters) of the stream's contents.
      Throws:
      IOException - if the read cannot be done.
    • getTextualContents

      public static String getTextualContents(InputStream in, String encoding) throws IOException
      Retrieves the textual contents of a stream.
      Parameters:
      in - the input stream to use.
      encoding - name of the encoding type.
      Returns:
      a contiguous string (including newline characters) of the stream's contents.
      Throws:
      IOException - if the read cannot be done.
    • getBinaryContents

      public static byte[] getBinaryContents(File f) throws IOException
      Retrieves the binary contents of a file.
      Parameters:
      f - the file to use.
      Returns:
      an array of the bytes that make up the file.
      Throws:
      FileNotFoundException - if the file cannot be found.
      IOException - if the read cannot be done.
    • getBinaryContents

      public static byte[] getBinaryContents(InputStream in, int len) throws IOException
      Retrieves the binary contents of a stream.
      Parameters:
      in - the input stream to use.
      len - the amount of bytes to read.
      Returns:
      an array of len bytes that make up the stream.
      Throws:
      IOException - if the read cannot be done.
    • getBinaryContents

      public static byte[] getBinaryContents(InputStream in) throws IOException
      Retrieves the binary contents of a stream until it hits the end of the stream.
      Parameters:
      in - the input stream to use.
      Returns:
      an array of len bytes that make up the data in the stream.
      Throws:
      IOException - if the read cannot be done.
    • relay

      public static int relay(InputStream in, OutputStream out) throws IOException
      Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.
      Parameters:
      in - the input stream to grab data from.
      out - the output stream to write the data to.
      Returns:
      the total amount of bytes relayed.
      Throws:
      IOException - if a read or write error occurs.
    • relay

      public static int relay(InputStream in, OutputStream out, int bufferSize) throws IOException
      Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.
      Parameters:
      in - the input stream to grab data from.
      out - the output stream to write the data to.
      bufferSize - the buffer size for the I/O. Must be > 0.
      Returns:
      the total amount of bytes relayed.
      Throws:
      IOException - if a read or write error occurs.
    • relay

      public static int relay(InputStream in, OutputStream out, int bufferSize, int maxLength) throws IOException
      Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.
      Parameters:
      in - the input stream to grab data from.
      out - the output stream to write the data to.
      bufferSize - the buffer size for the I/O. Must be > 0.
      maxLength - the maximum amount of bytes to relay, or a value < 0 for no max.
      Returns:
      the total amount of bytes relayed.
      Throws:
      IOException - if a read or write error occurs.
    • relay

      public static int relay(Reader reader, Writer writer) throws IOException
      Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.
      Parameters:
      reader - the reader to grab characters from.
      writer - the writer to write the characters to.
      Returns:
      the total amount of characters relayed.
      Throws:
      IOException - if a read or write error occurs.
    • relay

      public static int relay(Reader reader, Writer writer, int bufferSize) throws IOException
      Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.
      Parameters:
      reader - the reader to grab characters from.
      writer - the writer to write the characters to.
      bufferSize - the buffer size in characters for the I/O. Must be > 0.
      Returns:
      the total amount of characters relayed.
      Throws:
      IOException - if a read or write error occurs.
    • relay

      public static int relay(Reader reader, Writer writer, int bufferSize, int maxLength) throws IOException
      Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.
      Parameters:
      reader - the reader to grab characters from.
      writer - the writer to write the characters to.
      bufferSize - the buffer size in characters for the I/O. Must be > 0.
      maxLength - the maximum amount of characters to relay, or a value < 0 for no max.
      Returns:
      the total amount of characters relayed.
      Throws:
      IOException - if a read or write error occurs.
    • setRelayBufferSize

      public static void setRelayBufferSize(int size)
      Sets the size of the buffer in bytes for relay(InputStream, OutputStream). Although you may not encounter this problem, it would be unwise to set this during a call to relay(). Size cannot be 0 or less.
      Parameters:
      size - the size of the relay buffer.
    • getRelayBufferSize

      public static int getRelayBufferSize()
      Returns:
      the size of the relay buffer for relay(InputStream, OutputStream) in bytes.
    • getLine

      public static String getLine()
      Reads a line from standard in; throws a RuntimeException if something absolutely serious happens. Should be used just for convenience.
      Returns:
      a single line read from Standard In.
      See Also:
    • getNullOutputStream

      public static OutputStream getNullOutputStream()
      Returns:
      a null output stream, where all writes are accepted and not used.
    • getNullInputStream

      public static InputStream getNullInputStream()
      Returns:
      a null input stream, where all reads result in an end-of-stream.
    • getNullFile

      public static File getNullFile()
      Returns:
      a handle to the null file for this platform.
    • close

      public static void close(Closeable c)
      Attempts to close a Closeable object. If the object is null, this does nothing.
      Parameters:
      c - the reference to the closeable object.
    • close

      public static void close(AutoCloseable c)
      Attempts to close an AutoCloseable object. If the object is null, this does nothing.
      Parameters:
      c - the reference to the AutoCloseable object.