一、前言
关于schooner的java_memcached源码包com.schooner.MemCached.NativeHandler类,进行常用数据类型和byte[]间encode加密/decode解密处理,详情参见源码说明。
二、源码说明
package com.schooner.MemCached;@b@@b@import java.io.UnsupportedEncodingException;@b@import java.util.Date;@b@@b@public class NativeHandler@b@{@b@ public static final boolean isHandled(Object paramObject)@b@ {@b@ return ((paramObject instanceof Byte) || (paramObject instanceof Boolean) || (paramObject instanceof Integer) || (paramObject instanceof Long) || (paramObject instanceof Character) || (paramObject instanceof String) || (paramObject instanceof StringBuffer) || (paramObject instanceof Float) || (paramObject instanceof Short) || (paramObject instanceof Double) || (paramObject instanceof Date) || (paramObject instanceof StringBuilder) || (paramObject instanceof byte[]));@b@ }@b@@b@ public static final boolean isHandled(int paramInt)@b@ {@b@ return (((paramInt & 0x1) == 1) || ((paramInt & 0x2000) == 8192) || ((paramInt & 0x4) == 4) || ((paramInt & 0x4000) == 16384) || ((paramInt & 0x10) == 16) || ((paramInt & 0x20) == 32) || ((paramInt & 0x40) == 64) || ((paramInt & 0x80) == 128) || ((paramInt & 0x100) == 256) || ((paramInt & 0x200) == 512) || ((paramInt & 0x400) == 1024) || ((paramInt & 0x800) == 2048) || ((paramInt & 0x1000) == 4096));@b@ }@b@@b@ public static final int getMarkerFlag(Object paramObject)@b@ {@b@ if (paramObject instanceof Byte)@b@ return 1;@b@ if (paramObject instanceof Boolean)@b@ return 8192;@b@ if (paramObject instanceof Integer)@b@ return 4;@b@ if (paramObject instanceof Long)@b@ return 16384;@b@ if (paramObject instanceof Character)@b@ return 16;@b@ if (paramObject instanceof String)@b@ return 32;@b@ if (paramObject instanceof StringBuffer)@b@ return 64;@b@ if (paramObject instanceof Float)@b@ return 128;@b@ if (paramObject instanceof Short)@b@ return 256;@b@ if (paramObject instanceof Double)@b@ return 512;@b@ if (paramObject instanceof Date)@b@ return 1024;@b@ if (paramObject instanceof StringBuilder)@b@ return 2048;@b@ if (paramObject instanceof byte[])@b@ return 4096;@b@ return 0;@b@ }@b@@b@ public static byte[] encode(Object paramObject)@b@ throws UnsupportedEncodingException@b@ {@b@ if (paramObject instanceof Byte)@b@ return encode((Byte)paramObject);@b@ if (paramObject instanceof Boolean)@b@ return encode((Boolean)paramObject);@b@ if (paramObject instanceof Integer)@b@ return encode(((Integer)paramObject).intValue());@b@ if (paramObject instanceof Long)@b@ return encode(((Long)paramObject).longValue());@b@ if (paramObject instanceof Character)@b@ return encode((Character)paramObject);@b@ if (paramObject instanceof String)@b@ return encode((String)paramObject);@b@ if (paramObject instanceof StringBuffer)@b@ return encode((StringBuffer)paramObject);@b@ if (paramObject instanceof Float)@b@ return encode(((Float)paramObject).floatValue());@b@ if (paramObject instanceof Short)@b@ return encode((Short)paramObject);@b@ if (paramObject instanceof Double)@b@ return encode(((Double)paramObject).doubleValue());@b@ if (paramObject instanceof Date)@b@ return encode((Date)paramObject);@b@ if (paramObject instanceof StringBuilder)@b@ return encode((StringBuilder)paramObject);@b@ if (paramObject instanceof byte[])@b@ return encode((byte[])(byte[])paramObject);@b@ return null;@b@ }@b@@b@ protected static byte[] encode(Byte paramByte)@b@ {@b@ byte[] arrayOfByte = new byte[1];@b@ arrayOfByte[0] = paramByte.byteValue();@b@ return arrayOfByte;@b@ }@b@@b@ protected static byte[] encode(Boolean paramBoolean)@b@ {@b@ byte[] arrayOfByte = new byte[1];@b@ if (paramBoolean.booleanValue())@b@ arrayOfByte[0] = 1;@b@ else@b@ arrayOfByte[0] = 0;@b@ return arrayOfByte;@b@ }@b@@b@ protected static byte[] encode(int paramInt)@b@ {@b@ return getBytes(paramInt);@b@ }@b@@b@ protected static byte[] encode(long paramLong)@b@ throws UnsupportedEncodingException@b@ {@b@ return getBytes(paramLong);@b@ }@b@@b@ protected static byte[] encode(Date paramDate)@b@ {@b@ return getBytes(paramDate.getTime());@b@ }@b@@b@ protected static byte[] encode(Character paramCharacter)@b@ {@b@ return encode(paramCharacter.charValue());@b@ }@b@@b@ protected static byte[] encode(String paramString)@b@ throws UnsupportedEncodingException@b@ {@b@ return paramString.getBytes("UTF-8");@b@ }@b@@b@ protected static byte[] encode(StringBuffer paramStringBuffer)@b@ throws UnsupportedEncodingException@b@ {@b@ return encode(paramStringBuffer.toString());@b@ }@b@@b@ protected static byte[] encode(float paramFloat)@b@ throws UnsupportedEncodingException@b@ {@b@ return encode(Float.floatToIntBits(paramFloat));@b@ }@b@@b@ protected static byte[] encode(Short paramShort)@b@ throws UnsupportedEncodingException@b@ {@b@ return encode(paramShort.shortValue());@b@ }@b@@b@ protected static byte[] encode(double paramDouble)@b@ throws UnsupportedEncodingException@b@ {@b@ return encode(Double.doubleToLongBits(paramDouble));@b@ }@b@@b@ protected static byte[] encode(StringBuilder paramStringBuilder)@b@ throws UnsupportedEncodingException@b@ {@b@ return encode(paramStringBuilder.toString());@b@ }@b@@b@ protected static byte[] encode(byte[] paramArrayOfByte)@b@ {@b@ return paramArrayOfByte;@b@ }@b@@b@ protected static byte[] getBytes(long paramLong)@b@ {@b@ byte[] arrayOfByte = new byte[8];@b@ arrayOfByte[0] = (byte)(int)(paramLong >> 56 & 0xFF);@b@ arrayOfByte[1] = (byte)(int)(paramLong >> 48 & 0xFF);@b@ arrayOfByte[2] = (byte)(int)(paramLong >> 40 & 0xFF);@b@ arrayOfByte[3] = (byte)(int)(paramLong >> 32 & 0xFF);@b@ arrayOfByte[4] = (byte)(int)(paramLong >> 24 & 0xFF);@b@ arrayOfByte[5] = (byte)(int)(paramLong >> 16 & 0xFF);@b@ arrayOfByte[6] = (byte)(int)(paramLong >> 8 & 0xFF);@b@ arrayOfByte[7] = (byte)(int)(paramLong >> 0 & 0xFF);@b@ return arrayOfByte;@b@ }@b@@b@ protected static byte[] getBytes(int paramInt)@b@ {@b@ byte[] arrayOfByte = new byte[4];@b@ arrayOfByte[0] = (byte)(paramInt >> 24 & 0xFF);@b@ arrayOfByte[1] = (byte)(paramInt >> 16 & 0xFF);@b@ arrayOfByte[2] = (byte)(paramInt >> 8 & 0xFF);@b@ arrayOfByte[3] = (byte)(paramInt >> 0 & 0xFF);@b@ return arrayOfByte;@b@ }@b@@b@ public static Object decode(byte[] paramArrayOfByte, int paramInt)@b@ throws UnsupportedEncodingException@b@ {@b@ if (paramArrayOfByte.length < 1)@b@ return null;@b@ if ((paramInt & 0x1) == 1)@b@ return decodeByte(paramArrayOfByte);@b@ if ((paramInt & 0x2000) == 8192)@b@ return decodeBoolean(paramArrayOfByte);@b@ if ((paramInt & 0x4) == 4)@b@ return decodeInteger(paramArrayOfByte);@b@ if ((paramInt & 0x4000) == 16384)@b@ return decodeLong(paramArrayOfByte);@b@ if ((paramInt & 0x10) == 16)@b@ return decodeCharacter(paramArrayOfByte);@b@ if ((paramInt & 0x20) == 32)@b@ return decodeString(paramArrayOfByte);@b@ if ((paramInt & 0x40) == 64)@b@ return decodeStringBuffer(paramArrayOfByte);@b@ if ((paramInt & 0x80) == 128)@b@ return decodeFloat(paramArrayOfByte);@b@ if ((paramInt & 0x100) == 256)@b@ return decodeShort(paramArrayOfByte);@b@ if ((paramInt & 0x200) == 512)@b@ return decodeDouble(paramArrayOfByte);@b@ if ((paramInt & 0x400) == 1024)@b@ return decodeDate(paramArrayOfByte);@b@ if ((paramInt & 0x800) == 2048)@b@ return decodeStringBuilder(paramArrayOfByte);@b@ if ((paramInt & 0x1000) == 4096)@b@ return decodeByteArr(paramArrayOfByte);@b@ return null;@b@ }@b@@b@ protected static Byte decodeByte(byte[] paramArrayOfByte)@b@ {@b@ return new Byte(paramArrayOfByte[0]);@b@ }@b@@b@ protected static Boolean decodeBoolean(byte[] paramArrayOfByte)@b@ {@b@ int i = (paramArrayOfByte[0] == 1) ? 1 : 0;@b@ return ((i != 0) ? Boolean.TRUE : Boolean.FALSE);@b@ }@b@@b@ protected static Integer decodeInteger(byte[] paramArrayOfByte)@b@ {@b@ return new Integer(toInt(paramArrayOfByte));@b@ }@b@@b@ protected static Long decodeLong(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ return new Long(toLong(paramArrayOfByte));@b@ }@b@@b@ protected static Character decodeCharacter(byte[] paramArrayOfByte)@b@ {@b@ return new Character((char)decodeInteger(paramArrayOfByte).intValue());@b@ }@b@@b@ protected static String decodeString(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ return new String(paramArrayOfByte, "UTF-8");@b@ }@b@@b@ protected static StringBuffer decodeStringBuffer(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ return new StringBuffer(decodeString(paramArrayOfByte));@b@ }@b@@b@ protected static Float decodeFloat(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ Integer localInteger = decodeInteger(paramArrayOfByte);@b@ return new Float(Float.intBitsToFloat(localInteger.intValue()));@b@ }@b@@b@ protected static Short decodeShort(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ return new Short((short)decodeInteger(paramArrayOfByte).intValue());@b@ }@b@@b@ protected static Double decodeDouble(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ Long localLong = decodeLong(paramArrayOfByte);@b@ return new Double(Double.longBitsToDouble(localLong.longValue()));@b@ }@b@@b@ protected static Date decodeDate(byte[] paramArrayOfByte)@b@ {@b@ return new Date(toLong(paramArrayOfByte));@b@ }@b@@b@ protected static StringBuilder decodeStringBuilder(byte[] paramArrayOfByte)@b@ throws UnsupportedEncodingException@b@ {@b@ return new StringBuilder(decodeString(paramArrayOfByte));@b@ }@b@@b@ protected static byte[] decodeByteArr(byte[] paramArrayOfByte)@b@ {@b@ return paramArrayOfByte;@b@ }@b@@b@ protected static int toInt(byte[] paramArrayOfByte)@b@ {@b@ return (((paramArrayOfByte[3] & 0xFF) << 32) + ((paramArrayOfByte[2] & 0xFF) << 40) + ((paramArrayOfByte[1] & 0xFF) << 48) + ((paramArrayOfByte[0] & 0xFF) << 56));@b@ }@b@@b@ protected static long toLong(byte[] paramArrayOfByte)@b@ {@b@ return ((paramArrayOfByte[7] & 0xFF) + ((paramArrayOfByte[6] & 0xFF) << 8) + ((paramArrayOfByte[5] & 0xFF) << 16) + ((paramArrayOfByte[4] & 0xFF) << 24) + ((paramArrayOfByte[3] & 0xFF) << 32) + ((paramArrayOfByte[2] & 0xFF) << 40) + ((paramArrayOfByte[1] & 0xFF) << 48) + ((paramArrayOfByte[0] & 0xFF) << 56));@b@ }@b@}