public ProtoBufferObject(byte[] buffer, int offset) { if (buffer == null) { throw new ProtoBufferException(string.Format("字节数组是空")); } if (offset > buffer.Length) { throw new ProtoBufferException(string.Format("offset > 字节数组的长度。")); } int tmpOffset = offset; Varint header = new Varint(buffer, tmpOffset); tmpOffset += header.Bytes.Length; int headerValue = header; FieldNumber = headerValue >> 3; WireType = (WireType)((byte)headerValue & 0x07); switch (WireType) { case WireType.Varint: Value = new Varint(buffer, tmpOffset);; tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset - offset]; Array.Copy(buffer, offset, Bytes, 0, Bytes.Length); break; case WireType.Bit64: Value = new Bit64(buffer, tmpOffset); tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset - offset]; Array.Copy(buffer, offset, Bytes, 0, Bytes.Length); break; case WireType.LengthDelimited: Varint strleng = new Varint(buffer, tmpOffset); int leng = strleng; tmpOffset += strleng.Bytes.Length; Value = new LenghDelimited(buffer, tmpOffset, leng); tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset - offset]; Array.Copy(buffer, offset, Bytes, 0, Bytes.Length); break; case WireType.Bit32: Value = new Bit32(buffer, tmpOffset); tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset - offset]; Array.Copy(buffer, offset, Bytes, 0, Bytes.Length); break; default: throw new ArgumentOutOfRangeException(); } }
public ProtoBufferObject(byte[] buffer, int offset) { if (buffer == null) { throw new ProtoBufferException(string.Format("字节数组是空")); } if (offset > buffer.Length) { throw new ProtoBufferException(string.Format("offset > 字节数组的长度。")); } int tmpOffset = offset; Varint header = new Varint(buffer,tmpOffset); tmpOffset += header.Bytes.Length; int headerValue = header; FieldNumber = headerValue >> 3; WireType = (WireType) ((byte) headerValue & 0x07); switch (WireType) { case WireType.Varint: Value = new Varint(buffer,tmpOffset);; tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset-offset]; Array.Copy(buffer,offset,Bytes,0,Bytes.Length); break; case WireType.Bit64: Value = new Bit64(buffer, tmpOffset); tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset-offset]; Array.Copy(buffer,offset,Bytes,0,Bytes.Length); break; case WireType.LengthDelimited: Varint strleng = new Varint(buffer,tmpOffset); int leng = strleng; tmpOffset += strleng.Bytes.Length; Value = new LenghDelimited(buffer, tmpOffset, leng); tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset-offset]; Array.Copy(buffer,offset,Bytes,0,Bytes.Length); break; case WireType.Bit32: Value = new Bit32(buffer,tmpOffset); tmpOffset += Value.Bytes.Length; Bytes = new byte[tmpOffset-offset]; Array.Copy(buffer,offset,Bytes,0,Bytes.Length); break; default: throw new ArgumentOutOfRangeException(); } }