/*
This file is part of Airsonic.
Airsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Airsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Airsonic. If not, see
application/octet-stream
is returned.
*/
public static String getMimeType(String suffix) {
for (String[] map : MIME_TYPES) {
if (map[0].equalsIgnoreCase(suffix) || ('.' + map[0]).equalsIgnoreCase(suffix)) {
return map[1];
}
}
return "application/octet-stream";
}
public static String getMimeType(String suffix, boolean sonos) {
String result = getMimeType(suffix);
// Sonos doesn't work with "audio/mp4" but needs "audio/aac" for ALAC and AAC (in MP4 container)
return sonos && "audio/mp4".equals(result) ? "audio/aac" : result;
}
public static String getSuffix(String mimeType) {
for (String[] map : MIME_TYPES) {
if (map[1].equalsIgnoreCase(mimeType)) {
return map[0];
}
}
return null;
}
/**
* Converts a byte-count to a formatted string suitable for display to the user.
* For instance:
* format(918)
returns "918 B".format(98765)
returns "96 KB".format(1238476)
returns "1.2 MB"."u2 rem "greatest hits""
will return an array with
* three elements: {"u2", "rem", "greatest hits"}
*
* @param input The input string.
* @return Array of elements.
*/
public static String[] split(String input) {
if (input == null) {
return new String[0];
}
Pattern pattern = Pattern.compile("\".*?\"|\\S+");
Matcher matcher = pattern.matcher(input);
Listint
array.
*
* @param s String consisting of integers separated by whitespace.
* @return The corresponding array of ints.
* @throws NumberFormatException If string contains non-parseable text.
*/
public static int[] parseInts(String s) {
if (s == null) {
return new int[0];
}
String[] strings = StringUtils.split(s);
int[] ints = new int[strings.length];
for (int i = 0; i < strings.length; i++) {
ints[i] = Integer.parseInt(strings[i]);
}
return ints;
}
/**
* Determines whether a is equal to b, taking null into account.
*
* @return Whether a and b are equal, or both null.
*/
public static boolean isEqual(Object a, Object b) {
return Objects.equals(a, b);
}
/**
* Parses a locale from the given string.
*
* @param s The locale string. Should be formatted as per the documentation in {@link Locale#toString()}.
* @return The locale.
*/
public static Locale parseLocale(String s) {
if (s == null) {
return null;
}
String[] elements = s.split("_");
if (elements.length == 0) {
return new Locale(s, "", "");
}
if (elements.length == 1) {
return new Locale(elements[0], "", "");
}
if (elements.length == 2) {
return new Locale(elements[0], elements[1], "");
}
return new Locale(elements[0], elements[1], elements[2]);
}
/**
* URL-encodes the input value using UTF-8.
*/
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, StringUtil.ENCODING_UTF8);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x);
}
}
/**
* URL-decodes the input value using UTF-8.
*/
public static String urlDecode(String s) {
try {
return URLDecoder.decode(s, StringUtil.ENCODING_UTF8);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x);
}
}
/**
* Encodes the given string by using the hexadecimal representation of its UTF-8 bytes.
*
* @param s The string to encode.
* @return The encoded string.
*/
public static String utf8HexEncode(String s) {
if (s == null) {
return null;
}
byte[] utf8;
try {
utf8 = s.getBytes(ENCODING_UTF8);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x);
}
return String.valueOf(Hex.encodeHex(utf8));
}
/**
* Decodes the given string by using the hexadecimal representation of its UTF-8 bytes.
*
* @param s The string to decode.
* @return The decoded string.
* @throws Exception If an error occurs.
*/
public static String utf8HexDecode(String s) throws Exception {
if (s == null) {
return null;
}
return new String(Hex.decodeHex(s.toCharArray()), ENCODING_UTF8);
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param s Data to digest.
* @return MD5 digest as a hex string.
*/
public static String md5Hex(String s) {
if (s == null) {
return null;
}
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return new String(Hex.encodeHex(md5.digest(s.getBytes(ENCODING_UTF8))));
} catch (Exception x) {
throw new RuntimeException(x.getMessage(), x);
}
}
/**
* Returns the file part of an URL. For instance:
*
*
* getUrlFile("http://archive.ncsa.uiuc.edu:80/SDG/Software/Mosaic/Demo/url-primer.html")
*
*
* will return "url-primer.html".
*
* @param url The URL in question.
* @return The file part, or null
if no file can be resolved.
*/
public static String getUrlFile(String url) {
try {
String path = new URL(url).getPath();
if (StringUtils.isBlank(path) || path.endsWith("/")) {
return null;
}
File file = new File(path);
String filename = file.getName();
if (StringUtils.isBlank(filename)) {
return null;
}
return filename;
} catch (MalformedURLException x) {
return null;
}
}
/**
* Makes a given filename safe by replacing special characters like slashes ("/" and "\")
* with dashes ("-").
*
* @param filename The filename in question.
* @return The filename with special characters replaced by underscores.
*/
public static String fileSystemSafe(String filename) {
for (String s : FILE_SYSTEM_UNSAFE) {
filename = filename.replace(s, "-");
}
return filename;
}
public static String removeMarkup(String s) {
if (s == null) {
return null;
}
return s.replaceAll("<.*?>", "");
}
}