下面列出了com.google.protobuf.ByteString#byteAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Compares lexicographic order of {@code first} and {@code second}. */
public static int compare(ByteString first, ByteString second) {
Preconditions.checkNotNull(first);
Preconditions.checkNotNull(second);
// Note: size() is O(1) on ByteString.
for (int i = 0; i < first.size(); ++i) {
if (i == second.size()) {
// 'first' is longer than 'second' (logically, think of 'second' as padded with special
// 'blank' symbols that are smaller than any other symbol per the usual lexicographic
// ordering convention.)
return +1;
}
byte firstByte = first.byteAt(i);
byte secondByte = second.byteAt(i);
if (firstByte != secondByte) {
return (firstByte & 0xff) - (secondByte & 0xff);
}
}
// We ran through both strings and found no differences. If 'second' is longer than 'first',
// then we return -1. Otherwise, it implies that both strings have been consumed and no
// differences discovered in which case we return 0.
return (second.size() > first.size()) ? -1 : 0;
}
public static boolean lessThanTarget(byte[] found_hash, ByteString target)
{
// Quickly reject nonsense
for(int i=0; i<8; i++)
{
// If the target is not zero, done with loop
if (target.byteAt(i) != 0) break;
// If the target is zero, but found is not, fail
if (found_hash[i] != 0) return false;
}
ByteString found = ByteString.copyFrom(found_hash,0, Globals.TARGET_LENGTH);
return (ByteStringComparator.compareStatic(found, target) < 0);
}
public static String unsafeDecode(final ByteString in) {
final int len = in.size();
final char[] out = new char[len];
for (int i = 0; i < len; i++) {
out[i] = (char) (in.byteAt(i) & 0xFF);
}
return UnsafeUtil.moveToString(out);
}
public static boolean hasPrefix(ByteString str, ByteString prefix) {
for (int i = 0; i < prefix.size(); i++) {
if (str.byteAt(i) != prefix.byteAt(i)) {
return false;
}
}
return true;
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
public static String escapeBytes(final ByteString input) {
final StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
final byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07: builder.append("\\a" ); break;
case '\b': builder.append("\\b" ); break;
case '\f': builder.append("\\f" ); break;
case '\n': builder.append("\\n" ); break;
case '\r': builder.append("\\r" ); break;
case '\t': builder.append("\\t" ); break;
case 0x0b: builder.append("\\v" ); break;
case '\\': builder.append("\\\\"); break;
case '\'': builder.append("\\\'"); break;
case '"' : builder.append("\\\""); break;
default:
// Note: Bytes with the high-order bit set should be escaped. Since
// bytes are signed, such bytes will compare less than 0x20, hence
// the following line is correct.
if (b >= 0x20) {
builder.append((char) b);
} else {
builder.append('\\');
builder.append((char) ('0' + ((b >>> 6) & 3)));
builder.append((char) ('0' + ((b >>> 3) & 7)));
builder.append((char) ('0' + (b & 7)));
}
break;
}
}
return builder.toString();
}
public static String toHexString(ByteString bs) {
int len = bs.size();
if (len == 0) {
return "";
}
StringBuilder sb = new StringBuilder(len << 1);
for (int i = 0; i < len; i++) {
int b = bs.byteAt(i);
sb.append(HEX_CHARS[(b >> 4) & 0xf]).append(HEX_CHARS[b & 0xf]);
}
return sb.toString();
}
public static String toDebugString(ByteString bytes) {
int size = bytes.size();
StringBuilder result = new StringBuilder(2 * size);
for (int i = 0; i < size; i++) {
int value = bytes.byteAt(i) & 0xFF;
result.append(Character.forDigit(value >>> 4, 16));
result.append(Character.forDigit(value & 0xF, 16));
}
return result.toString();
}
public static int compareByteStrings(ByteString left, ByteString right) {
int size = Math.min(left.size(), right.size());
for (int i = 0; i < size; i++) {
// Make sure the bytes are unsigned
int thisByte = left.byteAt(i) & 0xff;
int otherByte = right.byteAt(i) & 0xff;
if (thisByte < otherByte) {
return -1;
} else if (thisByte > otherByte) {
return 1;
}
// Byte values are equal, continue with comparison
}
return Util.compareIntegers(left.size(), right.size());
}
public static boolean checkPermissionOprations(Permission permission, Contract contract)
throws PermissionException {
ByteString operations = permission.getOperations();
if (operations.size() != 32) {
throw new PermissionException("operations size must 32");
}
int contractType = contract.getTypeValue();
boolean b = (operations.byteAt(contractType / 8) & (1 << (contractType % 8))) != 0;
return b;
}
public static String getBase64FromByteString(ByteString sign) {
byte[] r = sign.substring(0, 32).toByteArray();
byte[] s = sign.substring(32, 64).toByteArray();
byte v = sign.byteAt(64);
if (v < 27) {
v += 27; //revId -> v
}
ECDSASignature signature = ECDSASignature.fromComponents(r, s, v);
return signature.toBase64();
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
public static String escapeBytes(final ByteString input) {
final StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
final byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07: builder.append("\\a" ); break;
case '\b': builder.append("\\b" ); break;
case '\f': builder.append("\\f" ); break;
case '\n': builder.append("\\n" ); break;
case '\r': builder.append("\\r" ); break;
case '\t': builder.append("\\t" ); break;
case 0x0b: builder.append("\\v" ); break;
case '\\': builder.append("\\\\"); break;
case '\'': builder.append("\\\'"); break;
case '"' : builder.append("\\\""); break;
default:
// Note: Bytes with the high-order bit set should be escaped. Since
// bytes are signed, such bytes will compare less than 0x20, hence
// the following line is correct.
if (b >= 0x20) {
builder.append((char) b);
} else {
builder.append('\\');
builder.append((char) ('0' + ((b >>> 6) & 3)));
builder.append((char) ('0' + ((b >>> 3) & 7)));
builder.append((char) ('0' + (b & 7)));
}
break;
}
}
return builder.toString();
}
/**
* Escapes bytes in the format used in protocol buffer text format, which
* is the same as the format used for C string literals. All bytes
* that are not printable 7-bit ASCII characters are escaped, as well as
* backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped
* using 3-digit octal sequences.
*/
static String escapeBytes(final ByteString input) {
final StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
final byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07: builder.append("\\a" ); break;
case '\b': builder.append("\\b" ); break;
case '\f': builder.append("\\f" ); break;
case '\n': builder.append("\\n" ); break;
case '\r': builder.append("\\r" ); break;
case '\t': builder.append("\\t" ); break;
case 0x0b: builder.append("\\v" ); break;
case '\\': builder.append("\\\\"); break;
case '\'': builder.append("\\\'"); break;
case '"' : builder.append("\\\""); break;
default:
if (b >= 0x20) {
builder.append((char) b);
} else {
builder.append('\\');
builder.append((char) ('0' + ((b >>> 6) & 3)));
builder.append((char) ('0' + ((b >>> 3) & 7)));
builder.append((char) ('0' + (b & 7)));
}
break;
}
}
return builder.toString();
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
public static String escapeBytes(final ByteString input) {
final StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
final byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07: builder.append("\\a" ); break;
case '\b': builder.append("\\b" ); break;
case '\f': builder.append("\\f" ); break;
case '\n': builder.append("\\n" ); break;
case '\r': builder.append("\\r" ); break;
case '\t': builder.append("\\t" ); break;
case 0x0b: builder.append("\\v" ); break;
case '\\': builder.append("\\\\"); break;
case '\'': builder.append("\\\'"); break;
case '"' : builder.append("\\\""); break;
default:
// Note: Bytes with the high-order bit set should be escaped. Since
// bytes are signed, such bytes will compare less than 0x20, hence
// the following line is correct.
if (b >= 0x20) {
builder.append((char) b);
} else {
builder.append('\\');
builder.append((char) ('0' + ((b >>> 6) & 3)));
builder.append((char) ('0' + ((b >>> 3) & 7)));
builder.append((char) ('0' + (b & 7)));
}
break;
}
}
return builder.toString();
}
public static boolean hasPrefix(ByteString str, ByteString prefix) {
for (int i = 0; i < prefix.size(); i++) {
if (str.byteAt(i) != prefix.byteAt(i)) {
return false;
}
}
return true;
}
public static List<Coprocessor.KeyRange> split(Coprocessor.KeyRange range, int splitFactor) {
if (splitFactor > 32 || splitFactor <= 0 || (splitFactor & (splitFactor - 1)) != 0) {
throw new TiClientInternalException(
"splitFactor must be positive integer power of 2 and no greater than 16");
}
ByteString startKey = range.getStart();
ByteString endKey = range.getEnd();
// we don't cut infinite
if (startKey.isEmpty() || endKey.isEmpty()) {
return ImmutableList.of(range);
}
ImmutableList.Builder<Coprocessor.KeyRange> resultList = ImmutableList.builder();
int maxSize = Math.max(startKey.size(), endKey.size());
int i;
for (i = 0; i < maxSize; i++) {
byte sb = i < startKey.size() ? startKey.byteAt(i) : 0;
byte eb = i < endKey.size() ? endKey.byteAt(i) : 0;
if (sb != eb) {
break;
}
}
ByteString sRemaining = i < startKey.size() ? startKey.substring(i) : ByteString.EMPTY;
ByteString eRemaining = i < endKey.size() ? endKey.substring(i) : ByteString.EMPTY;
CodecDataInput cdi = new CodecDataInput(sRemaining);
int uss = cdi.readPartialUnsignedShort();
cdi = new CodecDataInput(eRemaining);
int ues = cdi.readPartialUnsignedShort();
int delta = (ues - uss) / splitFactor;
if (delta <= 0) {
return ImmutableList.of(range);
}
ByteString prefix = startKey.size() > endKey.size() ?
startKey.substring(0, i) : endKey.substring(0, i);
ByteString newStartKey = startKey;
ByteString newEndKey;
for (int j = 0; j < splitFactor; j++) {
uss += delta;
if (j == splitFactor - 1) {
newEndKey = endKey;
} else {
CodecDataOutput cdo = new CodecDataOutput();
cdo.writeShort(uss);
newEndKey = prefix.concat(cdo.toByteString());
}
resultList.add(makeCoprocRange(newStartKey, newEndKey));
newStartKey = newEndKey;
}
return resultList.build();
}
private boolean checkPermission(Permission permission) throws ContractValidateException {
if (permission.getKeysCount() > dbManager.getDynamicPropertiesStore().getTotalSignNum()) {
throw new ContractValidateException("number of keys in permission should not be greater "
+ "than " + dbManager.getDynamicPropertiesStore().getTotalSignNum());
}
if (permission.getKeysCount() == 0) {
throw new ContractValidateException("key's count should be greater than 0");
}
if (permission.getType() == PermissionType.Witness && permission.getKeysCount() != 1) {
throw new ContractValidateException("Witness permission's key count should be 1");
}
if (permission.getThreshold() <= 0) {
throw new ContractValidateException("permission's threshold should be greater than 0");
}
String name = permission.getPermissionName();
if (!StringUtils.isEmpty(name) && name.length() > 32) {
throw new ContractValidateException("permission's name is too long");
}
//check owner name ?
if (permission.getParentId() != 0) {
throw new ContractValidateException("permission's parent should be owner");
}
long weightSum = 0;
List<ByteString> addressList = permission.getKeysList()
.stream()
.map(x -> x.getAddress())
.distinct()
.collect(toList());
if (addressList.size() != permission.getKeysList().size()) {
throw new ContractValidateException(
"address should be distinct in permission " + permission.getType());
}
for (Key key : permission.getKeysList()) {
if (!Wallet.addressValid(key.getAddress().toByteArray())) {
throw new ContractValidateException("key is not a validate address");
}
if (key.getWeight() <= 0) {
throw new ContractValidateException("key's weight should be greater than 0");
}
try {
weightSum = Math.addExact(weightSum, key.getWeight());
} catch (ArithmeticException e) {
throw new ContractValidateException(e.getMessage());
}
}
if (weightSum < permission.getThreshold()) {
throw new ContractValidateException(
"sum of all key's weight should not be less than threshold in permission " + permission
.getType());
}
ByteString operations = permission.getOperations();
if (permission.getType() != PermissionType.Active) {
if (!operations.isEmpty()) {
throw new ContractValidateException(
permission.getType() + " permission needn't operations");
}
return true;
}
//check operations
if (operations.isEmpty() || operations.size() != 32) {
throw new ContractValidateException("operations size must 32");
}
byte[] types1 = dbManager.getDynamicPropertiesStore().getAvailableContractType();
for (int i = 0; i < 256; i++) {
boolean b = (operations.byteAt(i / 8) & (1 << (i % 8))) != 0;
boolean t = ((types1[(i / 8)] & 0xff) & (1 << (i % 8))) != 0;
if (b && !t) {
throw new ContractValidateException(i + " isn't a validate ContractType");
}
}
return true;
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
static String escapeBytes(ByteString input) {
StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07:
builder.append("\\a");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case 0x0b:
builder.append("\\v");
break;
case '\\':
builder.append("\\\\");
break;
case '\'':
builder.append("\\\'");
break;
case '"':
builder.append("\\\"");
break;
default:
if (b >= 0x20) {
builder.append((char) b);
} else {
final String unicodeString = unicodeEscaped((char) b);
builder.append(unicodeString);
}
break;
}
}
return builder.toString();
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
static String escapeBytes(ByteString input) {
StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07:
builder.append("\\a");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case 0x0b:
builder.append("\\v");
break;
case '\\':
builder.append("\\\\");
break;
case '\'':
builder.append("\\\'");
break;
case '"':
builder.append("\\\"");
break;
default:
if (b >= 0x20) {
builder.append((char) b);
} else {
builder.append('\\');
builder.append((char) ('0' + ((b >>> 6) & 3)));
builder.append((char) ('0' + ((b >>> 3) & 7)));
builder.append((char) ('0' + (b & 7)));
}
break;
}
}
return builder.toString();
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
static String escapeBytes(ByteString input) {
StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07:
builder.append("\\a");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case 0x0b:
builder.append("\\v");
break;
case '\\':
builder.append("\\\\");
break;
case '\'':
builder.append("\\\'");
break;
case '"':
builder.append("\\\"");
break;
default:
if (b >= 0x20) {
builder.append((char) b);
} else {
builder.append('\\');
builder.append((char) ('0' + ((b >>> 6) & 3)));
builder.append((char) ('0' + ((b >>> 3) & 7)));
builder.append((char) ('0' + (b & 7)));
}
break;
}
}
return builder.toString();
}
/**
* Escapes bytes in the format used in protocol buffer text format, which is the same as the
* format used for C string literals. All bytes that are not printable 7-bit ASCII characters
* are escaped, as well as backslash, single-quote, and double-quote characters. Characters for
* which no defined short-hand escape sequence is defined will be escaped using 3-digit octal
* sequences.
*/
static String escapeBytes(ByteString input) {
StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
case 0x07:
builder.append("\\a");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case 0x0b:
builder.append("\\v");
break;
case '\\':
builder.append("\\\\");
break;
case '\'':
builder.append("\\\'");
break;
case '"':
builder.append("\\\"");
break;
default:
if (b >= 0x20) {
builder.append((char) b);
} else {
final String unicodeString = unicodeEscaped((char) b);
builder.append(unicodeString);
}
break;
}
}
return builder.toString();
}