一个用于网络编程封包解包的java类型转换类

 

public class VarType {
public int size;
private byte[] m_value;
private VarType()
{
}
public static VarType newVarTypeFromInt(int arg)
{
VarType var = new VarType();
var.size = 4;
var.m_value = new byte[4];
var.m_value[0] = (byte)((arg>>24)&0xFF);
var.m_value[1] = (byte)((arg>>16)&0xFF);
var.m_value[2] = (byte)((arg>>8)&0xFF);
var.m_value[3] = (byte)(arg&0xFF);
return var;
}
public static VarType newVarTypeFromShort(short arg)
{
VarType var = new VarType();
var.size = 2;
var.m_value = new byte[2];
var.m_value[0] = (byte)((arg>>8)&0xFF);
var.m_value[1] = (byte)(arg&0xFF);
return var;
}
public static VarType newVarTypeFromBytes(byte[] arg)
{
VarType var = new VarType();
var.size = arg.length;
var.m_value = arg;
return var;
}
public static VarType newVarTypeFromVarType(VarType arg)
{
VarType var = new VarType();
var.size = arg.size;
var.m_value = arg.getBytes();
return var;
}
public String stringAt(int index, int maxlen)
{
int len = size-index;
if (len > maxlen)
len = maxlen;
return getSubVar(index, len).getString();
}
public int intAt(int index)
{
return getSubVar(index, 4).getInt();
}
public short shortAt(int index)
{
return getSubVar(index, 2).getShort();
}
public byte byteAt(int index)
{
byte ret = 0;
try
{
ret = m_value[index];
}
catch (ArrayIndexOutOfBoundsException e)
{
throw e;
}
return ret;
}
public char charAt(int index)
{
byte b = byteAt(index);
return (char)(int)b;
}
public byte[] getBytes()
{
byte[] bs = new byte[size];
for (int i=0; i<size; i++)
bs[i] = m_value[i];
return bs;
}
public String getString()
{
StringBuffer sb = new StringBuffer();
for (int i=0; i<size && m_value[i]!=0; i++)
sb.append(m_value[i]);
return sb.toString();
}
public int getInt()
{
int ret = 0;
if (size <= 0)
ret = 0;
else
{
int l = 4;
if (l > size)
l = size;
for (int i=0; i<l; i++)
{
ret = (int)(ret << 8);
ret = (int)(ret + m_value[size-l+i]);
}
}
return ret;
}
public short getShort()
{
short ret = 0;
if (size <= 0)
ret = 0;
else
{
int l = 2;
if (l > size)
l = size;
for (int i=0; i<l; i++)
{
ret = (short)(ret << 8);
ret = (short)(ret + m_value[size-l+i]);
}
}
return ret;
}
public byte getByte()
{
if (size == 0)
return 0;
else
return m_value[size-1];
}
public char getChar()
{
if (size == 0)
return 0;
else
return (char)(int)m_value[size-1];
}
public VarType getSubVar(int start, int len)
{
VarType var;
try
{
int l = size - start;
if (len > l)
len = l;
byte[] bytes = new byte[len];
for (int i=0; i<len; i++)
bytes[i] = m_value[start+i];
var = VarType.newVarTypeFromBytes(bytes);
}
catch (ArrayIndexOutOfBoundsException e)
{
throw e;
}
catch (NullPointerException e)
{
throw e;
}
return var;
}
public VarType getLeftVar(int len)
{
if (len > size)
len = size;
return getSubVar(0, len);
}
public VarType getRightVar(int len)
{
if (len > size)
len = size;
return getSubVar(size-len, len);
}
public VarType appendString(String str)
{
return appendBytes(str.getBytes());
}
public VarType appendBytes(byte[] bytes)
{
int i;
byte[] newbytes = new byte[m_value.length+bytes.length];
for (i=0; i<size; i++)
newbytes[i] = m_value[i];
for (i=size; i<size+bytes.length; i++)
newbytes[i] = bytes[i-size];
size = newbytes.length;
m_value = newbytes;
return this;
}
public VarType appendChars(char[] bytes)
{
String str = new String(bytes);
byte[] tmp = str.getBytes();
return appendBytes(tmp);
}
public VarType appendInt(int i)
{
byte[] ibs = new byte[4];
ibs[0] = (byte)(i>>24&0xFF);
ibs[1] = (byte)(i>>16&0xFF);
ibs[2] = (byte)(i>> 8&0xFF);
ibs[3] = (byte)(i>> 0&0xFF);
return appendBytes(ibs);
}
public VarType appendShort(short s)
{
byte[] sbs = new byte[2];
sbs[0] = (byte)(s>> 8&0xFF);
sbs[1] = (byte)(s>> 0&0xFF);
return appendBytes(sbs);
}
public VarType appendByte(byte b)
{
int i;
byte[] newbytes = new byte[m_value.length+1];
for (i=0; i<size; i++)
newbytes[i] = m_value[i];
newbytes[i] = b;
size = newbytes.length;
m_value = newbytes;
return this;
}
public VarType appendChar(char c)
{
byte b = (byte)(int)c;
return appendByte(b);
}
public VarType appendVarType(VarType var)
{
return appendBytes(var.getBytes());
}
}

 

你可能感兴趣的:(java,编程,网络,String,Class,byte)