forked from jitsi/jitsi-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
2,761 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright @ 2018 - Present 8x8, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.utils; | ||
|
||
import java.lang.reflect.*; | ||
import java.util.*; | ||
|
||
/** | ||
* | ||
* @author Lyubomir Marinov | ||
*/ | ||
public final class ArrayUtils | ||
{ | ||
/** | ||
* Adds a specific element to a specific array with a specific component | ||
* type if the array does not contain the element yet. | ||
* | ||
* @param array the array to add <tt>element</tt> to | ||
* @param componentType the component type of <tt>array</tt> | ||
* @param element the element to add to <tt>array</tt> | ||
* @return an array with the specified <tt>componentType</tt> and | ||
* containing <tt>element</tt>. If <tt>array</tt> contained <tt>element</tt> | ||
* already, returns <tt>array</tt>. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> T[] add(T[] array, Class<T> componentType, T element) | ||
{ | ||
if (element == null) | ||
throw new NullPointerException("element"); | ||
|
||
if (array == null) | ||
{ | ||
array = (T[]) Array.newInstance(componentType, 1); | ||
} | ||
else | ||
{ | ||
for (T a : array) | ||
{ | ||
if (element.equals(a)) | ||
return array; | ||
} | ||
|
||
T[] newArray | ||
= (T[]) Array.newInstance(componentType, array.length + 1); | ||
|
||
System.arraycopy(array, 0, newArray, 0, array.length); | ||
array = newArray; | ||
} | ||
array[array.length - 1] = element; | ||
return array; | ||
} | ||
|
||
/** | ||
* Inserts the given element into an open (null) slot in the array if there | ||
* is one, otherwise creates a new array and adds all existing elements | ||
* and the given element | ||
* @param element the element to add | ||
* @param array the array to add to, if possible | ||
* @param componentType the class type of the array (used if a new one | ||
* needs to be allocated) | ||
* @param <T> the type of the element | ||
* @return an array containing all the elements in the array that was passed, | ||
* as well as the given element. May or may not be the original array. | ||
*/ | ||
public static <T> T[] insert(T element, T[] array, Class<T> componentType) | ||
{ | ||
T[] arrayToReturn = array; | ||
boolean inserted = false; | ||
for (int i = 0; i < array.length; ++i) | ||
{ | ||
if (array[i] == null) | ||
{ | ||
array[i] = element; | ||
inserted = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!inserted) | ||
{ | ||
arrayToReturn = add(array, componentType, element); | ||
} | ||
return arrayToReturn; | ||
} | ||
|
||
/** Prevents the initialization of new {@code ArrayUtils} instances. */ | ||
private ArrayUtils() | ||
{ | ||
} | ||
|
||
/** | ||
* Concatenates two arrays. | ||
* | ||
* @param first | ||
* @param second | ||
* @param <T> | ||
* @return | ||
*/ | ||
public static <T> T[] concat(T[] first, T[] second) | ||
{ | ||
if (isNullOrEmpty(first)) | ||
{ | ||
return second; | ||
} | ||
else if (isNullOrEmpty(second)) | ||
{ | ||
return first; | ||
} | ||
else | ||
{ | ||
T[] result = Arrays.copyOf(first, first.length + second.length); | ||
System.arraycopy(second, 0, result, first.length, second.length); | ||
return result; | ||
} | ||
} | ||
|
||
/** | ||
* Tests whether the array passed in as an argument is null or empty. | ||
* | ||
* @param array | ||
* @param <T> | ||
* @return | ||
*/ | ||
public static <T> boolean isNullOrEmpty(T[] array) | ||
{ | ||
return array == null || array.length == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Copyright @ 2019 - Present 8x8, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.utils; | ||
|
||
import java.io.*; | ||
import java.nio.charset.*; | ||
|
||
/** | ||
* Implements utility functions to facilitate work with <tt>String</tt>s. | ||
* | ||
* @author Grigorii Balutsel | ||
* @author Emil Ivov | ||
*/ | ||
public final class StringUtils | ||
{ | ||
/** | ||
* Prevents the initialization of <tt>StringUtils</tt> instances because the | ||
* <tt>StringUtils</tt> class implements utility function only. | ||
*/ | ||
private StringUtils() | ||
{ | ||
} | ||
|
||
/** | ||
* Checks whether a string is {@code null} or blank (empty or whitespace). | ||
* | ||
* @param s the string to analyze. | ||
* @return {@code true} if the string is {@code null} or blank. | ||
*/ | ||
public static boolean isNullOrEmpty(String s) | ||
{ | ||
return isNullOrEmpty(s, true); | ||
} | ||
|
||
/** | ||
* Indicates whether string is <tt>null</tt> or empty. | ||
* | ||
* @param s the string to analyze. | ||
* @param trim indicates whether to trim the string. | ||
* @return <tt>true</tt> if string is <tt>null</tt> or empty. | ||
*/ | ||
public static boolean isNullOrEmpty(String s, boolean trim) | ||
{ | ||
if (s == null) | ||
return true; | ||
if (trim) | ||
s = s.trim(); | ||
return s.length() == 0; | ||
} | ||
|
||
/** | ||
* Creates <tt>InputStream</tt> from the string in UTF8 encoding. | ||
* | ||
* @param string the string to convert. | ||
* @return the <tt>InputStream</tt>. | ||
* @throws UnsupportedEncodingException if UTF8 is unsupported. | ||
*/ | ||
public static InputStream fromString(String string) | ||
throws UnsupportedEncodingException | ||
{ | ||
return fromString(string, "UTF-8"); | ||
} | ||
|
||
/** | ||
* Creates <tt>InputStream</tt> from the string in the specified encoding. | ||
* | ||
* @param string the string to convert. | ||
* @param encoding the encoding | ||
* @return the <tt>InputStream</tt>. | ||
* @throws UnsupportedEncodingException if the encoding is unsupported. | ||
*/ | ||
public static InputStream fromString(String string, String encoding) | ||
throws UnsupportedEncodingException | ||
{ | ||
return new ByteArrayInputStream(string.getBytes(encoding)); | ||
} | ||
|
||
/** | ||
* Returns the UTF8 bytes for <tt>string</tt> and handles the unlikely case | ||
* where UTF-8 is not supported. | ||
* | ||
* @param string the <tt>String</tt> whose bytes we'd like to obtain. | ||
* @return <tt>string</tt>'s bytes. | ||
*/ | ||
public static byte[] getUTF8Bytes(String string) | ||
{ | ||
try | ||
{ | ||
return string.getBytes("UTF-8"); | ||
} | ||
catch(UnsupportedEncodingException exc) | ||
{ | ||
// shouldn't happen. UTF-8 is always supported, anyways ... if | ||
// this happens, we'll cheat | ||
return string.getBytes(); | ||
} | ||
} | ||
|
||
/** | ||
* Converts <tt>string</tt> into an UTF8 <tt>String</tt> and handles the | ||
* unlikely case where UTF-8 is not supported. | ||
* | ||
* @param bytes the <tt>byte</tt> array that we'd like to convert into a | ||
* <tt>String</tt>. | ||
* @return the UTF-8 <tt>String</tt>. | ||
*/ | ||
public static String getUTF8String(byte[] bytes) | ||
{ | ||
try | ||
{ | ||
return new String(bytes, "UTF-8"); | ||
} | ||
catch(UnsupportedEncodingException exc) | ||
{ | ||
// shouldn't happen. UTF-8 is always supported, anyways ... if | ||
// this happens, we'll cheat | ||
return new String(bytes); | ||
} | ||
} | ||
|
||
/** | ||
* Indicates whether the given string contains any letters. | ||
* | ||
* @param string the string to check for letters | ||
* @return <tt>true</tt> if the given string contains letters; | ||
* <tt>false</tt>, otherwise | ||
*/ | ||
public static boolean containsLetters(String string) | ||
{ | ||
for (int i = 0; i < string.length(); i++) | ||
{ | ||
if (Character.isLetter(string.charAt(i))) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Initializes a new <tt>String</tt> instance by decoding a specified array | ||
* of bytes (mostly used by JNI). | ||
* | ||
* @param bytes the bytes to be decoded into characters/a new | ||
* <tt>String</tt> instance | ||
* @return a new <tt>String</tt> instance whose characters were decoded from | ||
* the specified <tt>bytes</tt> | ||
*/ | ||
public static String newString(byte[] bytes) | ||
{ | ||
if ((bytes == null) || (bytes.length == 0)) | ||
return null; | ||
else | ||
{ | ||
Charset defaultCharset = Charset.defaultCharset(); | ||
String charsetName | ||
= (defaultCharset == null) ? "UTF-8" : defaultCharset.name(); | ||
|
||
try | ||
{ | ||
return new String(bytes, charsetName); | ||
} | ||
catch (UnsupportedEncodingException ueex) | ||
{ | ||
return new String(bytes); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.