package com.dd.model.protocol;
import java.nio.ByteBuffer;
public class ValueDecoder
{
private String serialData;
private DataTypeEnum type;
private byte []serialByte;
public ValueDecoder(DataTypeEnum type,byte []serialByte)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < serialByte.length; i++)
{
sb.append(String.format("%02x", serialByte[i]));
}
this.serialByte = serialByte;
this.serialData = sb.toString();
this.type = type;
}
public Value doDecoder()
{
Value result = null;
if (type == DataTypeEnum.BOOL)
{
Boolean t = stringToBoolean(serialData);
result = new Value<>(t);
} else if (type == DataTypeEnum.INT8)
{
Character t = stringToChar(serialData);
result = new Value<>(t);
} else if (type == DataTypeEnum.INT16)
{
Short t = stringToShort(serialData);
result = new Value<>(t);
} else if (type == DataTypeEnum.INT32)
{
Integer t = stringToInt(serialData);
result = new Value<>(t);
} else if (type == DataTypeEnum.FLOAT)
{
Float t = stringToFloat(serialData);
result = new Value<>(t);
} else if (type == DataTypeEnum.STRING)
{
result = new Value(serialData);
}
return result;
}
private Boolean stringToBoolean(String string)
{
Boolean result = null;
if(string.length()==2)
{
if((string.charAt(0) != 0) ||((string.charAt(1) != 0)))result = new Boolean(true);
else result = new Boolean(false);
}
return result;
}
private Character stringToChar(String string)
{
Character result = null;
if(string.length()==2)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getChar();
//result = new Character((char)t[0]);
}
return result;
}
private Short stringToShort(String string)
{
Short result = null;
if(string.length()==4)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getShort();
//result = new Short((short) ((t[0]<<8) + t[1]));
}
return result;
}
private Integer stringToInt(String string)
{
Integer result = null;
if(string.length()==8)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getInt();
//result = new Integer((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
}
return result;
}
private Float stringToFloat(String string)
{
Float result = null;
if(string.length()==8)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getFloat();
}
return result;
}
private byte[] stringToByte(String string)
{
if((string.length() %2) != 0)return null;
ByteBuffer buf = ByteBuffer.allocate(string.length());
int len = string.length() /2;
byte []result = new byte[len];
for (int i = 0; i < result.length; i++)
{
char hi = string.charAt(2*i);
char low = string.charAt(2*i+1);
int c = 0;
if(Character.isDigit(hi)) c |= hi - '0';
else if(Character.isUpperCase(hi))c |= hi - 'A' + 10;
else if(Character.isLowerCase(hi))c |= hi - 'a'+ 10;
c = (c * 16);
if(Character.isDigit(low)) c |= low - '0';
else if(Character.isUpperCase(low))c |= low - 'A' + 10;
else if(Character.isLowerCase(low))c |= low - 'a' + 10;
result[i] = (byte)c;
}
return result;
}
}