tinker.net.dongliu.apk.parser.utils.Utils Maven / Gradle / Ivy
The newest version!
package tinker.net.dongliu.apk.parser.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class Utils {
public static byte[] toByteArray(InputStream in) throws IOException {
try {
byte[] buf = new byte[1024*8];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
int len;
while ((len = in.read(buf)) != -1) {
bos.write(buf, 0, len);
}
return bos.toByteArray();
}
} finally {
in.close();
}
}
/**
* Copied fom commons StringUtils
* Joins the elements of the provided {@code Iterable} into
* a single String containing the provided elements.
* @param iterable
* @param separator
* @return value
*/
public static String join(final Iterable> iterable, final String separator) {
if (iterable == null) {
return null;
}
return join(iterable.iterator(), separator);
}
/**
* Copied fom commons StringUtils
* @param iterator
* @param separator
* @return value
*/
public static String join(final Iterator> iterator, final String separator) {
// handle null, zero and one elements before building a buffer
if (iterator == null) {
return null;
}
if (!iterator.hasNext()) {
return "";
}
final Object first = iterator.next();
if (!iterator.hasNext()) {
return first == null ? null : first.toString();
}
// two or more elements
final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
if (first != null) {
buf.append(first);
}
while (iterator.hasNext()) {
if (separator != null) {
buf.append(separator);
}
final Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
}
return buf.toString();
}
public static boolean isNumeric(final CharSequence cs) {
if (isEmpty(cs)) {
return false;
}
final int sz = cs.length();
for (int i = 0; i < sz; i++) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static String substringBefore(final String str, final String separator) {
if (Utils.isEmpty(str) || separator == null) {
return str;
}
if (separator.isEmpty()) {
return "";
}
final int pos = str.indexOf(separator);
if (pos == -1) {
return str;
}
return str.substring(0, pos);
}
}