0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.network.protocol;
0019
0020 import java.nio.charset.StandardCharsets;
0021
0022 import io.netty.buffer.ByteBuf;
0023
0024
0025 public class Encoders {
0026
0027
0028 public static class Strings {
0029 public static int encodedLength(String s) {
0030 return 4 + s.getBytes(StandardCharsets.UTF_8).length;
0031 }
0032
0033 public static void encode(ByteBuf buf, String s) {
0034 byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
0035 buf.writeInt(bytes.length);
0036 buf.writeBytes(bytes);
0037 }
0038
0039 public static String decode(ByteBuf buf) {
0040 int length = buf.readInt();
0041 byte[] bytes = new byte[length];
0042 buf.readBytes(bytes);
0043 return new String(bytes, StandardCharsets.UTF_8);
0044 }
0045 }
0046
0047
0048 public static class ByteArrays {
0049 public static int encodedLength(byte[] arr) {
0050 return 4 + arr.length;
0051 }
0052
0053 public static void encode(ByteBuf buf, byte[] arr) {
0054 buf.writeInt(arr.length);
0055 buf.writeBytes(arr);
0056 }
0057
0058 public static byte[] decode(ByteBuf buf) {
0059 int length = buf.readInt();
0060 byte[] bytes = new byte[length];
0061 buf.readBytes(bytes);
0062 return bytes;
0063 }
0064 }
0065
0066
0067 public static class StringArrays {
0068 public static int encodedLength(String[] strings) {
0069 int totalLength = 4;
0070 for (String s : strings) {
0071 totalLength += Strings.encodedLength(s);
0072 }
0073 return totalLength;
0074 }
0075
0076 public static void encode(ByteBuf buf, String[] strings) {
0077 buf.writeInt(strings.length);
0078 for (String s : strings) {
0079 Strings.encode(buf, s);
0080 }
0081 }
0082
0083 public static String[] decode(ByteBuf buf) {
0084 int numStrings = buf.readInt();
0085 String[] strings = new String[numStrings];
0086 for (int i = 0; i < strings.length; i ++) {
0087 strings[i] = Strings.decode(buf);
0088 }
0089 return strings;
0090 }
0091 }
0092
0093
0094 public static class IntArrays {
0095 public static int encodedLength(int[] ints) {
0096 return 4 + 4 * ints.length;
0097 }
0098
0099 public static void encode(ByteBuf buf, int[] ints) {
0100 buf.writeInt(ints.length);
0101 for (int i : ints) {
0102 buf.writeInt(i);
0103 }
0104 }
0105
0106 public static int[] decode(ByteBuf buf) {
0107 int numInts = buf.readInt();
0108 int[] ints = new int[numInts];
0109 for (int i = 0; i < ints.length; i ++) {
0110 ints[i] = buf.readInt();
0111 }
0112 return ints;
0113 }
0114 }
0115
0116
0117 public static class LongArrays {
0118 public static int encodedLength(long[] longs) {
0119 return 4 + 8 * longs.length;
0120 }
0121
0122 public static void encode(ByteBuf buf, long[] longs) {
0123 buf.writeInt(longs.length);
0124 for (long i : longs) {
0125 buf.writeLong(i);
0126 }
0127 }
0128
0129 public static long[] decode(ByteBuf buf) {
0130 int numLongs = buf.readInt();
0131 long[] longs = new long[numLongs];
0132 for (int i = 0; i < longs.length; i ++) {
0133 longs[i] = buf.readLong();
0134 }
0135 return longs;
0136 }
0137 }
0138 }