|
| 1 | +package org.platon.common; |
| 2 | + |
| 3 | +import com.google.protobuf.ByteString; |
| 4 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 5 | +import org.platon.common.proto.BaseProto; |
| 6 | +import org.platon.common.utils.ByteArrayWrapper; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +/** |
| 12 | + * base encode and decode by protoBuf |
| 13 | + * @author yanze |
| 14 | + */ |
| 15 | +public class BasicPbCodec { |
| 16 | + |
| 17 | + public static byte[] encodeBytesList(byte[]... elements){ |
| 18 | + if(elements == null){ |
| 19 | + return null; |
| 20 | + } |
| 21 | + BaseProto.BaseBytesList.Builder builder = BaseProto.BaseBytesList.newBuilder(); |
| 22 | + for(byte[] element : elements){ |
| 23 | + builder.addBytesListData(ByteString.copyFrom(element)); |
| 24 | + } |
| 25 | + return builder.build().toByteArray(); |
| 26 | + } |
| 27 | + |
| 28 | + public static byte[] encodeBytesList(List<ByteArrayWrapper> bytesList){ |
| 29 | + if(bytesList == null){ |
| 30 | + return null; |
| 31 | + } |
| 32 | + BaseProto.BaseBytesList.Builder builder = BaseProto.BaseBytesList.newBuilder(); |
| 33 | + for(int i = 0;i<bytesList.size();i++){ |
| 34 | + builder.addBytesListData(ByteString.copyFrom(bytesList.get(i).getData())); |
| 35 | + } |
| 36 | + return builder.build().toByteArray(); |
| 37 | + } |
| 38 | + |
| 39 | + public static List<ByteArrayWrapper> decodeBytesList(byte[] protoBuf) throws InvalidProtocolBufferException { |
| 40 | + BaseProto.BaseBytesList baseBytesList = BaseProto.BaseBytesList.parseFrom(protoBuf); |
| 41 | + List<ByteArrayWrapper> list = new ArrayList<>(); |
| 42 | + for(int i = 0;i<baseBytesList.getBytesListDataCount();i++){ |
| 43 | + list.add(new ByteArrayWrapper(baseBytesList.getBytesListData(i).toByteArray())); |
| 44 | + } |
| 45 | + return list; |
| 46 | + } |
| 47 | + |
| 48 | + public static byte[] encodeInt(int data){ |
| 49 | + BaseProto.BaseInt.Builder builder = BaseProto.BaseInt.newBuilder(); |
| 50 | + builder.setIntData(data); |
| 51 | + return builder.build().toByteArray(); |
| 52 | + } |
| 53 | + |
| 54 | + public static int decodeInt(byte[] protoBuf) throws InvalidProtocolBufferException{ |
| 55 | + BaseProto.BaseInt baseInt = BaseProto.BaseInt.parseFrom(protoBuf); |
| 56 | + return baseInt.getIntData(); |
| 57 | + } |
| 58 | + |
| 59 | + public static byte[] encodeLong(long data){ |
| 60 | + BaseProto.BaseLong.Builder builder = BaseProto.BaseLong.newBuilder(); |
| 61 | + builder.setLongData(data); |
| 62 | + return builder.build().toByteArray(); |
| 63 | + } |
| 64 | + |
| 65 | + public static long decodeLong(byte[] protoBuf) throws InvalidProtocolBufferException{ |
| 66 | + BaseProto.BaseLong baseLong = BaseProto.BaseLong.parseFrom(protoBuf); |
| 67 | + return baseLong.getLongData(); |
| 68 | + } |
| 69 | + |
| 70 | + public static byte[] encodeString(String data){ |
| 71 | + if(data == null){ |
| 72 | + return null; |
| 73 | + } |
| 74 | + BaseProto.BaseString.Builder builder = BaseProto.BaseString.newBuilder(); |
| 75 | + builder.setStringData(data); |
| 76 | + return builder.build().toByteArray(); |
| 77 | + } |
| 78 | + |
| 79 | + public static String decodeString(byte[] protoBuf) throws InvalidProtocolBufferException{ |
| 80 | + BaseProto.BaseString baseString = BaseProto.BaseString.parseFrom(protoBuf); |
| 81 | + return baseString.getStringData(); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments