From b50eddb0a23f16c525c9b7d58e2a57ad2f6bf68e Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Thu, 22 Feb 2018 10:18:42 +0100 Subject: [PATCH] Utils: Remove toString() and toBytes() helpers, use new String() and getBytes() with Guava charsets instead. --- .../org/bitcoinj/core/BitcoinSerializer.java | 4 +- .../main/java/org/bitcoinj/core/Message.java | 4 +- .../main/java/org/bitcoinj/core/Utils.java | 39 ------------------- .../bitcoinj/wallet/DeterministicSeed.java | 2 +- 4 files changed, 7 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java b/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java index 2990a65f..56f8a613 100644 --- a/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java +++ b/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java @@ -20,6 +20,8 @@ package org.bitcoinj.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Charsets; + import java.io.IOException; import java.io.OutputStream; import java.nio.BufferUnderflowException; @@ -364,7 +366,7 @@ public class BitcoinSerializer extends MessageSerializer { for (; header[cursor] != 0 && cursor < COMMAND_LEN; cursor++) ; byte[] commandBytes = new byte[cursor]; System.arraycopy(header, 0, commandBytes, 0, cursor); - command = Utils.toString(commandBytes, "US-ASCII"); + command = new String(commandBytes, Charsets.US_ASCII); cursor = COMMAND_LEN; size = (int) readUint32(header, cursor); diff --git a/core/src/main/java/org/bitcoinj/core/Message.java b/core/src/main/java/org/bitcoinj/core/Message.java index e6f5cd2b..67fe35a7 100644 --- a/core/src/main/java/org/bitcoinj/core/Message.java +++ b/core/src/main/java/org/bitcoinj/core/Message.java @@ -20,6 +20,8 @@ package org.bitcoinj.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Charsets; + import java.io.*; import java.math.BigInteger; import java.util.Arrays; @@ -347,7 +349,7 @@ public abstract class Message { protected String readStr() throws ProtocolException { long length = readVarInt(); - return length == 0 ? "" : Utils.toString(readBytes((int) length), "UTF-8"); // optimization for empty strings + return length == 0 ? "" : new String(readBytes((int) length), Charsets.UTF_8); // optimization for empty strings } protected Sha256Hash readHash() throws ProtocolException { diff --git a/core/src/main/java/org/bitcoinj/core/Utils.java b/core/src/main/java/org/bitcoinj/core/Utils.java index a6daab63..574858b3 100644 --- a/core/src/main/java/org/bitcoinj/core/Utils.java +++ b/core/src/main/java/org/bitcoinj/core/Utils.java @@ -30,7 +30,6 @@ import org.spongycastle.crypto.digests.RIPEMD160Digest; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.URL; import java.text.DateFormat; @@ -439,44 +438,6 @@ public class Utils { 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. diff --git a/core/src/main/java/org/bitcoinj/wallet/DeterministicSeed.java b/core/src/main/java/org/bitcoinj/wallet/DeterministicSeed.java index b0182315..59cb5f54 100644 --- a/core/src/main/java/org/bitcoinj/wallet/DeterministicSeed.java +++ b/core/src/main/java/org/bitcoinj/wallet/DeterministicSeed.java @@ -236,7 +236,7 @@ public class DeterministicSeed implements EncryptableItem { } private static List decodeMnemonicCode(byte[] mnemonicCode) { - return decodeMnemonicCode(Utils.toString(mnemonicCode, "UTF-8")); + return decodeMnemonicCode(new String(mnemonicCode, Charsets.UTF_8)); } private static List decodeMnemonicCode(String mnemonicCode) {