![JAR search and dependency download from the Maven repository](/logo.png)
com.formkiq.server.util.Strings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of greeting Show documentation
Show all versions of greeting Show documentation
Server-side integration for the FormKiQ ios application
The newest version!
package com.formkiq.server.util;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
/**
* String helper utilities.
*
*/
public final class Strings {
/** Default deliminator to use to encode strings. */
public static final String DEFAULT_DELIM = ":";
/** Regex to extract label / value. */
private static final Pattern EXTRACT_LABEL_VALUE = Pattern
.compile("^(.*)\\[(.*)\\]$");
/**
* Decodes string using deliminator and Base64.
* @param base64 {@link String}
* @param deliminator {@link String}
* @return String[]
*/
public static String[] decode(final String base64,
final String deliminator) {
byte[] bytes = Base64.decodeBase64(base64);
String s = new String(bytes, StandardCharsets.UTF_8);
return s.split(deliminator);
}
/**
* Encodes two strings together using deliminator and Base64.
* @param s0 {@link String}
* @param s1 {@link String}
* @param deliminator {@link String}
* @return {@link String}
*/
public static String encode(final String s0, final String s1,
final String deliminator) {
String encoded = s0 + deliminator + s1;
byte[] bytes = Base64
.encodeBase64(encoded.getBytes(StandardCharsets.UTF_8));
return new String(bytes, StandardCharsets.UTF_8);
}
/**
* Generate Token based off of Offset / Max Results.
* @param offset int
* @param maxResults int
* @return {@link String}
*/
public static String generateOffsetToken(final int offset,
final int maxResults) {
return encode("" + offset, "" + maxResults, DEFAULT_DELIM);
}
/**
* Get Max Results from Token.
* @param token {@link String}
* @param defaultMaxResults int
* @return int
*/
public static int getMaxResults(final String token,
final int defaultMaxResults) {
try {
String s = decode(token, DEFAULT_DELIM)[1];
return Integer.parseInt(s);
} catch (NullPointerException | ArrayIndexOutOfBoundsException
| NumberFormatException e) {
return defaultMaxResults;
}
}
/**
* Get Offset from token.
* @param token {@link String}
* @return int
*/
public static int getOffset(final String token) {
try {
String s = decode(token, DEFAULT_DELIM)[0];
return Integer.parseInt(s);
} catch (NullPointerException | ArrayIndexOutOfBoundsException
| NumberFormatException e) {
return 0;
}
}
/**
* private constructor.
*/
private Strings() {
}
/**
* Extract Label / Value from String.
* @param s {@link String}
* @return String[]
*/
public static String[] extractLabelAndValue(final String s) {
if (s != null) {
Matcher m = EXTRACT_LABEL_VALUE.matcher(s);
if (m.matches()) {
return new String[] { m.group(1), m.group(2)};
}
return new String[] { s, s };
}
return new String[] { "", "" };
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy