Package io.muserver

Class Mutils


  • public class Mutils
    extends java.lang.Object
    Utility methods
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String NEWLINE
      The new-line character for the current platform, e.g.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T coalesce​(T... values)
      Returns the first non-null value from the given values (or null if all values are null)
      static void copy​(java.io.InputStream from, java.io.OutputStream to, int bufferSize)
      Copies an input stream to another stream
      static java.util.Date fromHttpDate​(java.lang.String date)
      Converts a date string as used in HTTP headers (defined in RFC 7321) into a date object
      static java.lang.String fullPath​(java.io.File file)
      Gets the canonical path of the given file, or if that throws an exception then gets the absolute path.
      static boolean hasValue​(java.lang.String val)
      Checks that a string is not null and has a length greater than 0.
      static java.lang.String htmlEncode​(java.lang.String value)
      Very basic HTML encoding, converting characters such as < to <lt;
      static java.lang.String join​(java.lang.String one, java.lang.String sep, java.lang.String two)
      Joins two strings with the given separator, unless the first string ends with the separator, or the second string begins with it.
      static void notNull​(java.lang.String name, java.lang.Object value)
      Throws an IllegalArgumentException if the given value is null
      static boolean nullOrEmpty​(java.lang.String val)
      Checks for a null string or string with a length of 9
      static java.lang.String pathAndQuery​(java.net.URI uri)
      Given a gets the raw path and (if present) querystring portion of a URI.
      static byte[] toByteArray​(java.io.InputStream source, int bufferSize)
      Reads the given input stream into a byte array and closes the input stream
      static java.nio.ByteBuffer toByteBuffer​(java.lang.String text)
      Converts a string to a ByteBuffer with UTF-8 encoding.
      static java.lang.String toHttpDate​(java.util.Date date)
      Converts a date into a date string as used in HTTP headers (defined in RFC 7321), for example Tue, 15 Nov 1994 08:12:31 GMT
      static java.lang.String trim​(java.lang.String value, java.lang.String toTrim)
      Trims the given string from the given value
      static java.lang.String urlDecode​(java.lang.String value)  
      static java.lang.String urlEncode​(java.lang.String value)  
      • Methods inherited from class java.lang.Object

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

      • NEWLINE

        public static final java.lang.String NEWLINE
        The new-line character for the current platform, e.g. \n in Linux or \r\n on Windows.
    • Method Detail

      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String value)
        Parameters:
        value - the value to encode
        Returns:
        Returns the UTF-8 URL encoded value
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String value)
        Parameters:
        value - the value to decode
        Returns:
        Returns the UTF-8 URL decoded value
      • copy

        public static void copy​(java.io.InputStream from,
                                java.io.OutputStream to,
                                int bufferSize)
                         throws java.io.IOException
        Copies an input stream to another stream
        Parameters:
        from - The source of the bytes
        to - The destination of the bytes
        bufferSize - The size of the byte buffer to use as bytes are copied
        Throws:
        java.io.IOException - Thrown if there is a problem reading from or writing to either stream
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream source,
                                         int bufferSize)
                                  throws java.io.IOException
        Reads the given input stream into a byte array and closes the input stream
        Parameters:
        source - The source of the bytes
        bufferSize - The size of the byte buffer to use when copying streams
        Returns:
        Returns a byte array
        Throws:
        java.io.IOException - If there is an error reading from the stream
      • nullOrEmpty

        public static boolean nullOrEmpty​(java.lang.String val)
        Checks for a null string or string with a length of 9
        Parameters:
        val - The value to check
        Returns:
        True if the value is null or a zero-length string.
      • hasValue

        public static boolean hasValue​(java.lang.String val)
        Checks that a string is not null and has a length greater than 0.
        Parameters:
        val - The value to check
        Returns:
        True if the string is 1 or more characters.
      • join

        public static java.lang.String join​(java.lang.String one,
                                            java.lang.String sep,
                                            java.lang.String two)
        Joins two strings with the given separator, unless the first string ends with the separator, or the second string begins with it. For example, the output one/two would be returned from join("one", "two", "/") or join("one/", "/two", "/") or join("one/", "two", "/") or join("one", "/two", "/")
        Parameters:
        one - The prefix
        sep - The separator to put between the two strings, if it is not there already
        two - The suffix
        Returns:
        The joined strings
      • trim

        public static java.lang.String trim​(java.lang.String value,
                                            java.lang.String toTrim)
        Trims the given string from the given value
        Parameters:
        value - The value to be trimmed
        toTrim - The string to trim
        Returns:
        The value with any occurrences of toTrim removed from the start and end of the value
      • notNull

        public static void notNull​(java.lang.String name,
                                   java.lang.Object value)
        Throws an IllegalArgumentException if the given value is null
        Parameters:
        name - The name of the variable to check
        value - The value to check
      • fullPath

        public static java.lang.String fullPath​(java.io.File file)
        Gets the canonical path of the given file, or if that throws an exception then gets the absolute path.
        Parameters:
        file - The file to check
        Returns:
        The canonical or absolute path of the given file
      • toHttpDate

        public static java.lang.String toHttpDate​(java.util.Date date)
        Converts a date into a date string as used in HTTP headers (defined in RFC 7321), for example Tue, 15 Nov 1994 08:12:31 GMT
        Parameters:
        date - A date to format
        Returns:
        The date as a formatted string
        Throws:
        java.lang.IllegalArgumentException - If the date is null
      • fromHttpDate

        public static java.util.Date fromHttpDate​(java.lang.String date)
                                           throws java.time.format.DateTimeParseException
        Converts a date string as used in HTTP headers (defined in RFC 7321) into a date object
        Parameters:
        date - A date formatted like Tue, 15 Nov 1994 08:12:31 GMT
        Returns:
        A date representing the string
        Throws:
        java.lang.IllegalArgumentException - If the date is null
        java.time.format.DateTimeParseException - If the date is not a valid HTTP date format
      • htmlEncode

        public static java.lang.String htmlEncode​(java.lang.String value)

        Very basic HTML encoding, converting characters such as < to <lt;

        Important: HTML encoding is a complex topic, and different schemes are needed depending on whether the string is destined for a tag name, attribute, the contents of a tag, CSS, or JavaScript etc. It is recommended that a fully featured text encoding library is used rather than this method.

        Parameters:
        value - A value
        Returns:
        A value that can be safely included inside HTML tags.
      • coalesce

        public static <T> T coalesce​(T... values)
        Returns the first non-null value from the given values (or null if all values are null)
        Type Parameters:
        T - The type of the value
        Parameters:
        values - An array of values
        Returns:
        The first object in the list that is not null (or null, if all are null)
      • toByteBuffer

        public static java.nio.ByteBuffer toByteBuffer​(java.lang.String text)
        Converts a string to a ByteBuffer with UTF-8 encoding.
        Parameters:
        text - Some text to convert
        Returns:
        A ByteBuffer containing the text as UTF-8 encoded bytes
        Throws:
        java.lang.IllegalArgumentException - if text is null
      • pathAndQuery

        public static java.lang.String pathAndQuery​(java.net.URI uri)

        Given a gets the raw path and (if present) querystring portion of a URI.

        Note: paths and querystrings are not URL decoded.

        Parameters:
        uri - The URI to get the info from
        Returns:
        A string such as /path?query=something