Class DataFormatTools


  • public class DataFormatTools
    extends java.lang.Object
    Helper methods related to strings and maps.
    • Constructor Summary

      Constructors 
      Constructor Description
      DataFormatTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean arrayRangeEquals​(byte[] array, int offset, byte[] segment)  
      static java.util.Map<java.lang.String,​java.lang.String> convertToMapLayout​(java.lang.String response)  
      static java.util.Map<java.lang.String,​java.lang.String> convertToMapLayout​(java.util.Collection<org.apache.http.NameValuePair> pairs)
      Converts name value pairs to a map, with the last entry for each name being present.
      static java.util.Map<java.lang.String,​java.lang.String> decodeUrlEncodedItems​(java.lang.String input, boolean escapedSeparator)  
      static <T> T defaultOnNull​(T value, T defaultValue)
      Returns the specified default value if the value itself is null.
      static long durationTextToMillis​(java.lang.String durationText)
      Converts duration in the format HH:mm:ss (or mm:ss or ss) to milliseconds.
      static java.lang.String extractAfter​(java.lang.String haystack, java.lang.String start)  
      static java.lang.String extractAfter​(java.lang.String haystack, java.lang.String[] candidates)  
      static java.lang.String extractBetween​(java.lang.String haystack, DataFormatTools.TextRange[] candidates)  
      static java.lang.String extractBetween​(java.lang.String haystack, java.lang.String start, java.lang.String end)
      Extract text between the first subsequent occurrences of start and end in haystack
      static boolean isNullOrEmpty​(java.lang.String text)  
      static java.lang.String readNullableText​(java.io.DataInput input)
      Reads a string from input which may be null.
      static java.lang.String[] streamToLines​(java.io.InputStream inputStream, java.nio.charset.Charset charset)
      Consumes a stream and returns it as lines.
      static void writeNullableText​(java.io.DataOutput output, java.lang.String text)
      Writes a string to output with the additional information whether it is null or not.
      • Methods inherited from class java.lang.Object

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

      • DataFormatTools

        public DataFormatTools()
    • Method Detail

      • extractBetween

        public static java.lang.String extractBetween​(java.lang.String haystack,
                                                      java.lang.String start,
                                                      java.lang.String end)
        Extract text between the first subsequent occurrences of start and end in haystack
        Parameters:
        haystack - The text to search from
        start - The text after which to start extracting
        end - The text before which to stop extracting
        Returns:
        The extracted string
      • extractBetween

        public static java.lang.String extractBetween​(java.lang.String haystack,
                                                      DataFormatTools.TextRange[] candidates)
      • extractAfter

        public static java.lang.String extractAfter​(java.lang.String haystack,
                                                    java.lang.String start)
      • extractAfter

        public static java.lang.String extractAfter​(java.lang.String haystack,
                                                    java.lang.String[] candidates)
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.String text)
      • convertToMapLayout

        public static java.util.Map<java.lang.String,​java.lang.String> convertToMapLayout​(java.util.Collection<org.apache.http.NameValuePair> pairs)
        Converts name value pairs to a map, with the last entry for each name being present.
        Parameters:
        pairs - Name value pairs to convert
        Returns:
        The resulting map
      • convertToMapLayout

        public static java.util.Map<java.lang.String,​java.lang.String> convertToMapLayout​(java.lang.String response)
      • decodeUrlEncodedItems

        public static java.util.Map<java.lang.String,​java.lang.String> decodeUrlEncodedItems​(java.lang.String input,
                                                                                                   boolean escapedSeparator)
      • defaultOnNull

        public static <T> T defaultOnNull​(T value,
                                          T defaultValue)
        Returns the specified default value if the value itself is null.
        Type Parameters:
        T - The type of the value
        Parameters:
        value - Value to check
        defaultValue - Default value to return if value is null
        Returns:
        Value or default value
      • streamToLines

        public static java.lang.String[] streamToLines​(java.io.InputStream inputStream,
                                                       java.nio.charset.Charset charset)
                                                throws java.io.IOException
        Consumes a stream and returns it as lines.
        Parameters:
        inputStream - Input stream to consume.
        charset - Character set of the stream
        Returns:
        Lines from the stream
        Throws:
        java.io.IOException - On read error
      • durationTextToMillis

        public static long durationTextToMillis​(java.lang.String durationText)
        Converts duration in the format HH:mm:ss (or mm:ss or ss) to milliseconds. Does not support day count.
        Parameters:
        durationText - Duration in text format.
        Returns:
        Duration in milliseconds.
      • writeNullableText

        public static void writeNullableText​(java.io.DataOutput output,
                                             java.lang.String text)
                                      throws java.io.IOException
        Writes a string to output with the additional information whether it is null or not. Compatible with readNullableText(DataInput).
        Parameters:
        output - Output to write to.
        text - Text to write.
        Throws:
        java.io.IOException - On write error.
      • readNullableText

        public static java.lang.String readNullableText​(java.io.DataInput input)
                                                 throws java.io.IOException
        Reads a string from input which may be null. Compatible with writeNullableText(DataOutput, String).
        Parameters:
        input - Input to read from.
        Returns:
        The string that was read, or null.
        Throws:
        java.io.IOException - On read error.
      • arrayRangeEquals

        public static boolean arrayRangeEquals​(byte[] array,
                                               int offset,
                                               byte[] segment)