Example usage for java.io Bits putShort

List of usage examples for java.io Bits putShort

Introduction

In this page you can find the example usage for java.io Bits putShort.

Prototype

static void putShort(byte[] b, int off, short val) 

Source Link

Usage

From source file:voldemort.store.cachestore.BlockUtil.java

public static byte[] toKeyBytes(Key key) {
    KeyType t = key.getType();//  w ww . j  ava2s. c  om
    byte[] bytes;
    int size;
    switch (t) {
    case INT:
        int value = (Integer) key.getKey();
        size = getIntSize(value);
        bytes = new byte[size + 1];
        bytes[0] = makeLen4Type(size, t.value);
        makeInt(bytes, value, size);
        return bytes;
    case LONG:
        long l = (Long) key.getKey();
        size = getLongSize(l);
        bytes = new byte[size + 1];
        bytes[0] = makeLen4Type(size, t.value);
        makeLong(bytes, l, size);
        return bytes;
    case STRING:
        byte[] str;
        try {
            str = ((String) key.getKey()).getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new StoreException(ex.getMessage(), ex);
        }
        //key for string can not longer than 32K
        short len = (short) str.length;
        bytes = new byte[1 + 2 + str.length];
        bytes[0] = t.value;
        Bits.putShort(bytes, 1, len);
        System.arraycopy(str, 0, bytes, 3, str.length);
        return bytes;
    case BARRAY:
        ByteArray array = (ByteArray) key.getKey();
        bytes = new byte[array.get().length + 1];
        bytes[0] = t.value;
        System.arraycopy(array.get(), 0, bytes, 1, bytes.length - 1);
        return bytes;
    case BYTEARY:
        byte[] bs = (byte[]) key.getKey();
        bytes = new byte[bs.length + 1];
        bytes[0] = t.value;
        System.arraycopy(bs, 0, bytes, 1, bytes.length - 1);
        return bytes;
    default:
        //@TODO support different serialize and deseiralze
        byte[] obj = toJBytes(key.getKey());
        bytes = new byte[1 + obj.length];
        bytes[0] = t.value;
        System.arraycopy(obj, 0, bytes, 1, obj.length);
        return bytes;
    }

}