Class ClientServerIO


  • public class ClientServerIO
    extends java.lang.Object
    Utility class for manipulating URL, stream, and File data.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String UTF_8
      Constant for UTF-8 name.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClientServerIO()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyFile​(java.io.File sourceFile, java.io.File destinationFile)
      Copies data from sourceFile to destinationFile.
      static void extractURLToFile​(java.io.File file, java.net.URL url)
      Writes the content provided at a specified URL into a file.
      static byte[] getBytes​(java.io.InputStream inputStream)
      This method returns a byte array containing the data in the specified stream.
      static byte[] getFileBytes​(java.io.File file)
      Returns a byte array containing a File's content.
      static java.lang.String getFileContent​(java.io.File file)
      Returns the text content of a file.
      static URLReturnData getURLBytes​(java.lang.String sURL)
      Returns the content provided at a specified URL.
      static URLReturnData getURLBytes​(java.lang.String sURL, java.lang.String cookieString)
      Returns the content provided at a specified URL.
      static java.io.DataInputStream getURLInputStream​(java.lang.String url)
      Returns the content provided at a specified URL as a stream.
      static java.util.Properties loadProperties​(java.lang.String propertiesPath)
      Loads an instance of java.util.Properties from the specified file path.
      static java.lang.String makeXssSafe​(java.lang.String input)
      Remove html entities from a string to prevent malicious Javascript and cross site scripting attacks.
      static int readBlocking​(java.io.InputStream inputStream, byte[] byteArray, int offset, int length)
      Reads exactly length bytes from the input stream into the byte array.
      static java.lang.String readStringFromInputStream​(java.io.InputStream inputStream)
      Reads data from the specified inputStream and returns it as a String.
      static void saveFileBytes​(byte[] data, java.io.File file)
      Saves the specified byte array to the specified File.
      static byte[] toBytes​(java.lang.Object object)
      Serializes an object out to an ObjectOutputStream and then returns the serialized content as a byte array.
      static boolean transferStreams​(java.io.InputStream sourceStream, java.io.OutputStream destinationStream)
      Copy an InputStream to an OutputStream, until EOF.
      static java.io.File writeInputStreamToTempFile​(java.io.InputStream inputStream, java.lang.String prefix, java.lang.String suffix)
      Writes the data from the specified InputStream to a temp file.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UTF_8

        public static final java.lang.String UTF_8
        Constant for UTF-8 name.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ClientServerIO

        public ClientServerIO()
    • Method Detail

      • getURLBytes

        public static URLReturnData getURLBytes​(java.lang.String sURL)
                                         throws java.io.IOException
        Returns the content provided at a specified URL.
        Parameters:
        sURL - The address of content to retrieve
        Returns:
        An object containing the response header fields and the response as a byte array.
        Throws:
        java.io.IOException - IOException
      • getURLBytes

        public static URLReturnData getURLBytes​(java.lang.String sURL,
                                                java.lang.String cookieString)
                                         throws java.io.IOException
        Returns the content provided at a specified URL.
        Parameters:
        sURL - The address of content to retrieve
        cookieString - A cookie string to add to the request
        Returns:
        An object containing the response header fields and the response as a byte array.
        Throws:
        java.io.IOException - IOException
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.String propertiesPath)
                                                   throws java.io.IOException

        Loads an instance of java.util.Properties from the specified file path. The specified file is expected to be a text file following the java.util.Properties format such as the following:

         color=green
         city=Boston
         state=Massachusetts
         
        Parameters:
        propertiesPath - path to properties file
        Returns:
        loaded properties object
        Throws:
        java.io.IOException - if properties file cannot be read
      • makeXssSafe

        public static java.lang.String makeXssSafe​(java.lang.String input)
        Remove html entities from a string to prevent malicious Javascript and cross site scripting attacks.
        Parameters:
        input - String to be sanitized
        Returns:
        Sanitized string
      • transferStreams

        public static boolean transferStreams​(java.io.InputStream sourceStream,
                                              java.io.OutputStream destinationStream)
        Copy an InputStream to an OutputStream, until EOF. Use only when you don't know the length.
        Parameters:
        sourceStream - Stream to be copied. The stream will not be closed by this method.
        destinationStream - Stream to be copied to. The stream will not be closed by this method.
        Returns:
        true if the copy was successful.
      • readBlocking

        public static final int readBlocking​(java.io.InputStream inputStream,
                                             byte[] byteArray,
                                             int offset,
                                             int length)
                                      throws java.io.IOException

        Reads exactly length bytes from the input stream into the byte array. This method reads repeatedly from the underlying stream until all the bytes are read. InputStream.read is often documented to block like this, but in actuality it does not always do so, and returns early with just a few bytes. readBlocking blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown. You will always get as many bytes as you asked for unless you get an EOF or other exception. Unlike readFully, you find out how many bytes you did get.

        Parameters:
        inputStream - Source stream
        byteArray - the buffer into which the data is read.
        offset - the start offset of the data.
        length - the number of bytes to read.
        Returns:
        number of bytes actually read.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getBytes

        public static byte[] getBytes​(java.io.InputStream inputStream)
        This method returns a byte array containing the data in the specified stream.
        Parameters:
        inputStream - The input stream to read
        Returns:
        a byte array containing the data in the stream.
      • readStringFromInputStream

        public static java.lang.String readStringFromInputStream​(java.io.InputStream inputStream)
        Reads data from the specified inputStream and returns it as a String.
        Parameters:
        inputStream - Source stream
        Returns:
        UTF8 String data from stream
      • writeInputStreamToTempFile

        public static java.io.File writeInputStreamToTempFile​(java.io.InputStream inputStream,
                                                              java.lang.String prefix,
                                                              java.lang.String suffix)
        Writes the data from the specified InputStream to a temp file.
        Parameters:
        inputStream - stream to copy data from
        prefix - prefix to the temp filename
        suffix - suffix to the temp filename
        Returns:
        reference to temp file
      • saveFileBytes

        public static void saveFileBytes​(byte[] data,
                                         java.io.File file)
        Saves the specified byte array to the specified File.
        Parameters:
        data - data to save
        file - file reference to save data to
      • copyFile

        public static void copyFile​(java.io.File sourceFile,
                                    java.io.File destinationFile)
                             throws java.io.IOException
        Copies data from sourceFile to destinationFile.
        Parameters:
        sourceFile - Source file
        destinationFile - Destination file
        Throws:
        java.io.IOException - IOException
      • getFileBytes

        public static byte[] getFileBytes​(java.io.File file)
                                   throws java.io.IOException
        Returns a byte array containing a File's content.
        Parameters:
        file - File reference to read from
        Returns:
        byte content of file
        Throws:
        java.io.IOException - if data could not be read from file
      • getFileContent

        public static java.lang.String getFileContent​(java.io.File file)
                                               throws java.io.IOException
        Returns the text content of a file.
        Parameters:
        file - File reference to read from
        Returns:
        text content of file
        Throws:
        java.io.IOException - if data could not be read from file
      • getURLInputStream

        public static java.io.DataInputStream getURLInputStream​(java.lang.String url)
                                                         throws java.io.IOException
        Returns the content provided at a specified URL as a stream.
        Parameters:
        url - The address of content to retrieve
        Returns:
        a stream containing the data returned from the specified site
        Throws:
        java.io.IOException - IOException
      • toBytes

        public static byte[] toBytes​(java.lang.Object object)
                              throws java.io.IOException
        Serializes an object out to an ObjectOutputStream and then returns the serialized content as a byte array.
        Parameters:
        object - object to be serialized
        Returns:
        byte array of serialized object
        Throws:
        java.io.IOException - if an I/O exception occurs while writing to stream
        See Also:
        ObjectOutputStream.writeObject(java.lang.Object)
      • extractURLToFile

        public static void extractURLToFile​(java.io.File file,
                                            java.net.URL url)
                                     throws java.io.IOException
        Writes the content provided at a specified URL into a file.
        Parameters:
        file - Destination file
        url - The address of content to retrieve
        Throws:
        java.io.IOException - IOException