Class StreamUtils

java.lang.Object
org.jppf.utils.streams.StreamUtils

public final class StreamUtils extends Object
Collection of utility methods for manipulating streams.
Author:
Laurent Cohen
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    close(AutoCloseable closeable)
    Attempt to close the specified closeable without logging an eventual error.
    static void
    close(AutoCloseable closeable, org.slf4j.Logger log)
    Attempt to close the specified closeable and log any eventual error.
    static void
    Attempt to silently close (no exception logging) the specified closeable.
    static long
    copyFile(File src, File dest)
    Copy the data read from the specified input stream to the specified output stream.
    static long
    Copy the data read from the specified input stream to the specified output stream.
    static long
    copyStream(InputStream is, OutputStream os, boolean closeStreams)
    Copy the data read from the specified input stream to the specified output stream.
    static byte[]
    Get the content of an input stream as an array of bytes.
    static void
    printf(org.slf4j.Logger logger, String format, Object... params)
    Convenience method to print a formatted mssage to System.out and a specified logger.
    static void
    Display a message and wait until a key is pressed.
    static void
    waitKeyPressed(char c, String message)
    Display a message and wait until the specified key is pressed.
    static void
    Display a message and wait until a key is pressed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • close

      public static void close(AutoCloseable closeable) throws Exception
      Attempt to close the specified closeable without logging an eventual error.
      Parameters:
      closeable - the closeable to close.
      Throws:
      Exception - if any error occurs while closing the closeable.
    • closeSilent

      public static void closeSilent(AutoCloseable closeable)
      Attempt to silently close (no exception logging) the specified closeable.
      Parameters:
      closeable - the closeable to close.
    • close

      public static void close(AutoCloseable closeable, org.slf4j.Logger log)
      Attempt to close the specified closeable and log any eventual error.
      Parameters:
      closeable - the closeable to close.
      log - the logger to use; if null no logging occurs.
    • getInputStreamAsByte

      public static byte[] getInputStreamAsByte(InputStream is) throws IOException
      Get the content of an input stream as an array of bytes. This method closes the input stream before terminating.
      Parameters:
      is - the input stream to read from.
      Returns:
      a byte array.
      Throws:
      IOException - if an IO error occurs.
    • copyFile

      public static long copyFile(File src, File dest) throws IOException
      Copy the data read from the specified input stream to the specified output stream. This method closes both streams before terminating.
      Parameters:
      src - the input stream to read from.
      dest - the output stream to write to.
      Returns:
      the number of bytes copied.
      Throws:
      IOException - if an I/O error occurs.
    • copyStream

      public static long copyStream(InputStream is, OutputStream os) throws IOException
      Copy the data read from the specified input stream to the specified output stream. This method closes both streams before terminating.
      Parameters:
      is - the input stream to read from.
      os - the output stream to write to.
      Returns:
      the number of bytes copied.
      Throws:
      IOException - if an I/O error occurs.
    • copyStream

      public static long copyStream(InputStream is, OutputStream os, boolean closeStreams) throws IOException
      Copy the data read from the specified input stream to the specified output stream. This method closes both streams before terminating.
      Parameters:
      is - the input stream to read from.
      os - the output stream to write to.
      closeStreams - true to cause the streams to be closed at the end of the copy, false otherwise.
      Returns:
      the number of bytes copied.
      Throws:
      IOException - if an I/O error occurs.
    • waitKeyPressed

      public static void waitKeyPressed() throws Exception
      Display a message and wait until a key is pressed.
      Throws:
      Exception - if any I/O error occurs.
    • waitKeyPressed

      public static void waitKeyPressed(String message) throws Exception
      Display a message and wait until a key is pressed.
      Parameters:
      message - the message to dispplay
      Throws:
      Exception - if any I/O error occurs.
    • waitKeyPressed

      public static void waitKeyPressed(char c, String message) throws Exception
      Display a message and wait until the specified key is pressed.
      Parameters:
      c - the character to wait for.
      message - the message to dispplay.
      Throws:
      Exception - if any I/O error occurs.
    • printf

      public static void printf(org.slf4j.Logger logger, String format, Object... params)
      Convenience method to print a formatted mssage to System.out and a specified logger.
      Parameters:
      logger - the logger to print to, may be null. If specified, the message is logged at INFO level.
      format - the message format as specified in Formatter.
      params - the formatted message parameters.