根据C++版的改编,刚刚改完,估计使用会有问题,对于uint8处理的不好
关于使用:
1 BitStream bs = new BitStream( );2 bs.WriteInt32( 123 );3 4 int a = bs.ReadInt32( );
非常简单
BitStream.cs
1 public class BitStream 2 { 3 #if __BITSTREAM_BIG_END 4 // Set up the read/write routines to PRoduce Big-End network streams. 5 private const int B16_1 = 0; 6 private const int B16_0 = 1; 7 8 private const int B32_3 = 0; 9 private const int B32_2 = 1; 10 private const int B32_1 = 2; 11 private const int B32_0 = 3; 12 13 private const int B64_7 = 0; 14 private const int B64_6 = 1; 15 private const int B64_5 = 2; 16 private const int B64_4 = 3; 17 private const int B64_3 = 4; 18 private const int B64_2 = 5; 19 private const int B64_1 = 6; 20 private const int B64_0 = 7; 21 22 #else 23 // Default to producing Little-End network streams. 24 private const int B16_1 = 1; 25 private const int B16_0 = 0; 26 27 private const int B32_3 = 3; 28 private const int B32_2 = 2; 29 private const int B32_1 = 1; 30 private const int B32_0 = 0; 31 32 private const int B64_7 = 7; 33 private const int B64_6 = 6; 34 private const int B64_5 = 5; 35 private const int B64_4 = 4; 36 private const int B64_3 = 3; 37 private const int B64_2 = 2; 38 private const int B64_1 = 1; 39 private const int B64_0 = 0; 40 #endif 41 public const int BITSTREAM_STACK_ALLOCATION_SIZE = 2048; 42 43 /// Default Constructor 44 public static int BITS_TO_BYTES(int x) 45 { 46 return (((x) + 7) >> 3); 47 } 48 49 public static int BYTES_TO_BITS(int x) 50 { 51 return (x << 3); 52 } 53 54 /** 55 * @brief Packets encoding and decoding facilities 56 * 57 * Helper class to encode and decode packets. 58 * 59 */ 60 61 /** 62 * Default Constructor 63 */ 64 65 public BitStream() 66 { 67 numberOfBitsUsed = 0; 68 //numberOfBitsAllocated = 32 * 8; 69 numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE*8; 70 readOffset = 0; 71 //data = ( unsigned char* ) malloc( 32 ); 72 data = stackData; 73 copyData = true; 74 } 75 76 /** 77 * Preallocate some memory for the construction of the packet 78 * @param initialBytesToAllocate the amount of byte to pre-allocate. 79 */ 80 81 public BitStream(int initialBytesToAllocate) 82 { 83 numberOfBitsUsed = 0; 84 readOffset = 0; 85 if (initialBytesToAllocate <= BITSTREAM_STACK_ALLOCATION_SIZE) 86 { 87 data = stackData; 88 numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE*8; 89 } 90 else 91 { 92 data = new Byte[initialBytesToAllocate]; 93 numberOfBitsAllocated = initialBytesToAllocate << 3; 94 } 95 copyData = true; 96 } 97 98 /** 99 * Initialize the BitStream object using data from the network. 100 * Set _copyData to true if you want to make an internal copy of 101 * the data you are passing. You can then Write and do all other 102 * Operations Set it to false if you want to just use a pointer to 103 * the data you are passing, in order to save memory and speed. 104 * You should only then do read operations. 105 * @param _data An array of bytes. 106 * @param lengthInBytes Size of the @em _data. 107 * @param _copyData Does a copy of the input data. 108 */ 109 110 public BitStream(Byte[] _data, int lengthInBytes, bool _copyData) 111 { 112 numberOfBitsUsed = lengthInBytes << 3; 113 readOffset = 0; 114 copyData = _copyData; 115 numberOfBitsAllocated = lengthInBytes << 3; 116 117 if (copyData) 118 { 119 if (lengthInBytes > 0) 120 { 121 if (lengthInBytes < BITSTREAM_STACK_ALLOCATION_SIZE) 122 { 123 data = stackData; 124 numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE << 3; 125 } 126 else 127 { 128 data = new Byte[lengthInBytes]; 129 } 130 _data.CopyTo(data, 0); 131 } 132 else 133 data = null; 134 } 135 else 136 { 137 data = _data; 138 numberOfBitsUsed = 0; 139 } 140 } 141 142 // 143 public BitStream(Byte[] _data, int lengthInBytes, int datasize) 144 { 145 numberOfBitsUsed = datasize << 3; 146 readOffset = 0; 147 numberOfBitsAllocated = lengthInBytes << 3; 148 data = _data; 149 copyData = false; 150 } 151 152 /** 153 * Destructor 154 */ 155 //~BitStream(){} 156 /** 157 * Reset the bitstream for reuse 158 */ 159 160 private void Reset() 161 { 162 if (numberOfBitsUsed > 0) 163 { 164 // memset(data, 0, BITS_TO_BYTES(numberOfBitsUsed)); 165 } 166 // Don't free memory here for speed efficiency 167 //free(data); // Use realloc and free so we are more efficient than delete and new for resizing 168 numberOfBitsUsed = 0; 169 //numberOfBitsAllocated=8; 170 readOffset = 0; 171 } 172 173 public void SetBuffer(Byte[] _data, int lengthInBytes, int datasize) 174 { 175 numberOfBitsUsed = datasize << 3; 176 readOffset = 0; 177 numberOfBitsAllocated = lengthInBytes << 3; 178 data = _data; 179 copyData = false; 180 } 181 182 public void ClearBuffer() 183 { 184 numberOfBitsUsed = 0; 185 readOffset = 0; 186 numberOfBitsAllocated = 0; 187 data = null; 188 } 189 190 /** 191 * Write the native types to the end of the buffer 192 * without any compression mecanism. 193 * @param input The data 194 */ 195 196 public void WriteBool(bool input) 197 { 198 if (input) 199 WriteInt8(1); 200 else 201 WriteInt8(0); 202 } 203 204 /** 205 * Write the native types to the end of the buffer 206 * without any compression mecanism. 207 * @param input The data 208 */ 209 210 public void WriteUInt8(Byte input) 211 { 212 WriteBits(BitConverter.GetBytes(input), sizeof (Byte)*8, true); 213 } 214 215 // 216 217 /** 218 * Write the native types to the end of the buffer 219 * without any compression mecanism. 220 * @param input The data 221 */ 222 223 public void WriteInt8(SByte input) 224 { 225 WriteBits(BitConverter.GetBytes(input), sizeof (SByte)*8, true); 226 } 227 228 /** 229 * Write the native types to the end of the buffer 230 * without any compression mecanism. 231 * @param input The data 232 */ 233 234 public void WriteUInt16(UInt16 input) 235 { 236 var uint16w = new Byte[2]; 237 uint16w[B16_1] = (Byte) ((Byte) (input >> 8) & (0xff)); 238
新闻热点
疑难解答