public class Mutils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
NEWLINE
The new-line character for the current platform, e.g.
|
Modifier and Type | Method and 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(InputStream from,
OutputStream to,
int bufferSize)
Copies an input stream to another stream
|
static Date |
fromHttpDate(String date)
Converts a date string as used in HTTP headers (defined in RFC 7321) into a date object
|
static String |
fullPath(File file)
Gets the canonical path of the given file, or if that throws an exception then gets the absolute path.
|
static boolean |
hasValue(String val)
Checks that a string is not null and has a length greater than 0.
|
static String |
htmlEncode(String value)
Very basic HTML encoding, converting characters such as
< to <lt; |
static String |
join(String one,
String sep,
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(String name,
Object value)
Throws an
IllegalArgumentException if the given value is null |
static boolean |
nullOrEmpty(String val)
Checks for a null string or string with a length of 9
|
static byte[] |
toByteArray(InputStream source,
int bufferSize)
Reads the given input stream into a byte array and closes the input stream
|
static ByteBuffer |
toByteBuffer(String text)
Converts a string to a ByteBuffer with UTF-8 encoding.
|
static String |
toHttpDate(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 String |
trim(String value,
String toTrim)
Trims the given string from the given value
|
static String |
urlDecode(String value) |
static String |
urlEncode(String value) |
public static final String NEWLINE
\n
in Linux or \r\n
on Windows.public static String urlEncode(String value)
value
- the value to encodepublic static String urlDecode(String value)
value
- the value to decodepublic static void copy(InputStream from, OutputStream to, int bufferSize) throws IOException
from
- The source of the bytesto
- The destination of the bytesbufferSize
- The size of the byte buffer to use as bytes are copiedIOException
- Thrown if there is a problem reading from or writing to either streampublic static byte[] toByteArray(InputStream source, int bufferSize) throws IOException
source
- The source of the bytesbufferSize
- The size of the byte buffer to use when copying streamsIOException
- If there is an error reading from the streampublic static boolean nullOrEmpty(String val)
val
- The value to checkpublic static boolean hasValue(String val)
val
- The value to checkpublic static String join(String one, String sep, String two)
one/two
would be returned from join("one", "two", "/")
or join("one/", "/two", "/")
or join("one/", "two", "/")
or
join("one", "/two", "/")
one
- The prefixsep
- The separator to put between the two strings, if it is not there alreadytwo
- The suffixpublic static String trim(String value, String toTrim)
value
- The value to be trimmedtoTrim
- The string to trimpublic static void notNull(String name, Object value)
IllegalArgumentException
if the given value is nullname
- The name of the variable to checkvalue
- The value to checkpublic static String fullPath(File file)
file
- The file to checkpublic static String toHttpDate(Date date)
Tue, 15 Nov 1994 08:12:31 GMT
date
- A date to formatIllegalArgumentException
- If the date is nullpublic static Date fromHttpDate(String date) throws DateTimeParseException
date
- A date formatted like Tue, 15 Nov 1994 08:12:31 GMT
IllegalArgumentException
- If the date is nullDateTimeParseException
- If the date is not a valid HTTP date formatpublic static String htmlEncode(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.
value
- A valuepublic static <T> T coalesce(T... values)
T
- The type of the valuevalues
- An array of valuespublic static ByteBuffer toByteBuffer(String text)
text
- Some text to convertIllegalArgumentException
- if text is nullCopyright © 2017–2021. All rights reserved.