首页

关于gora-core源码包中ByteUtils字节工具类对字符串、集合及基本数据类型间转换

标签:ByteUtils,字节工具类,apache,gora     发布时间:2018-05-15   

一、前言

关于apachegora-core源码包org.apache.gora.util.ByteUtils字节工具类,进行字节ByteBuffer转为字节数组toBytes、字节数组与字符串相互转换(toString、toBytes)、字符串数组和基本数据类型间的转换处理等

二、源码说明

package org.apache.gora.util;@b@@b@import java.io.IOException;@b@import java.io.UnsupportedEncodingException;@b@import java.nio.ByteBuffer;@b@import java.util.List;@b@import org.apache.avro.Schema;@b@import org.apache.avro.Schema.Type;@b@import org.apache.avro.reflect.ReflectData;@b@import org.apache.avro.specific.SpecificDatumReader;@b@import org.apache.avro.specific.SpecificDatumWriter;@b@import org.apache.avro.specific.SpecificRecord;@b@import org.apache.avro.util.Utf8;@b@import org.apache.hadoop.io.WritableUtils;@b@@b@public class ByteUtils@b@{@b@  public static final int SIZEOF_BOOLEAN = 1;@b@  public static final int SIZEOF_BYTE = 1;@b@  public static final int SIZEOF_CHAR = 2;@b@  public static final int SIZEOF_DOUBLE = 8;@b@  public static final int SIZEOF_FLOAT = 4;@b@  public static final int SIZEOF_INT = 4;@b@  public static final int SIZEOF_LONG = 8;@b@  public static final int SIZEOF_SHORT = 2;@b@@b@  public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes, int srcOffset, int srcLength)@b@  {@b@    System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength);@b@    return (tgtOffset + srcLength);@b@  }@b@@b@  public static int putByte(byte[] bytes, int offset, byte b)@b@  {@b@    bytes[offset] = b;@b@    return (offset + 1);@b@  }@b@@b@  public static byte[] toBytes(ByteBuffer bb)@b@  {@b@    int length = bb.limit();@b@    byte[] result = new byte[length];@b@    System.arraycopy(bb.array(), bb.arrayOffset(), result, 0, length);@b@    return result;@b@  }@b@@b@  public static String toString(byte[] b)@b@  {@b@    if (b == null)@b@      return null;@b@@b@    return toString(b, 0, b.length);@b@  }@b@@b@  public static String toString(byte[] b1, String sep, byte[] b2)@b@  {@b@    return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length);@b@  }@b@@b@  public static String toString(byte[] b, int off, int len)@b@  {@b@    if (b == null)@b@      return null;@b@@b@    if (len == 0)@b@      return "";@b@@b@    String result = null;@b@    try {@b@      result = new String(b, off, len, "UTF-8");@b@    } catch (UnsupportedEncodingException e) {@b@      e.printStackTrace();@b@    }@b@    return result;@b@  }@b@@b@  public static byte[] toBytes(String s)@b@  {@b@    if (s == null)@b@      throw new IllegalArgumentException("string cannot be null");@b@@b@    byte[] result = null;@b@    try {@b@      result = s.getBytes("UTF-8");@b@    } catch (UnsupportedEncodingException e) {@b@      e.printStackTrace();@b@    }@b@    return result;@b@  }@b@@b@  public static byte[] toBytes(boolean b)@b@  {@b@    byte[] bb = new byte[1];@b@    bb[0] = ((b) ? -1 : 0);@b@    return bb;@b@  }@b@@b@  public static boolean toBoolean(byte[] b)@b@  {@b@    if ((b == null) || (b.length > 1))@b@      throw new IllegalArgumentException("Array is wrong size");@b@@b@    return (b[0] != 0);@b@  }@b@@b@  public static byte[] toBytes(long val)@b@  {@b@    byte[] b = new byte[8];@b@    for (int i = 7; i > 0; --i) {@b@      b[i] = (byte)(int)val;@b@      val >>>= 8;@b@    }@b@    b[0] = (byte)(int)val;@b@    return b;@b@  }@b@@b@  public static long toLong(byte[] bytes)@b@  {@b@    return toLong(bytes, 0);@b@  }@b@@b@  public static long toLong(byte[] bytes, int offset)@b@  {@b@    return toLong(bytes, offset, 8);@b@  }@b@@b@  public static long toLong(byte[] bytes, int offset, int length)@b@  {@b@    if ((bytes == null) || (length != 8) || (offset + length > bytes.length))@b@    {@b@      return -1L;@b@    }@b@    long l = 0L;@b@    for (int i = offset; i < offset + length; ++i) {@b@      l <<= 8;@b@      l ^= bytes[i] & 0xFF;@b@    }@b@    return l;@b@  }@b@@b@  public static float toFloat(byte[] bytes)@b@  {@b@    return toFloat(bytes, 0);@b@  }@b@@b@  public static float toFloat(byte[] bytes, int offset)@b@  {@b@    int i = toInt(bytes, offset);@b@    return Float.intBitsToFloat(i);@b@  }@b@@b@  public static byte[] toBytes(float f)@b@  {@b@    int i = Float.floatToRawIntBits(f);@b@    return toBytes(i);@b@  }@b@@b@  public static double toDouble(byte[] bytes)@b@  {@b@    return toDouble(bytes, 0);@b@  }@b@@b@  public static double toDouble(byte[] bytes, int offset)@b@  {@b@    long l = toLong(bytes, offset);@b@    return Double.longBitsToDouble(l);@b@  }@b@@b@  public static byte[] toBytes(double d)@b@  {@b@    long l = Double.doubleToRawLongBits(d);@b@    return toBytes(l);@b@  }@b@@b@  public static byte[] toBytes(int val)@b@  {@b@    byte[] b = new byte[4];@b@    for (int i = 3; i > 0; --i) {@b@      b[i] = (byte)val;@b@      val >>>= 8;@b@    }@b@    b[0] = (byte)val;@b@    return b;@b@  }@b@@b@  public static int toInt(byte[] bytes)@b@  {@b@    return toInt(bytes, 0);@b@  }@b@@b@  public static int toInt(byte[] bytes, int offset)@b@  {@b@    return toInt(bytes, offset, 4);@b@  }@b@@b@  public static int toInt(byte[] bytes, int offset, int length)@b@  {@b@    if ((bytes == null) || (length != 4) || (offset + length > bytes.length))@b@    {@b@      return -1;@b@    }@b@    int n = 0;@b@    for (int i = offset; i < offset + length; ++i) {@b@      n <<= 8;@b@      n ^= bytes[i] & 0xFF;@b@    }@b@    return n;@b@  }@b@@b@  public static byte[] toBytes(short val)@b@  {@b@    byte[] b = new byte[2];@b@    b[1] = (byte)val;@b@    val = (short)(val >> 8);@b@    b[0] = (byte)val;@b@    return b;@b@  }@b@@b@  public static short toShort(byte[] bytes)@b@  {@b@    return toShort(bytes, 0);@b@  }@b@@b@  public static short toShort(byte[] bytes, int offset)@b@  {@b@    return toShort(bytes, offset, 2);@b@  }@b@@b@  public static short toShort(byte[] bytes, int offset, int length)@b@  {@b@    if ((bytes == null) || (length != 2) || (offset + length > bytes.length))@b@    {@b@      return -1;@b@    }@b@    short n = 0;@b@    n = (short)(n ^ bytes[offset] & 0xFF);@b@    n = (short)(n << 8);@b@    n = (short)(n ^ bytes[(offset + 1)] & 0xFF);@b@    return n;@b@  }@b@@b@  public static byte[] toBytes(char val)@b@  {@b@    byte[] b = new byte[2];@b@    b[1] = (byte)val;@b@    val = (char)(val >> '\b');@b@    b[0] = (byte)val;@b@    return b;@b@  }@b@@b@  public static char toChar(byte[] bytes)@b@  {@b@    return toChar(bytes, 0);@b@  }@b@@b@  public static char toChar(byte[] bytes, int offset)@b@  {@b@    return toChar(bytes, offset, 2);@b@  }@b@@b@  public static char toChar(byte[] bytes, int offset, int length)@b@  {@b@    if ((bytes == null) || (length != 2) || (offset + length > bytes.length))@b@    {@b@      return 65535;@b@    }@b@    char n = ';@b@    n = (char)(n ^ bytes[offset] & 0xFF);@b@    n = (char)(n << '\b');@b@    n = (char)(n ^ bytes[(offset + 1)] & 0xFF);@b@    return n;@b@  }@b@@b@  public static char[] toChars(byte[] bytes)@b@  {@b@    return toChars(bytes, 0, bytes.length);@b@  }@b@@b@  public static char[] toChars(byte[] bytes, int offset)@b@  {@b@    return toChars(bytes, offset, bytes.length - offset);@b@  }@b@@b@  public static char[] toChars(byte[] bytes, int offset, int length)@b@  {@b@    int max = offset + length;@b@    if ((bytes == null) || (max > bytes.length) || (length % 2 == 1)) {@b@      return null;@b@    }@b@@b@    char[] chars = new char[length / 2];@b@    int i = 0; for (int j = offset; (i < chars.length) && (j < max); ) {@b@      char c = ';@b@      c = (char)(c ^ bytes[j] & 0xFF);@b@      c = (char)(c << '\b');@b@      c = (char)(c ^ bytes[(j + 1)] & 0xFF);@b@      chars[i] = c;@b@@b@      ++i; j += 2;@b@    }@b@@b@    return chars;@b@  }@b@@b@  public static byte[] vintToBytes(long vint)@b@  {@b@    long i = vint;@b@    int size = WritableUtils.getVIntSize(i);@b@    byte[] result = new byte[size];@b@    int offset = 0;@b@    if ((i >= -112L) && (i <= 127L)) {@b@      result[offset] = (byte)(int)i;@b@      return result;@b@    }@b@@b@    int len = -112;@b@    if (i < 0L) {@b@      i ^= -1L;@b@      len = -120;@b@    }@b@@b@    long tmp = i;@b@    while (tmp != 0L) {@b@      tmp >>= 8;@b@      --len;@b@    }@b@@b@    result[(offset++)] = (byte)len;@b@@b@    len = (len < -120) ? -(len + 120) : -(len + 112);@b@@b@    for (int idx = len; idx != 0; --idx) {@b@      int shiftbits = (idx - 1) * 8;@b@      long mask = 255L << shiftbits;@b@      result[(offset++)] = (byte)(int)((i & mask) >> shiftbits);@b@    }@b@    return result;@b@  }@b@@b@  public static long bytesToVlong(byte[] buffer)@b@  {@b@    int offset = 0;@b@    byte firstByte = buffer[(offset++)];@b@    int len = WritableUtils.decodeVIntSize(firstByte);@b@    if (len == 1)@b@      return firstByte;@b@@b@    long i = 0L;@b@    for (int idx = 0; idx < len - 1; ++idx) {@b@      byte b = buffer[(offset++)];@b@      i <<= 8;@b@      i |= b & 0xFF;@b@    }@b@    return ((WritableUtils.isNegativeVInt(firstByte)) ? i ^ 0xFFFFFFFF : i);@b@  }@b@@b@  public static int bytesToVint(byte[] buffer)@b@  {@b@    int offset = 0;@b@    byte firstByte = buffer[(offset++)];@b@    int len = WritableUtils.decodeVIntSize(firstByte);@b@    if (len == 1)@b@      return firstByte;@b@@b@    long i = 0L;@b@    for (int idx = 0; idx < len - 1; ++idx) {@b@      byte b = buffer[(offset++)];@b@      i <<= 8;@b@      i |= b & 0xFF;@b@    }@b@    return (int)((WritableUtils.isNegativeVInt(firstByte)) ? i ^ 0xFFFFFFFF : i);@b@  }@b@@b@  public static long readVLong(byte[] buffer, int offset)@b@    throws IOException@b@  {@b@    byte firstByte = buffer[offset];@b@    int len = WritableUtils.decodeVIntSize(firstByte);@b@    if (len == 1)@b@      return firstByte;@b@@b@    long i = 0L;@b@    for (int idx = 0; idx < len - 1; ++idx) {@b@      byte b = buffer[(offset + 1 + idx)];@b@      i <<= 8;@b@      i |= b & 0xFF;@b@    }@b@    return ((WritableUtils.isNegativeVInt(firstByte)) ? i ^ 0xFFFFFFFF : i);@b@  }@b@@b@  public static int compareTo(byte[] left, byte[] right)@b@  {@b@    return compareTo(left, 0, left.length, right, 0, right.length);@b@  }@b@@b@  public static int compareTo(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)@b@  {@b@    int end1 = s1 + l1;@b@    int end2 = s2 + l2;@b@    int i = s1; for (int j = s2; (i < end1) && (j < end2); ) {@b@      int a = b1[i] & 0xFF;@b@      int b = b2[j] & 0xFF;@b@      if (a != b)@b@        return (a - b);@b@      ++i; ++j;@b@    }@b@@b@    return (l1 - l2);@b@  }@b@@b@  public static boolean equals(byte[] left, byte[] right)@b@  {@b@    return ((left == null) && (right == null));@b@  }@b@@b@  public static <T> T fromBytes(byte[] val, Schema schema, SpecificDatumReader<T> datumReader, T object)@b@    throws IOException@b@  {@b@    Schema.Type type = schema.getType();@b@    switch (1.$SwitchMap$org$apache$avro$Schema$Type[type.ordinal()])@b@    {@b@    case 1:@b@      String symbol = (String)schema.getEnumSymbols().get(val[0]);@b@      return Enum.valueOf(ReflectData.get().getClass(schema), symbol);@b@    case 2:@b@      return new Utf8(toString(val));@b@    case 3:@b@      return ByteBuffer.wrap(val);@b@    case 4:@b@      return Integer.valueOf(bytesToVint(val));@b@    case 5:@b@      return Long.valueOf(bytesToVlong(val));@b@    case 6:@b@      return Float.valueOf(toFloat(val));@b@    case 7:@b@      return Double.valueOf(toDouble(val));@b@    case 8:@b@      return Boolean.valueOf(val[0] != 0);@b@    case 9:@b@    case 10:@b@    case 11:@b@      return IOUtils.deserialize(val, datumReader, schema, (SpecificRecord)object); }@b@    throw new RuntimeException("Unknown type: " + type);@b@  }@b@@b@  @b@}