Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
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 /** Provides a canonical set of Encoders for simple types. */
0025 public class Encoders {
0026 
0027   /** Strings are encoded with their length followed by UTF-8 bytes. */
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   /** Byte arrays are encoded with their length followed by bytes. */
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   /** String arrays are encoded with the number of strings followed by per-String encoding. */
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   /** Integer arrays are encoded with their length followed by integers. */
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   /** Long integer arrays are encoded with their length followed by long integers. */
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 }