Skip to content

Commit

Permalink
Moves more utils from libjitsi.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrozev committed Apr 1, 2019
1 parent 3a9823c commit fb8922e
Show file tree
Hide file tree
Showing 25 changed files with 2,761 additions and 28 deletions.
141 changes: 141 additions & 0 deletions src/main/java/org/jitsi/utils/ArrayUtils.java
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;
}
}
12 changes: 7 additions & 5 deletions src/main/java/org/jitsi/utils/AsyncQueueHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package org.jitsi.utils;

import org.jitsi.utils.concurrent.*;
import org.jitsi.utils.logging.*;

import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*;

/**
* Asynchronously reads items from provided {@link #queue} on separate thread
Expand All @@ -36,7 +38,7 @@ public final class AsyncQueueHandler<T>
* The {@link java.util.logging.Logger} used by the
* {@link AsyncQueueHandler} class and its instances for logging output.
*/
private static final java.util.logging.Logger logger
private static final Logger logger
= Logger.getLogger(AsyncQueueHandler.class.getName());

/**
Expand Down Expand Up @@ -140,7 +142,7 @@ public void run()
}
catch (Throwable e)
{
logger.warning("Failed to handle item: " + e);
logger.warn("Failed to handle item: " + e);
}
}
}
Expand Down Expand Up @@ -231,9 +233,9 @@ public void handleQueueItemsUntilEmpty()
*/
private void onYield()
{
if (logger.isLoggable(java.util.logging.Level.FINE))
if (logger.isDebugEnabled())
{
logger.fine("Yielding AsyncQueueHandler with ID = " + id);
logger.debug("Yielding AsyncQueueHandler with ID = " + id);
}

rescheduleReader();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/jitsi/utils/PacketQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package org.jitsi.utils;

import org.jitsi.utils.logging.*;
import org.jitsi.utils.stats.*;

import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*;

/**
* An abstract queue of packets. This is meant to eventually be able to be used
Expand Down Expand Up @@ -221,7 +223,7 @@ public PacketQueue(
asyncQueueHandler = null;
}

logger.fine("Initialized a PacketQueue instance with ID " + id);
logger.debug("Initialized a PacketQueue instance with ID " + id);
}

/**
Expand Down Expand Up @@ -309,7 +311,7 @@ private void doAdd(T pkt)
this.numDroppedPackets.incrementAndGet();
if (logDroppedPacket(numDroppedPackets))
{
logger.warning(
logger.warn(
"Packets dropped (id=" + id + "): " + numDroppedPackets);
}

Expand Down
180 changes: 180 additions & 0 deletions src/main/java/org/jitsi/utils/StringUtils.java
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);
}
}
}
}
Loading

0 comments on commit fb8922e

Please sign in to comment.