@Override
 public Object read(ByteBuffer buff, int tag) {
   if (tag != TYPE_ARRAY) {
     byte[] data;
     int len = tag - TAG_BYTE_ARRAY_0_15;
     data = DataUtils.newBytes(len);
     buff.get(data);
     return data;
   }
   int ct = buff.get();
   Class<?> clazz;
   Object obj;
   if (ct == -1) {
     String componentType = StringDataType.INSTANCE.read(buff);
     try {
       clazz = Class.forName(componentType);
     } catch (Exception e) {
       throw DataUtils.newIllegalStateException("Could not get class {0}", componentType, e);
     }
   } else {
     clazz = COMMON_CLASSES[ct];
   }
   int len = DataUtils.readVarInt(buff);
   try {
     obj = Array.newInstance(clazz, len);
   } catch (Exception e) {
     throw DataUtils.newIllegalStateException(
         "Could not create array of type {0} length {1}", clazz, len, e);
   }
   if (clazz.isPrimitive()) {
     for (int i = 0; i < len; i++) {
       if (clazz == boolean.class) {
         ((boolean[]) obj)[i] = buff.get() == 1;
       } else if (clazz == byte.class) {
         ((byte[]) obj)[i] = buff.get();
       } else if (clazz == char.class) {
         ((char[]) obj)[i] = buff.getChar();
       } else if (clazz == short.class) {
         ((short[]) obj)[i] = buff.getShort();
       } else if (clazz == int.class) {
         ((int[]) obj)[i] = buff.getInt();
       } else if (clazz == float.class) {
         ((float[]) obj)[i] = buff.getFloat();
       } else if (clazz == double.class) {
         ((double[]) obj)[i] = buff.getDouble();
       } else {
         ((long[]) obj)[i] = buff.getLong();
       }
     }
   } else {
     Object[] array = (Object[]) obj;
     for (int i = 0; i < len; i++) {
       array[i] = elementType.read(buff);
     }
   }
   return obj;
 }
Пример #2
0
 @Override
 public void write(WriteBuffer buff, Object obj) {
   if (!isArray(obj)) {
     super.write(buff, obj);
     return;
   }
   Class<?> type = obj.getClass().getComponentType();
   Integer classId = getCommonClassId(type);
   if (classId != null) {
     if (type.isPrimitive()) {
       if (type == byte.class) {
         byte[] data = (byte[]) obj;
         int len = data.length;
         if (len <= 15) {
           buff.put((byte) (TAG_BYTE_ARRAY_0_15 + len));
         } else {
           buff.put((byte) TYPE_ARRAY).put((byte) classId.intValue()).putVarInt(len);
         }
         buff.put(data);
         return;
       }
       int len = Array.getLength(obj);
       buff.put((byte) TYPE_ARRAY).put((byte) classId.intValue()).putVarInt(len);
       for (int i = 0; i < len; i++) {
         if (type == boolean.class) {
           buff.put((byte) (((boolean[]) obj)[i] ? 1 : 0));
         } else if (type == char.class) {
           buff.putChar(((char[]) obj)[i]);
         } else if (type == short.class) {
           buff.putShort(((short[]) obj)[i]);
         } else if (type == int.class) {
           buff.putInt(((int[]) obj)[i]);
         } else if (type == float.class) {
           buff.putFloat(((float[]) obj)[i]);
         } else if (type == double.class) {
           buff.putDouble(((double[]) obj)[i]);
         } else {
           buff.putLong(((long[]) obj)[i]);
         }
       }
       return;
     }
     buff.put((byte) TYPE_ARRAY).put((byte) classId.intValue());
   } else {
     buff.put((byte) TYPE_ARRAY).put((byte) -1);
     String c = type.getName();
     StringDataType.INSTANCE.write(buff, c);
   }
   Object[] array = (Object[]) obj;
   int len = array.length;
   buff.putVarInt(len);
   for (Object x : array) {
     elementType.write(buff, x);
   }
 }