From ea08e761ebc6530d32f20c0ca269322a9e18a4dc Mon Sep 17 00:00:00 2001 From: brenzosa Date: Wed, 16 Sep 2015 13:55:05 +0800 Subject: [PATCH] Fixed imported Utils package and included methods from bitcoinj version of Utils needed by DownloadProgresstracker --- .../java/io/xpydev/paycoinj/core/Utils.java | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/io/xpydev/paycoinj/core/Utils.java b/core/src/main/java/io/xpydev/paycoinj/core/Utils.java index 885dffb5..0ad4e480 100644 --- a/core/src/main/java/io/xpydev/paycoinj/core/Utils.java +++ b/core/src/main/java/io/xpydev/paycoinj/core/Utils.java @@ -30,11 +30,15 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; @@ -61,6 +65,8 @@ public class Utils { public static final String Paycoin_SIGNED_MESSAGE_HEADER = "Paycoin Signed Message:\n"; public static final byte[] Paycoin_SIGNED_MESSAGE_HEADER_BYTES = Paycoin_SIGNED_MESSAGE_HEADER.getBytes(Charsets.UTF_8); + private static final Joiner SPACE_JOINER = Joiner.on(" "); + private static BlockingQueue mockSleepQueue; /** @@ -443,6 +449,40 @@ public static long currentTimeSeconds() { return currentTimeMillis() / 1000; } + private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); + + /** + * Formats a given date+time value to an ISO 8601 string. + * @param dateTime value to format, as a Date + */ + public static String dateTimeFormat(Date dateTime) { + DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); + iso8601.setTimeZone(UTC); + return iso8601.format(dateTime); + } + + /** + * Formats a given date+time value to an ISO 8601 string. + * @param dateTime value to format, unix time (ms) + */ + public static String dateTimeFormat(long dateTime) { + DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); + iso8601.setTimeZone(UTC); + return iso8601.format(dateTime); + } + + /** + * Returns a string containing the string representation of the given items, + * delimited by a single space character. + * + * @param items the items to join + * @param the item type + * @return the joined space-delimited string + */ + public static String join(Iterable items) { + return SPACE_JOINER.join(items); + } + public static byte[] copyOf(byte[] in, int length) { byte[] out = new byte[length]; System.arraycopy(in, 0, out, 0, Math.min(length, in.length)); @@ -458,6 +498,44 @@ public static byte[] appendByte(byte[] bytes, byte b) { return result; } + /** + * Constructs a new String by decoding the given bytes using the specified charset. + *

+ * This is a convenience method which wraps the checked exception with a RuntimeException. + * The exception can never occur given the charsets + * US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16LE or UTF-16BE. + * + * @param bytes the bytes to be decoded into characters + * @param charsetName the name of a supported {@linkplain java.nio.charset.Charset charset} + * @return the decoded String + */ + public static String toString(byte[] bytes, String charsetName) { + try { + return new String(bytes, charsetName); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + /** + * Encodes the given string into a sequence of bytes using the named charset. + *

+ * This is a convenience method which wraps the checked exception with a RuntimeException. + * The exception can never occur given the charsets + * US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16LE or UTF-16BE. + * + * @param str the string to encode into bytes + * @param charsetName the name of a supported {@linkplain java.nio.charset.Charset charset} + * @return the encoded bytes + */ + public static byte[] toBytes(CharSequence str, String charsetName) { + try { + return str.toString().getBytes(charsetName); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + /** * Attempts to parse the given string as arbitrary-length hex or base58 and then return the results, or null if * neither parse was successful. @@ -550,9 +628,13 @@ public static void finishMockSleep() { } } + private static int isAndroid = -1; public static boolean isAndroidRuntime() { + if (isAndroid == -1) { final String runtime = System.getProperty("java.runtime.name"); - return runtime != null && runtime.equals("Android Runtime"); + isAndroid = (runtime != null && runtime.equals("Android Runtime")) ? 1 : 0; + } + return isAndroid == 1; } private static class Pair implements Comparable { @@ -602,4 +684,22 @@ public static String getResourceAsString(URL url) throws IOException { return Joiner.on('\n').join(lines); } + // Can't use Closeable here because it's Java 7 only and Android devices only got that with KitKat. + public static InputStream closeUnchecked(InputStream stream) { + try { + stream.close(); + return stream; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static OutputStream closeUnchecked(OutputStream stream) { + try { + stream.close(); + return stream; + } catch (IOException e) { + throw new RuntimeException(e); + } + } }