Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright 2019 Evan Saulpaugh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.brewchain.sdk.contract.rlp;
import org.brewchain.sdk.contract.rlp.util.Integers;
import org.brewchain.sdk.contract.rlp.eip778.KeyValuePair;
import java.util.Arrays;
/**
* Encodes data to RLP format.
*/
public final class RLPEncoder {
public static long encodedLen(long val) {
int dataLen = Integers.len(val);
if(dataLen == 1) {
return (byte) val >= 0x00 ? 1 : 2;
}
return 1 + dataLen;
}
public static long dataLen(KeyValuePair[] pairs) {
long total = 0;
for (KeyValuePair kvp : pairs) {
total += itemEncodedLen(kvp.getKey()) + itemEncodedLen(kvp.getValue());
}
return total;
}
public static int prefixLength(long dataLen) {
if (isLong(dataLen)) {
return 1 + Integers.len(dataLen);
} else {
return 1;
}
}
public static int insertListPrefix(long dataLen, byte[] dest, int destIndex) {
return isLong(dataLen)
? encodeLongListPrefix(dataLen, dest, destIndex)
: encodeShortListPrefix(dataLen, dest, destIndex);
}
// -----------------------------------------------------------------------------------------------------------------
private static boolean isLong(long dataLen) {
return dataLen >= DataType.MIN_LONG_DATA_LEN;
}
private static long totalEncodedLen(Iterable> objects) {
long total = 0;
for (Object obj : objects) {
total += itemEncodedLen(obj);
}
return total;
}
private static long itemEncodedLen(Object obj) {
if (obj instanceof byte[]) {
return stringEncodedLen((byte[]) obj);
}
if (obj instanceof Iterable>) {
return listEncodedLen((Iterable>) obj);
}
if(obj instanceof Object[]) {
return listEncodedLen(Arrays.asList((Object[]) obj));
}
if(obj == null) {
throw new NullPointerException();
}
throw new IllegalArgumentException("unsupported object type: " + obj.getClass().getName());
}
private static int stringEncodedLen(byte[] byteString) {
final int dataLen = byteString.length;
if (isLong(dataLen)) {
return 1 + Integers.len(dataLen) + dataLen;
}
if (dataLen == 1 && byteString[0] >= 0x00) { // same as (bytes[0] & 0xFF) < 0x80
return 1;
}
return 1 + dataLen;
}
private static long listEncodedLen(Iterable> elements) {
final long listDataLen = totalEncodedLen(elements);
if (isLong(listDataLen)) {
return 1 + Integers.len(listDataLen) + listDataLen;
}
return 1 + listDataLen;
}
private static int encodeKeyValuePair(KeyValuePair pair, byte[] dest, int destIndex) {
destIndex = encodeString(pair.getKey(), dest, destIndex);
return encodeString(pair.getValue(), dest, destIndex);
}
private static int encodeItem(Object item, byte[] dest, int destIndex) {
if (item instanceof byte[]) {
return encodeString((byte[]) item, dest, destIndex);
}
if (item instanceof Iterable>) {
Iterable> elements = (Iterable>) item;
return encodeList(totalEncodedLen(elements), elements, dest, destIndex);
}
if(item instanceof Object[]) {
Iterable