各类数据的序列化以及反序列化的方法(与Java服务器通讯)
2017-07-20 09:06
531 查看
上次在博客中说了int类型数据的序列化以及反序列化,但是通讯的时候不止是只有int类型的数据,还有很多类型,string类型,flaot类型,bool类型等等。
显然只有int类型是不够的,所以我们要在项目中新添加一个类,专门用来序列化以反序列化数据用,这样通讯时只需要调用这个方法即可,由于数据类型比较多,我就一块写一下下,供大家参考,下面代码如下:
我先贴代码,至于注意事项我在下面说。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ByteBuf{
private List<byte> bytes = new List<byte>();
public int Length
{
get
{
return this.bytes.Count;
}
}
public int Postion
{
get;
set;
}
public byte [] Buffer
{
get
{
return this.bytes.ToArray();
}
}
//public ByteArray()
//{
//}
/// <summary>
/// 写int类型的数据(序列化)
/// </summary>
/// <param 长度="valve"></param>
public void WriteInt(int valve)
{
byte[] by = BitConverter.GetBytes(valve);
Array.Reverse(by);
this.bytes.AddRange(by);
}
/// <summary>
/// 读int类型的数据(反序列化)
/// </summary>
/// <returns></returns>
public int ReadInt()
{
byte[] array = new byte[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 4;
Array.Reverse(array);
return BitConverter.ToInt32(array, 0);
}
/// <summary>
/// 写byte类型的数据
/// </summary>
/// <param name="value"></param>
/// <param name="offest"></param>
/// <param name="length"></param>
public void WirteBytes(byte[] value, int offest, int length)
{
for (int i = 0; i < length; i++)
{
this.bytes.Add(value[i + offest]);
}
}
/// <summary>
/// 读byte类型的数据
/// </summary>
/// <returns></returns>
public byte[] ReadBytes()
{
byte[] array = new byte[this.Length - this.Postion];
for (int i = 0; i < this.Length-this.Postion; i++)
{
4000
array[i] = this.bytes[i + this.Postion];
}
this.Postion = this.Length;
return array;
}
/// <summary>
/// 写double类型
/// </summary>
/// <param name="dou"></param>
public void WriteDouble(double dou)
{
byte[] bs = BitConverter.GetBytes(dou);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读double类型
/// </summary>
/// <returns></returns>
public double ReadDouble()
{
byte[] array = new byte[8];
for (int i = 0; i < 8; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 8;
Array.Reverse(array);
return BitConverter.ToDouble(array, 0);
}
/// <summary>
/// 写float类型
/// </summary>
/// <param name="flao"></param>
public void WriteFloat(float flao)
{
byte[] bs = BitConverter.GetBytes(flao);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读float类型
/// </summary>
/// <returns></returns>
public float ReadFloat()
{
byte[] array = new byte[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 4;
Array.Reverse(array);
return BitConverter.ToSingle(array, 0);
}
/// <summary>
/// 写long类型
/// </summary>
/// <param name="lon"></param>
public void WriteLong(long lon)
{
byte[] bs = BitConverter.GetBytes(lon);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读long类型
/// </summary>
/// <returns></returns>
public long ReadLong()
{
byte[] array = new byte[8];
for (int i = 0; i < 8; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 8;
Array.Reverse(array);
return BitConverter.ToInt64(array, 0);
}
/// <summary>
/// 写short类型
/// </summary>
/// <param name="sho"></param>
public void WriteShort(short sho)
{
byte[] bs = BitConverter.GetBytes(sho);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读short类型
/// </summary>
/// <returns></returns>
public short ReadShort()
{
byte[] array = new byte[2];
for (int i = 0; i < 2; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 2;
Array.Reverse(array);
return BitConverter.ToInt16(array, 0);
}
/// <summary>
/// 写char类型
/// </summary>
/// <param name="cha"></param>
public void WriteChar(char cha)
{
byte[] bs = BitConverter.GetBytes(cha);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读char类型
/// </summary>
/// <returns></returns>
public char ReadChar()
{
byte[] array = new byte[2];
for (int i = 0; i < 2; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 2;
Array.Reverse(array);
return BitConverter.ToChar(array, 0);
}
/// <summary>
/// 写string类型
/// </summary>
/// <param name="str"></param>
public void WriteString (string str)
{
byte[] bs = Encoding.UTF8.GetBytes(str);
Array.Reverse(bs);
this.WriteInt(bs.Length);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读string类型
/// </summary>
/// <returns></returns>
public string ReadString()
{
int num = this.ReadInt();
if (num<=0)
{
return string.Empty;
}
byte[] array = new byte[num];
for (int i = 0; i < num; i++)
{
array[i] = this.bytes[i + this.Postion];
}
this.Postion += num;
Array.Reverse(array);
return Encoding.UTF8.GetString(array);
}
/// <summary>
/// 写bool类型
/// </summary>
/// <param name="value"></param>
public void WriteBoolean(bool value)
{
if (value==true)
{
byte[] bs = BitConverter.GetBytes(1);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
else
{
byte[] bs = BitConverter.GetBytes(0);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
}
/// <summary>
/// 读bool类型
/// </summary>
/// <returns></returns>
public bool ReadBoolean()
{
byte[] array = new byte[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 4;
Array.Reverse(array);
int boo = BitConverter.ToInt32(array, 0);
return boo != 0;
}
}
这里就是所有类型的数据的序列化以及反序列化,我在前面一篇博客说过,传输时最好在客户端进行数据反转,否则服务器收到的数据时反过来的,但是string类型的数据不用反转,任何语言都是一样的。
显然只有int类型是不够的,所以我们要在项目中新添加一个类,专门用来序列化以反序列化数据用,这样通讯时只需要调用这个方法即可,由于数据类型比较多,我就一块写一下下,供大家参考,下面代码如下:
我先贴代码,至于注意事项我在下面说。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ByteBuf{
private List<byte> bytes = new List<byte>();
public int Length
{
get
{
return this.bytes.Count;
}
}
public int Postion
{
get;
set;
}
public byte [] Buffer
{
get
{
return this.bytes.ToArray();
}
}
//public ByteArray()
//{
//}
/// <summary>
/// 写int类型的数据(序列化)
/// </summary>
/// <param 长度="valve"></param>
public void WriteInt(int valve)
{
byte[] by = BitConverter.GetBytes(valve);
Array.Reverse(by);
this.bytes.AddRange(by);
}
/// <summary>
/// 读int类型的数据(反序列化)
/// </summary>
/// <returns></returns>
public int ReadInt()
{
byte[] array = new byte[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 4;
Array.Reverse(array);
return BitConverter.ToInt32(array, 0);
}
/// <summary>
/// 写byte类型的数据
/// </summary>
/// <param name="value"></param>
/// <param name="offest"></param>
/// <param name="length"></param>
public void WirteBytes(byte[] value, int offest, int length)
{
for (int i = 0; i < length; i++)
{
this.bytes.Add(value[i + offest]);
}
}
/// <summary>
/// 读byte类型的数据
/// </summary>
/// <returns></returns>
public byte[] ReadBytes()
{
byte[] array = new byte[this.Length - this.Postion];
for (int i = 0; i < this.Length-this.Postion; i++)
{
4000
array[i] = this.bytes[i + this.Postion];
}
this.Postion = this.Length;
return array;
}
/// <summary>
/// 写double类型
/// </summary>
/// <param name="dou"></param>
public void WriteDouble(double dou)
{
byte[] bs = BitConverter.GetBytes(dou);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读double类型
/// </summary>
/// <returns></returns>
public double ReadDouble()
{
byte[] array = new byte[8];
for (int i = 0; i < 8; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 8;
Array.Reverse(array);
return BitConverter.ToDouble(array, 0);
}
/// <summary>
/// 写float类型
/// </summary>
/// <param name="flao"></param>
public void WriteFloat(float flao)
{
byte[] bs = BitConverter.GetBytes(flao);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读float类型
/// </summary>
/// <returns></returns>
public float ReadFloat()
{
byte[] array = new byte[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 4;
Array.Reverse(array);
return BitConverter.ToSingle(array, 0);
}
/// <summary>
/// 写long类型
/// </summary>
/// <param name="lon"></param>
public void WriteLong(long lon)
{
byte[] bs = BitConverter.GetBytes(lon);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读long类型
/// </summary>
/// <returns></returns>
public long ReadLong()
{
byte[] array = new byte[8];
for (int i = 0; i < 8; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 8;
Array.Reverse(array);
return BitConverter.ToInt64(array, 0);
}
/// <summary>
/// 写short类型
/// </summary>
/// <param name="sho"></param>
public void WriteShort(short sho)
{
byte[] bs = BitConverter.GetBytes(sho);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读short类型
/// </summary>
/// <returns></returns>
public short ReadShort()
{
byte[] array = new byte[2];
for (int i = 0; i < 2; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 2;
Array.Reverse(array);
return BitConverter.ToInt16(array, 0);
}
/// <summary>
/// 写char类型
/// </summary>
/// <param name="cha"></param>
public void WriteChar(char cha)
{
byte[] bs = BitConverter.GetBytes(cha);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读char类型
/// </summary>
/// <returns></returns>
public char ReadChar()
{
byte[] array = new byte[2];
for (int i = 0; i < 2; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 2;
Array.Reverse(array);
return BitConverter.ToChar(array, 0);
}
/// <summary>
/// 写string类型
/// </summary>
/// <param name="str"></param>
public void WriteString (string str)
{
byte[] bs = Encoding.UTF8.GetBytes(str);
Array.Reverse(bs);
this.WriteInt(bs.Length);
this.bytes.AddRange(bs);
}
/// <summary>
/// 读string类型
/// </summary>
/// <returns></returns>
public string ReadString()
{
int num = this.ReadInt();
if (num<=0)
{
return string.Empty;
}
byte[] array = new byte[num];
for (int i = 0; i < num; i++)
{
array[i] = this.bytes[i + this.Postion];
}
this.Postion += num;
Array.Reverse(array);
return Encoding.UTF8.GetString(array);
}
/// <summary>
/// 写bool类型
/// </summary>
/// <param name="value"></param>
public void WriteBoolean(bool value)
{
if (value==true)
{
byte[] bs = BitConverter.GetBytes(1);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
else
{
byte[] bs = BitConverter.GetBytes(0);
Array.Reverse(bs);
this.bytes.AddRange(bs);
}
}
/// <summary>
/// 读bool类型
/// </summary>
/// <returns></returns>
public bool ReadBoolean()
{
byte[] array = new byte[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.bytes[i + this.Postion];
}
Postion += 4;
Array.Reverse(array);
int boo = BitConverter.ToInt32(array, 0);
return boo != 0;
}
}
这里就是所有类型的数据的序列化以及反序列化,我在前面一篇博客说过,传输时最好在客户端进行数据反转,否则服务器收到的数据时反过来的,但是string类型的数据不用反转,任何语言都是一样的。
相关文章推荐
- Int类型数据的序列化以及反序列化(与Java服务器通讯预热)
- Rhythmk 一步一步学 JAVA(3): java JSON 数据序列化 以及反序列化
- java POI导出excel(带有图片的数据)以及ftp上传图片到另一台服务器的方法参考代码
- Android中传递对象的三种方法以及Java对象的序列化和反序列化实践
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据
- 三种序列化的方法以及反序列化
- .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
- java序列化和反序列化以及serialVersionUID的作用
- java post数据服务器拒绝的解决方法
- T级图片数据Cache思路以及图片服务器搭建方法
- java序列化与反序列化以及保证数据一致性
- java序列化的引用问题以及将transitent的序列化方法
- java线程当中 sleep() join() yield()方法的简单比较以及如何保护共享数据介绍
- Java中对象的序列化和反序列化方法
- NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
- .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
- JAVA 对象序列化(三)——transient以及Externalizable的一种替代方法(使用Serializable)
- T级图片数据Cache思路以及图片服务器搭建方法
- Java中对象的序列化和反序列化方法
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据