Exemplo n.º 1
0
 /** Write a table using it's ByteBuffer serialization code. */
 public void writeTable(VoltTable table) throws IOException {
   int len = table.getSerializedSize();
   growIfNeeded(len);
   table.flattenToBuffer(buffer.b());
 }
Exemplo n.º 2
0
  static void echo(byte[] t, byte[] buffer, int length) {
    VoltType type = null;
    ByteBuffer buf;

    if (t[0] == ARRAY_BEGIN) {
      isArray = true;
      return;
    } else if (t[0] == ARRAY_END) {
      isArray = false;
      return;
    } else {
      type = VoltType.get(t[0]);
    }

    FastDeserializer fds = new FastDeserializer(buffer);
    int count = 1;
    try {
      fs.writeInt(length);
      fs.writeByte(type.getValue());

      if (isArray) {
        if (type == VoltType.TINYINT) {
          count = fds.readInt();
          fs.writeInt(count);
        } else {
          count = fds.readShort();
          fs.writeShort(count);
        }
      }

      for (int ii = 0; ii < count; ii++) {
        switch (type) {
          case TINYINT:
            byte b = fds.readByte();
            fs.write(b);
            break;
          case SMALLINT:
            short s = fds.readShort();
            fs.writeShort(s);
            break;
          case INTEGER:
            int i = fds.readInt();
            fs.writeInt(i);
            break;
          case BIGINT:
            long l = fds.readLong();
            fs.writeLong(l);
            break;
          case FLOAT:
            double f = fds.readDouble();
            fs.writeDouble(f);
            break;
          case STRING:
            String str = fds.readString();
            fs.writeString(str);
            break;
          case VARBINARY:
            byte[] bin = fds.readVarbinary();
            fs.writeVarbinary(bin);
            break;
          case TIMESTAMP:
            long micros = fds.readLong();
            fs.writeLong(micros);
            break;
          case DECIMAL:
            BigDecimal bd = VoltDecimalHelper.deserializeBigDecimal(fds.buffer());
            buf = ByteBuffer.allocate(16);
            VoltDecimalHelper.serializeBigDecimal(bd, buf);
            buf.flip();
            fs.write(buf);
            break;
          case VOLTTABLE:
            VoltTable table = PrivateVoltTableFactory.createVoltTableFromSharedBuffer(fds.buffer());
            buf = ByteBuffer.allocate(table.getSerializedSize());
            table.flattenToBuffer(buf);
            buf.flip();
            fs.write(buf);
            break;
          default:
            throw new RuntimeException("FIXME: Unsupported type " + type);
        }
      }

      client.getOutputStream().write(fs.getBytes());
      fs.clear();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }