3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 18:25:51 +00:00

Utils: Remove toString() and toBytes() helpers, use new String() and getBytes() with Guava charsets instead.

This commit is contained in:
Andreas Schildbach 2018-02-22 10:18:42 +01:00
parent b6ecc380b8
commit b50eddb0a2
4 changed files with 7 additions and 42 deletions

View File

@ -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);

View File

@ -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 {

View File

@ -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.
* <p>
* 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.
* <p>
* 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.

View File

@ -236,7 +236,7 @@ public class DeterministicSeed implements EncryptableItem {
}
private static List<String> decodeMnemonicCode(byte[] mnemonicCode) {
return decodeMnemonicCode(Utils.toString(mnemonicCode, "UTF-8"));
return decodeMnemonicCode(new String(mnemonicCode, Charsets.UTF_8));
}
private static List<String> decodeMnemonicCode(String mnemonicCode) {