mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-13 02:35:52 +00:00
Utils: Remove toString() and toBytes() helpers, use new String() and getBytes() with Guava charsets instead.
This commit is contained in:
parent
b6ecc380b8
commit
b50eddb0a2
@ -20,6 +20,8 @@ package org.bitcoinj.core;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.BufferUnderflowException;
|
import java.nio.BufferUnderflowException;
|
||||||
@ -364,7 +366,7 @@ public class BitcoinSerializer extends MessageSerializer {
|
|||||||
for (; header[cursor] != 0 && cursor < COMMAND_LEN; cursor++) ;
|
for (; header[cursor] != 0 && cursor < COMMAND_LEN; cursor++) ;
|
||||||
byte[] commandBytes = new byte[cursor];
|
byte[] commandBytes = new byte[cursor];
|
||||||
System.arraycopy(header, 0, commandBytes, 0, cursor);
|
System.arraycopy(header, 0, commandBytes, 0, cursor);
|
||||||
command = Utils.toString(commandBytes, "US-ASCII");
|
command = new String(commandBytes, Charsets.US_ASCII);
|
||||||
cursor = COMMAND_LEN;
|
cursor = COMMAND_LEN;
|
||||||
|
|
||||||
size = (int) readUint32(header, cursor);
|
size = (int) readUint32(header, cursor);
|
||||||
|
@ -20,6 +20,8 @@ package org.bitcoinj.core;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -347,7 +349,7 @@ public abstract class Message {
|
|||||||
|
|
||||||
protected String readStr() throws ProtocolException {
|
protected String readStr() throws ProtocolException {
|
||||||
long length = readVarInt();
|
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 {
|
protected Sha256Hash readHash() throws ProtocolException {
|
||||||
|
@ -30,7 +30,6 @@ import org.spongycastle.crypto.digests.RIPEMD160Digest;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
@ -439,44 +438,6 @@ public class Utils {
|
|||||||
return result;
|
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
|
* Attempts to parse the given string as arbitrary-length hex or base58 and then return the results, or null if
|
||||||
* neither parse was successful.
|
* neither parse was successful.
|
||||||
|
@ -236,7 +236,7 @@ public class DeterministicSeed implements EncryptableItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> decodeMnemonicCode(byte[] mnemonicCode) {
|
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) {
|
private static List<String> decodeMnemonicCode(String mnemonicCode) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user