Java Code Examples for org.fisco.bcos.web3j.utils.ByteUtil#merge()

The following examples show how to use org.fisco.bcos.web3j.utils.ByteUtil#merge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolidityType.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] encodeList(List l) {
    byte[][] elems;
    if (elementType.isDynamicType()) {
        elems = new byte[l.size() * 2 + 1][];
        elems[0] = IntType.encodeInt(l.size());
        int offset = l.size() * 32;
        for (int i = 0; i < l.size(); i++) {
            elems[i + 1] = IntType.encodeInt(offset);
            byte[] encoded = elementType.encode(l.get(i));
            elems[l.size() + i + 1] = encoded;
            offset += 32 * ((encoded.length - 1) / 32 + 1);
        }
    } else {
        elems = new byte[l.size() + 1][];
        elems[0] = IntType.encodeInt(l.size());

        for (int i = 0; i < l.size(); i++) {
            elems[i + 1] = elementType.encode(l.get(i));
        }
    }
    return ByteUtil.merge(elems);
}
 
Example 2
Source File: SolidityType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encodeList(List l) {
    if (l.size() != size)
        throw new RuntimeException(
                "List size (" + l.size() + ") != " + size + " for type " + getName());
    byte[][] elems = new byte[size][];
    for (int i = 0; i < l.size(); i++) {
        elems[i] = elementType.encode(l.get(i));
    }
    return ByteUtil.merge(elems);
}
 
Example 3
Source File: SolidityType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode(Object value) {
    byte[] bb;
    if (value instanceof byte[]) {
        bb = (byte[]) value;
    } else if (value instanceof String) {
        bb = ((String) value).getBytes();
    } else {
        throw new RuntimeException("byte[] or String value is expected for type 'bytes'");
    }
    byte[] ret = new byte[((bb.length - 1) / 32 + 1) * 32]; // padding 32 bytes
    System.arraycopy(bb, 0, ret, 0, bb.length);

    return ByteUtil.merge(IntType.encodeInt(bb.length), ret);
}
 
Example 4
Source File: SolidityType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode(Object value) {
    if (!(value instanceof byte[]))
        throw new RuntimeException("Expected byte[] value for FunctionType");
    if (((byte[]) value).length != 24)
        throw new RuntimeException("Expected byte[24] for FunctionType");
    return super.encode(ByteUtil.merge((byte[]) value, new byte[8]));
}
 
Example 5
Source File: Abi.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public byte[] encode(Object... args) {
    return ByteUtil.merge(encodeSignature(), encodeArguments(args));
}