Class ClientServerIO


  • public class ClientServerIO
    extends Object
    Utility class for manipulating URL, stream, and File data.
    • Constructor Detail

      • ClientServerIO

        public ClientServerIO()
    • Method Detail

      • getURLBytes

        public static URLReturnData getURLBytes​(String sURL)
                                         throws 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:
        IOException - IOException
      • getURLBytes

        public static URLReturnData getURLBytes​(String sURL,
                                                String cookieString)
                                         throws 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:
        IOException - IOException
      • loadProperties

        public static Properties loadProperties​(String propertiesPath)
                                         throws 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:
        IOException - if properties file cannot be read
      • makeXssSafe

        public static String makeXssSafe​(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​(InputStream sourceStream,
                                              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​(InputStream inputStream,
                                             byte[] byteArray,
                                             int offset,
                                             int length)
                                      throws 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:
        IOException - if an I/O error occurs.
      • getBytes

        public static byte[] getBytes​(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 String readStringFromInputStream​(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 File writeInputStreamToTempFile​(InputStream inputStream,
                                                      String prefix,
                                                      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,
                                         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​(File sourceFile,
                                    File destinationFile)
                             throws IOException
        Copies data from sourceFile to destinationFile.
        Parameters:
        sourceFile - Source file
        destinationFile - Destination file
        Throws:
        IOException - IOException
      • getFileBytes

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

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

        public static DataInputStream getURLInputStream​(String url)
                                                 throws 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:
        IOException - IOException
      • toBytes

        public static byte[] toBytes​(Object object)
                              throws 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:
        IOException - if an I/O exception occurs while writing to stream
        See Also:
        ObjectOutputStream.writeObject(java.lang.Object)
      • extractURLToFile

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