Class IOUtils

java.lang.Object
scriptella.util.IOUtils

public final class IOUtils extends Object
I/O utility methods.
Version:
1.0
Author:
Fyodor Kupolov
  • Method Details

    • closeSilently

      public static void closeSilently(Closeable closeable)
      Silently closes data.
      Parameters:
      closeable - data to close
    • closeSilently

      public static void closeSilently(Iterable<? extends Closeable> closeables)
      Silently closes a collection of objects.
      Parameters:
      closeables - iterable closeables. Null value allowed.
      See Also:
    • toString

      public static String toString(Reader reader) throws IOException
      Loads a reader content into a string.

      Note:The content length is limited by MAX_LENGTH characters.

      Parameters:
      reader - reader to load content from. Closed at the end of the operation.
      Returns:
      string representation of reader content.
      Throws:
      IOException
    • toString

      public static String toString(Reader reader, long maxLength) throws IOException
      Loads a reader content into a string.
      Parameters:
      reader - reader to load content from. Closed at the end of the operation.
      maxLength - max number of characters to read before throwing a Content Too Long Exception.
      Returns:
      string representation of reader content.
      Throws:
      IOException
    • toByteArray

      public static byte[] toByteArray(InputStream is) throws IOException
      Loads an input stream content into a byte array.

      Note:The content length is limited by MAX_LENGTH bytes.

      Parameters:
      is - stream to load. Closed at the end of the operation.
      Returns:
      stream bytes
      Throws:
      IOException - if I/O error occurs or stream length exceeds the MAX_LENGTH.
    • toByteArray

      public static byte[] toByteArray(InputStream is, long maxLength) throws IOException
      Loads an input stream content into a byte array.
      Parameters:
      is - stream to load. Closed at the end of the operation.
      maxLength - maxLength max number of bytes to read before throwing a Content Too Long Exception.
      Returns:
      stream bytes
      Throws:
      IOException
    • getOutputStream

      public static OutputStream getOutputStream(URL url) throws IOException
      Opens output stream for specified URL.

      This method is a helper for url.openConnection().getOutputStream(). Additionally a file: URLs are supported, see FileURLConnection doesn't implement getOutputStream()

      Parameters:
      url - URL to open an output stream.
      Returns:
      output stream for URL.
      Throws:
      IOException - if an I/O error occurs while creating the output stream.
    • getReader

      public static Reader getReader(InputStream is, String enc) throws UnsupportedEncodingException
      Returns:
      buffered reader for specified input stream.
      Throws:
      UnsupportedEncodingException
      See Also:
    • getReader

      public static Reader getReader(InputStream is, String enc, boolean buffered) throws UnsupportedEncodingException
      Returns reader for specified input stream and charset name.
      Parameters:
      is - source input stream.
      enc - charset name, null means default.
      buffered - true if buffered reader should be used.
      Returns:
      reader for inputstream.
      Throws:
      UnsupportedEncodingException - If the named charset is not supported
    • asBuffered

      public static BufferedReader asBuffered(Reader reader)
      Optionally makes a buffered reader from the specified one.

      If specified reader is buffered the object is returned unchanged.

      Parameters:
      reader - reader to convert.
      Returns:
      buffered reader.
    • asBuffered

      public static BufferedWriter asBuffered(Writer writer)
      Optionally makes a buffered writer from the specified one.

      If specified writer is buffered the object is returned unchanged.

      Parameters:
      writer - writer to convert.
      Returns:
      buffered writer.
    • getWriter

      public static Writer getWriter(OutputStream os, String enc) throws IOException
      Returns:
      buffered writer for specified output stream.
      Throws:
      IOException
      See Also:
    • getWriter

      public static Writer getWriter(OutputStream os, String enc, boolean buffered) throws IOException
      Returns writer for specified output stream and charset name.
      Parameters:
      os - source output stream.
      enc - charset name, null means default.
      buffered - true if buffered reader should be used.
      Returns:
      reader for inputstream.
      Throws:
      UnsupportedEncodingException - If the named charset is not supported
      IOException
    • toUrl

      public static URL toUrl(File file) throws MalformedURLException
      A replacement for a deprecated File.toURL() method.
      Parameters:
      file - file to convert to URL.
      Returns:
      URL representing the file location.
      Throws:
      MalformedURLException - If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL.
    • resolve

      public static URL resolve(URL base, String uri) throws MalformedURLException
      Resolves specified uri to a specified base URL.

      This method use URL(URL,String) constructor and handles additional checks if URL cannot be resolved by a standard mechanism.

      Typical example is handling windows absolute paths with forward slashes. These paths are malformed URIs, but Scriptella recognize them and converts to URL if no protocol handler has been registered.

      In future we can add support for default URL stream handlers in addition to the ones supported by the JRE.

      Parameters:
      base - base URL to use for resulution.
      uri - a relative or an absolute URI.
      Returns:
      URL resolved relatively to the base URL.
      Throws:
      MalformedURLException - if specified URI cannot be resolved.