simplify creation of payload input stream
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpUtils.java
index 3a5d5ba..ad2a798 100644 (file)
@@ -65,4 +65,83 @@ public class FcpUtils {
                return encodedField.toString();
        }
 
+       /**
+        * Encodes the given string array into a string, separating the values by
+        * ‘;’.
+        * 
+        * @param values
+        *            The values to encode
+        * @return The encoded values
+        */
+       public static String encodeMultiStringField(String[] values) {
+               StringBuilder encodedField = new StringBuilder();
+               for (String value: values) {
+                       if (encodedField.length() > 0) {
+                               encodedField.append(';');
+                       }
+                       encodedField.append(value);
+               }
+               return encodedField.toString();
+       }
+
+       /**
+        * Tries to parse the given string into an int, returning <code>-1</code>
+        * if the string can not be parsed.
+        * 
+        * @param value
+        *            The string to parse
+        * @return The parsed int, or <code>-1</code>
+        */
+       public static int safeParseInt(String value) {
+               return safeParseInt(value, -1);
+       }
+
+       /**
+        * Tries to parse the given string into an int, returning
+        * <code>defaultValue</code> if the string can not be parsed.
+        * 
+        * @param value
+        *            The string to parse
+        * @param defaultValue
+        *            The value to return if the string can not be parsed.
+        * @return The parsed int, or <code>defaultValue</code>
+        */
+       public static int safeParseInt(String value, int defaultValue) {
+               try {
+                       return Integer.valueOf(value);
+               } catch (NumberFormatException nfe1) {
+                       return defaultValue;
+               }
+       }
+
+       /**
+        * Tries to parse the given string into an long, returning <code>-1</code>
+        * if the string can not be parsed.
+        * 
+        * @param value
+        *            The string to parse
+        * @return The parsed long, or <code>-1</code>
+        */
+       public static long safeParseLong(String value) {
+               return safeParseLong(value, -1);
+       }
+
+       /**
+        * Tries to parse the given string into an long, returning
+        * <code>defaultValue</code> if the string can not be parsed.
+        * 
+        * @param value
+        *            The string to parse
+        * @param defaultValue
+        *            The value to return if the string can not be parsed.
+        * @return The parsed long, or <code>defaultValue</code>
+        */
+       public static long safeParseLong(String value, long defaultValue) {
+               try {
+                       return Integer.valueOf(value);
+               } catch (NumberFormatException nfe1) {
+                       return defaultValue;
+               }
+       }
+
 }