simplify creation of payload input stream
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpUtils.java
index a492f2e..ad2a798 100644 (file)
@@ -35,7 +35,7 @@ public class FcpUtils {
         * @throws NumberFormatException
         *             if a value can not be converted to a number
         */
-       public static int[] parseMultiIntegerField(String field) throws NumberFormatException {
+       public static int[] decodeMultiIntegerField(String field) throws NumberFormatException {
                StringTokenizer fieldTokens = new StringTokenizer(field, ";");
                int[] result = new int[fieldTokens.countTokens()];
                int counter = 0;
@@ -46,4 +46,102 @@ public class FcpUtils {
                return result;
        }
 
+       /**
+        * Encodes the given integer array into a string, separating the values by
+        * ‘;’.
+        * 
+        * @param values
+        *            The values to encode
+        * @return The encoded values
+        */
+       public static String encodeMultiIntegerField(int[] values) {
+               StringBuilder encodedField = new StringBuilder();
+               for (int value: values) {
+                       if (encodedField.length() > 0) {
+                               encodedField.append(';');
+                       }
+                       encodedField.append(value);
+               }
+               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;
+               }
+       }
+
 }