ByteBuffer.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. using ET;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. /// <summary>
  6. /// 字节缓冲处理类,本类仅支持大字节序
  7. /// 本类非线程安全
  8. /// </summary>
  9. [Serializable]
  10. public class ByteBuffer //: ICloneable
  11. {
  12. //字节缓存区
  13. private byte[] buf;
  14. //读取索引
  15. private int readIndex = 0;
  16. //写入索引
  17. private int writeIndex = 0;
  18. //读取索引标记
  19. private int markReadIndex = 0;
  20. //写入索引标记
  21. private int markWirteIndex = 0;
  22. //缓存区字节数组的长度
  23. private int capacity;
  24. //对象池
  25. [StaticField]
  26. private static List<ByteBuffer> pool = new List<ByteBuffer>();
  27. [StaticField]
  28. private static int poolMaxCount = 200;
  29. //此对象是否池化
  30. private bool isPool = false;
  31. /// <summary>
  32. /// 构造方法
  33. /// </summary>
  34. /// <param name="capacity">初始容量</param>
  35. private ByteBuffer(int capacity)
  36. {
  37. this.buf = new byte[capacity];
  38. this.capacity = capacity;
  39. this.readIndex = 0;
  40. this.writeIndex = 0;
  41. }
  42. /// <summary>
  43. /// 构造方法
  44. /// </summary>
  45. /// <param name="bytes">初始字节数组</param>
  46. private ByteBuffer(byte[] bytes)
  47. {
  48. this.buf = new byte[bytes.Length];
  49. Array.Copy(bytes, 0, buf, 0, buf.Length);
  50. this.capacity = buf.Length;
  51. this.readIndex = 0;
  52. this.writeIndex = bytes.Length + 1;
  53. }
  54. /// <summary>
  55. /// 构建一个capacity长度的字节缓存区ByteBuffer对象
  56. /// </summary>
  57. /// <param name="capacity">初始容量</param>
  58. /// <param name="fromPool">
  59. /// true表示获取一个池化的ByteBuffer对象,池化的对象必须在调用Dispose后才会推入池中,此方法为线程安全的。
  60. /// 当为true时,从池中获取的对象的实际capacity值。
  61. /// </param>
  62. /// <returns>ByteBuffer对象</returns>
  63. public static ByteBuffer Allocate(int capacity, bool fromPool = false)
  64. {
  65. if(!fromPool)
  66. {
  67. return new ByteBuffer(capacity);
  68. }
  69. lock (pool)
  70. {
  71. ByteBuffer bbuf;
  72. if (pool.Count == 0)
  73. {
  74. bbuf = new ByteBuffer(capacity)
  75. {
  76. isPool = true
  77. };
  78. return bbuf;
  79. }
  80. int lastIndex = pool.Count - 1;
  81. bbuf = pool[lastIndex];
  82. pool.RemoveAt(lastIndex);
  83. if (!bbuf.isPool)
  84. {
  85. bbuf.isPool = true;
  86. }
  87. return bbuf;
  88. }
  89. }
  90. /// <summary>
  91. /// 构建一个以bytes为字节缓存区的ByteBuffer对象,一般不推荐使用
  92. /// </summary>
  93. /// <param name="bytes">初始字节数组</param>
  94. /// <param name="fromPool">
  95. /// true表示获取一个池化的ByteBuffer对象,池化的对象必须在调用Dispose后才会推入池中,此方法为线程安全的。
  96. /// </param>
  97. /// <returns>ByteBuffer对象</returns>
  98. public static ByteBuffer Allocate(byte[] bytes, bool fromPool = false)
  99. {
  100. if (!fromPool)
  101. {
  102. return new ByteBuffer(bytes);
  103. }
  104. lock (pool)
  105. {
  106. ByteBuffer bbuf;
  107. if (pool.Count == 0)
  108. {
  109. bbuf = new ByteBuffer(bytes)
  110. {
  111. isPool = true
  112. };
  113. return bbuf;
  114. }
  115. int lastIndex = pool.Count - 1;
  116. bbuf = pool[lastIndex];
  117. bbuf.WriteBytes(bytes);
  118. pool.RemoveAt(lastIndex);
  119. if (!bbuf.isPool)
  120. {
  121. bbuf.isPool = true;
  122. }
  123. return bbuf;
  124. }
  125. }
  126. /// <summary>
  127. /// 根据value,确定大于此length的最近的2次方数,如length=7,则返回值为8;length=12,则返回16
  128. /// </summary>
  129. /// <param name="value">参考容量</param>
  130. /// <returns>比参考容量大的最接近的2次方数</returns>
  131. private int FixLength(int value)
  132. {
  133. if (value == 0)
  134. {
  135. return 1;
  136. }
  137. value--;
  138. value |= value >> 1;
  139. value |= value >> 2;
  140. value |= value >> 4;
  141. value |= value >> 8;
  142. value |= value >> 16;
  143. return value + 1;
  144. }
  145. /// <summary>
  146. /// 翻转字节数组,如果本地字节序列为低字节序列,则进行翻转以转换为高字节序列
  147. /// </summary>
  148. /// <param name="bytes">待转为高字节序的字节数组</param>
  149. /// <returns>高字节序列的字节数组</returns>
  150. private byte[] Flip(byte[] bytes)
  151. {
  152. if (!BitConverter.IsLittleEndian)
  153. {
  154. Array.Reverse(bytes);
  155. }
  156. return bytes;
  157. }
  158. /// <summary>
  159. /// 确定内部字节缓存数组的大小
  160. /// </summary>
  161. /// <param name="currLen">当前容量</param>
  162. /// <param name="futureLen">将来的容量</param>
  163. /// <returns>当前缓冲区的最大容量</returns>
  164. private int FixSizeAndReset(int currLen, int futureLen)
  165. {
  166. if (futureLen > currLen)
  167. {
  168. //以原大小的2次方数的两倍确定内部字节缓存区大小
  169. int size = FixLength(currLen) * 2;
  170. if (futureLen > size)
  171. {
  172. //以将来的大小的2次方的两倍确定内部字节缓存区大小
  173. size = FixLength(futureLen) * 2;
  174. }
  175. byte[] newbuf = new byte[size];
  176. Array.Copy(buf, 0, newbuf, 0, currLen);
  177. buf = newbuf;
  178. capacity = size;
  179. }
  180. return futureLen;
  181. }
  182. /// <summary>
  183. /// 将bytes字节数组从startIndex开始的length字节写入到此缓存区
  184. /// </summary>
  185. /// <param name="bytes">待写入的字节数据</param>
  186. /// <param name="startIndex">写入的开始位置</param>
  187. /// <param name="length">写入的长度</param>
  188. public void WriteBytes(byte[] bytes, int startIndex, int length)
  189. {
  190. int offset = length - startIndex;
  191. if (offset <= 0) return;
  192. int total = offset + writeIndex;
  193. int len = buf.Length;
  194. FixSizeAndReset(len, total);
  195. for (int i = writeIndex, j = startIndex; i < total; i++, j++)
  196. {
  197. buf[i] = bytes[j];
  198. }
  199. writeIndex = total;
  200. }
  201. /// <summary>
  202. /// 将字节数组中从0到length的元素写入缓存区
  203. /// </summary>
  204. /// <param name="bytes">待写入的字节数据</param>
  205. /// <param name="length">写入的长度</param>
  206. public void WriteBytes(byte[] bytes, int length)
  207. {
  208. WriteBytes(bytes, 0, length);
  209. }
  210. /// <summary>
  211. /// 将字节数组全部写入缓存区
  212. /// </summary>
  213. /// <param name="bytes">待写入的字节数据</param>
  214. public void WriteBytes(byte[] bytes)
  215. {
  216. WriteBytes(bytes, bytes.Length);
  217. }
  218. /// <summary>
  219. /// 将一个ByteBuffer的有效字节区写入此缓存区中
  220. /// </summary>
  221. /// <param name="buffer">待写入的字节缓存区</param>
  222. public void Write(ByteBuffer buffer)
  223. {
  224. if (buffer == null) return;
  225. if (buffer.ReadableBytes <= 0) return;
  226. WriteBytes(buffer.ToArray());
  227. }
  228. /// <summary>
  229. /// 写入一个int16数据
  230. /// </summary>
  231. /// <param name="value">short数据</param>
  232. public void WriteShort(short value)
  233. {
  234. WriteBytes(Flip(BitConverter.GetBytes(value)));
  235. }
  236. /// <summary>
  237. /// 写入一个uint16数据
  238. /// </summary>
  239. /// <param name="value">ushort数据</param>
  240. public void WriteUshort(ushort value)
  241. {
  242. WriteBytes(Flip(BitConverter.GetBytes(value)));
  243. }
  244. /// <summary>
  245. /// 写入一个int32数据
  246. /// </summary>
  247. /// <param name="value">int数据</param>
  248. public void WriteInt(int value)
  249. {
  250. WriteBytes(Flip(BitConverter.GetBytes(value)));
  251. }
  252. /// <summary>
  253. /// 写入一个uint32数据
  254. /// </summary>
  255. /// <param name="value">uint数据</param>
  256. public void WriteUint(uint value)
  257. {
  258. WriteBytes(Flip(BitConverter.GetBytes(value)));
  259. }
  260. /// <summary>
  261. /// 写入一个int64数据
  262. /// </summary>
  263. /// <param name="value">long数据</param>
  264. public void WriteLong(long value)
  265. {
  266. WriteBytes(Flip(BitConverter.GetBytes(value)));
  267. }
  268. /// <summary>
  269. /// 写入一个uint64数据
  270. /// </summary>
  271. /// <param name="value">ulong数据</param>
  272. public void WriteUlong(ulong value)
  273. {
  274. WriteBytes(Flip(BitConverter.GetBytes(value)));
  275. }
  276. /// <summary>
  277. /// 写入一个float数据
  278. /// </summary>
  279. /// <param name="value">float数据</param>
  280. public void WriteFloat(float value)
  281. {
  282. WriteBytes(Flip(BitConverter.GetBytes(value)));
  283. }
  284. /// <summary>
  285. /// 写入一个byte数据
  286. /// </summary>
  287. /// <param name="value">byte数据</param>
  288. public void WriteByte(byte value)
  289. {
  290. int afterLen = writeIndex + 1;
  291. int len = buf.Length;
  292. FixSizeAndReset(len, afterLen);
  293. buf[writeIndex] = value;
  294. writeIndex = afterLen;
  295. }
  296. /// <summary>
  297. /// 写入一个byte数据
  298. /// </summary>
  299. /// <param name="value">byte数据</param>
  300. public void WriteByte(int value)
  301. {
  302. byte b = (byte)value;
  303. WriteByte(b);
  304. }
  305. /// <summary>
  306. /// 写入一个double类型数据
  307. /// </summary>
  308. /// <param name="value">double数据</param>
  309. public void WriteDouble(double value)
  310. {
  311. WriteBytes(Flip(BitConverter.GetBytes(value)));
  312. }
  313. /// <summary>
  314. /// 写入一个字符
  315. /// </summary>
  316. /// <param name="value"></param>
  317. public void WriteChar(char value)
  318. {
  319. WriteBytes(Flip(BitConverter.GetBytes(value)));
  320. }
  321. /// <summary>
  322. /// 写入一个布尔型数据
  323. /// </summary>
  324. /// <param name="value"></param>
  325. public void WriteBoolean(bool value)
  326. {
  327. WriteBytes(Flip(BitConverter.GetBytes(value)));
  328. }
  329. /// <summary>
  330. /// 读取一个字节
  331. /// </summary>
  332. /// <returns>字节数据</returns>
  333. public byte ReadByte()
  334. {
  335. byte b = buf[readIndex];
  336. readIndex++;
  337. return b;
  338. }
  339. /// <summary>
  340. /// 获取从index索引处开始len长度的字节
  341. /// </summary>
  342. /// <param name="index"></param>
  343. /// <param name="len"></param>
  344. /// <returns></returns>
  345. private byte[] Get(int index, int len)
  346. {
  347. byte[] bytes = new byte[len];
  348. Array.Copy(buf, index, bytes, 0, len);
  349. return Flip(bytes);
  350. }
  351. /// <summary>
  352. /// 从读取索引位置开始读取len长度的字节数组
  353. /// </summary>
  354. /// <param name="len">待读取的字节长度</param>
  355. /// <returns>字节数组</returns>
  356. private byte[] Read(int len)
  357. {
  358. byte[] bytes = Get(readIndex, len);
  359. readIndex += len;
  360. return bytes;
  361. }
  362. /// <summary>
  363. /// 读取一个uint16数据
  364. /// </summary>
  365. /// <returns>ushort数据</returns>
  366. public ushort ReadUshort()
  367. {
  368. return BitConverter.ToUInt16(Read(2), 0);
  369. }
  370. /// <summary>
  371. /// 读取一个int16数据
  372. /// </summary>
  373. /// <returns>short数据</returns>
  374. public short ReadShort()
  375. {
  376. return BitConverter.ToInt16(Read(2), 0);
  377. }
  378. /// <summary>
  379. /// 读取一个uint32数据
  380. /// </summary>
  381. /// <returns>uint数据</returns>
  382. public uint ReadUint()
  383. {
  384. return BitConverter.ToUInt32(Read(4), 0);
  385. }
  386. /// <summary>
  387. /// 读取一个int32数据
  388. /// </summary>
  389. /// <returns>int数据</returns>
  390. public int ReadInt()
  391. {
  392. return BitConverter.ToInt32(Read(4), 0);
  393. }
  394. /// <summary>
  395. /// 读取一个uint64数据
  396. /// </summary>
  397. /// <returns>ulong数据</returns>
  398. public ulong ReadUlong()
  399. {
  400. return BitConverter.ToUInt64(Read(8), 0);
  401. }
  402. /// <summary>
  403. /// 读取一个long数据
  404. /// </summary>
  405. /// <returns>long数据</returns>
  406. public long ReadLong()
  407. {
  408. return BitConverter.ToInt64(Read(8), 0);
  409. }
  410. /// <summary>
  411. /// 读取一个float数据
  412. /// </summary>
  413. /// <returns>float数据</returns>
  414. public float ReadFloat()
  415. {
  416. return BitConverter.ToSingle(Read(4), 0);
  417. }
  418. /// <summary>
  419. /// 读取一个double数据
  420. /// </summary>
  421. /// <returns>double数据</returns>
  422. public double ReadDouble()
  423. {
  424. return BitConverter.ToDouble(Read(8), 0);
  425. }
  426. /// <summary>
  427. /// 读取一个字符
  428. /// </summary>
  429. /// <returns></returns>
  430. public char ReadChar()
  431. {
  432. return BitConverter.ToChar(Read(2), 0);
  433. }
  434. /// <summary>
  435. /// 读取布尔型数据
  436. /// </summary>
  437. /// <returns></returns>
  438. public bool ReadBoolean()
  439. {
  440. return BitConverter.ToBoolean(Read(1), 0);
  441. }
  442. /// <summary>
  443. /// 从读取索引位置开始读取len长度的字节到disbytes目标字节数组中
  444. /// </summary>
  445. /// <param name="disbytes">读取的字节将存入此字节数组</param>
  446. /// <param name="disstart">目标字节数组的写入索引</param>
  447. /// <param name="len">读取的长度</param>
  448. public void ReadBytes(byte[] disbytes, int disstart, int len)
  449. {
  450. int size = disstart + len;
  451. for (int i = disstart; i < size; i++)
  452. {
  453. disbytes[i] = this.ReadByte();
  454. }
  455. }
  456. public string ReadString(int length)
  457. {
  458. var str = Encoding.UTF8.GetString(buf, readIndex, length);
  459. readIndex += length;
  460. return str;
  461. }
  462. /// <summary>
  463. /// 获取一个字节
  464. /// </summary>
  465. /// <param name="index"></param>
  466. /// <returns></returns>
  467. public byte GetByte(int index)
  468. {
  469. return buf[index];
  470. }
  471. /// <summary>
  472. /// 获取一个字节
  473. /// </summary>
  474. /// <returns></returns>
  475. public byte GetByte()
  476. {
  477. return GetByte(readIndex);
  478. }
  479. /// <summary>
  480. /// 获取一个双精度浮点数据,不改变数据内容
  481. /// </summary>
  482. /// <param name="index">字节索引</param>
  483. /// <returns></returns>
  484. public double GetDouble(int index)
  485. {
  486. return BitConverter.ToDouble(Get(index, 8), 0);
  487. }
  488. /// <summary>
  489. /// 获取一个双精度浮点数据,不改变数据内容
  490. /// </summary>
  491. /// <returns></returns>
  492. public double GetDouble()
  493. {
  494. return GetDouble(readIndex);
  495. }
  496. /// <summary>
  497. /// 获取一个浮点数据,不改变数据内容
  498. /// </summary>
  499. /// <param name="index">字节索引</param>
  500. /// <returns></returns>
  501. public float GetFloat(int index)
  502. {
  503. return BitConverter.ToSingle(Get(index, 4), 0);
  504. }
  505. /// <summary>
  506. /// 获取一个浮点数据,不改变数据内容
  507. /// </summary>
  508. /// <returns></returns>
  509. public float GetFloat()
  510. {
  511. return GetFloat(readIndex);
  512. }
  513. /// <summary>
  514. /// 获取一个长整形数据,不改变数据内容
  515. /// </summary>
  516. /// <param name="index">字节索引</param>
  517. /// <returns></returns>
  518. public long GetLong(int index)
  519. {
  520. return BitConverter.ToInt64(Get(index, 8), 0);
  521. }
  522. /// <summary>
  523. /// 获取一个长整形数据,不改变数据内容
  524. /// </summary>
  525. /// <returns></returns>
  526. public long GetLong()
  527. {
  528. return GetLong(readIndex);
  529. }
  530. /// <summary>
  531. /// 获取一个长整形数据,不改变数据内容
  532. /// </summary>
  533. /// <param name="index">字节索引</param>
  534. /// <returns></returns>
  535. public ulong GetUlong(int index)
  536. {
  537. return BitConverter.ToUInt64(Get(index, 8), 0);
  538. }
  539. /// <summary>
  540. /// 获取一个长整形数据,不改变数据内容
  541. /// </summary>
  542. /// <returns></returns>
  543. public ulong GetUlong()
  544. {
  545. return GetUlong(readIndex);
  546. }
  547. /// <summary>
  548. /// 获取一个整形数据,不改变数据内容
  549. /// </summary>
  550. /// <param name="index">字节索引</param>
  551. /// <returns></returns>
  552. public int GetInt(int index)
  553. {
  554. return BitConverter.ToInt32(Get(index, 4), 0);
  555. }
  556. /// <summary>
  557. /// 获取一个整形数据,不改变数据内容
  558. /// </summary>
  559. /// <returns></returns>
  560. public int GetInt()
  561. {
  562. return GetInt(readIndex);
  563. }
  564. /// <summary>
  565. /// 获取一个整形数据,不改变数据内容
  566. /// </summary>
  567. /// <param name="index">字节索引</param>
  568. /// <returns></returns>
  569. public uint GetUint(int index)
  570. {
  571. return BitConverter.ToUInt32(Get(index, 4), 0);
  572. }
  573. /// <summary>
  574. /// 获取一个整形数据,不改变数据内容
  575. /// </summary>
  576. /// <returns></returns>
  577. public uint GetUint()
  578. {
  579. return GetUint(readIndex);
  580. }
  581. /// <summary>
  582. /// 获取一个短整形数据,不改变数据内容
  583. /// </summary>
  584. /// <param name="index">字节索引</param>
  585. /// <returns></returns>
  586. public int GetShort(int index)
  587. {
  588. return BitConverter.ToInt16(Get(index, 2), 0);
  589. }
  590. /// <summary>
  591. /// 获取一个短整形数据,不改变数据内容
  592. /// </summary>
  593. /// <returns></returns>
  594. public int GetShort()
  595. {
  596. return GetShort(readIndex);
  597. }
  598. /// <summary>
  599. /// 获取一个短整形数据,不改变数据内容
  600. /// </summary>
  601. /// <param name="index">字节索引</param>
  602. /// <returns></returns>
  603. public int GetUshort(int index)
  604. {
  605. return BitConverter.ToUInt16(Get(index, 2), 0);
  606. }
  607. /// <summary>
  608. /// 获取一个短整形数据,不改变数据内容
  609. /// </summary>
  610. /// <returns></returns>
  611. public int GetUshort()
  612. {
  613. return GetUshort(readIndex);
  614. }
  615. /// <summary>
  616. /// 获取一个char数据,不改变数据内容
  617. /// </summary>
  618. /// <param name="index">字节索引</param>
  619. /// <returns></returns>
  620. public char GetChar(int index)
  621. {
  622. return BitConverter.ToChar(Get(index, 2), 0);
  623. }
  624. /// <summary>
  625. /// 获取一个char数据,不改变数据内容
  626. /// </summary>
  627. /// <returns></returns>
  628. public char GetChar()
  629. {
  630. return GetChar(readIndex);
  631. }
  632. /// <summary>
  633. /// 获取一个布尔数据,不改变数据内容
  634. /// </summary>
  635. /// <param name="index">字节索引</param>
  636. /// <returns></returns>
  637. public bool GetBoolean(int index)
  638. {
  639. return BitConverter.ToBoolean(Get(index, 1), 0);
  640. }
  641. /// <summary>
  642. /// 获取一个布尔数据,不改变数据内容
  643. /// </summary>
  644. /// <returns></returns>
  645. public bool GetBoolean()
  646. {
  647. return GetBoolean(readIndex);
  648. }
  649. /// <summary>
  650. /// 清除已读字节并重建缓存区
  651. /// </summary>
  652. public void DiscardReadBytes()
  653. {
  654. if (readIndex <= 0) return;
  655. int len = buf.Length - readIndex;
  656. byte[] newbuf = new byte[len];
  657. Array.Copy(buf, readIndex, newbuf, 0, len);
  658. buf = newbuf;
  659. writeIndex -= readIndex;
  660. markReadIndex -= readIndex;
  661. if (markReadIndex < 0)
  662. {
  663. //markReadIndex = readIndex;
  664. markReadIndex = 0;
  665. }
  666. markWirteIndex -= readIndex;
  667. if (markWirteIndex < 0 || markWirteIndex < readIndex || markWirteIndex < markReadIndex)
  668. {
  669. markWirteIndex = writeIndex;
  670. }
  671. readIndex = 0;
  672. }
  673. /// <summary>
  674. /// 设置/获取读指针位置
  675. /// </summary>
  676. public int ReaderIndex
  677. {
  678. get
  679. {
  680. return readIndex;
  681. }
  682. set
  683. {
  684. if (value < 0) return;
  685. readIndex = value;
  686. }
  687. }
  688. /// <summary>
  689. /// 设置/获取写指针位置
  690. /// </summary>
  691. public int WriterIndex
  692. {
  693. get
  694. {
  695. return writeIndex;
  696. }
  697. set
  698. {
  699. if (value < 0) return;
  700. writeIndex = value;
  701. }
  702. }
  703. /// <summary>
  704. /// 标记读取的索引位置
  705. /// </summary>
  706. public void MarkReaderIndex()
  707. {
  708. markReadIndex = readIndex;
  709. }
  710. /// <summary>
  711. /// 标记写入的索引位置
  712. /// </summary>
  713. public void MarkWriterIndex()
  714. {
  715. markWirteIndex = writeIndex;
  716. }
  717. /// <summary>
  718. /// 将读取的索引位置重置为标记的读取索引位置
  719. /// </summary>
  720. public void ResetReaderIndex()
  721. {
  722. readIndex = markReadIndex;
  723. }
  724. /// <summary>
  725. /// 将写入的索引位置重置为标记的写入索引位置
  726. /// </summary>
  727. public void ResetWriterIndex()
  728. {
  729. writeIndex = markWirteIndex;
  730. }
  731. /// <summary>
  732. /// 可读的有效字节数
  733. /// </summary>
  734. /// <returns>可读的字节数</returns>
  735. public int ReadableBytes
  736. {
  737. get
  738. {
  739. return writeIndex - readIndex;
  740. }
  741. }
  742. /// <summary>
  743. /// 获取缓存区容量大小
  744. /// </summary>
  745. /// <returns>缓存区容量</returns>
  746. public int Capacity
  747. {
  748. get
  749. {
  750. return this.capacity;
  751. }
  752. }
  753. /// <summary>
  754. /// 获取可读的字节数组
  755. /// </summary>
  756. /// <returns>字节数据</returns>
  757. public byte[] ToArray()
  758. {
  759. byte[] bytes = new byte[writeIndex];
  760. Array.Copy(buf, 0, bytes, 0, bytes.Length);
  761. return bytes;
  762. }
  763. //是的,提高性能,就酱
  764. public byte[] GetBuffer()
  765. {
  766. return buf;
  767. }
  768. /// <summary>
  769. /// 复制一个对象,具有与原对象相同的数据,不改变原对象的数据,不包括已读数据
  770. /// </summary>
  771. /// <returns></returns>
  772. public ByteBuffer Copy()
  773. {
  774. if (buf == null)
  775. {
  776. return new ByteBuffer(16);
  777. }
  778. if (readIndex < writeIndex)
  779. {
  780. byte[] newbytes = new byte[writeIndex - readIndex];
  781. Array.Copy(buf, readIndex, newbytes, 0, newbytes.Length);
  782. ByteBuffer buffer = new ByteBuffer(newbytes.Length);
  783. buffer.WriteBytes(newbytes);
  784. buffer.isPool = this.isPool;
  785. return buffer;
  786. }
  787. return new ByteBuffer(16);
  788. }
  789. /// <summary>
  790. /// 深度复制,具有与原对象相同的数据,不改变原对象的数据,包括已读数据
  791. /// </summary>
  792. /// <returns></returns>
  793. public ByteBuffer Clone()
  794. {
  795. if (buf == null)
  796. {
  797. return new ByteBuffer(16);
  798. }
  799. ByteBuffer newBuf = new ByteBuffer(buf)
  800. {
  801. capacity = this.capacity,
  802. readIndex = this.readIndex,
  803. writeIndex = this.writeIndex,
  804. markReadIndex = this.markReadIndex,
  805. markWirteIndex = this.markWirteIndex,
  806. isPool = this.isPool
  807. };
  808. return newBuf;
  809. }
  810. /// <summary>
  811. /// 遍历所有的字节数据
  812. /// </summary>
  813. /// <param name="action"></param>
  814. public void ForEach(Action<byte> action)
  815. {
  816. for(int i = 0; i < this.ReadableBytes; i++)
  817. {
  818. action.Invoke(this.buf[i]);
  819. }
  820. }
  821. /// <summary>
  822. /// 清空此对象,但保留字节缓存数组(空数组)
  823. /// </summary>
  824. public void Clear()
  825. {
  826. //buf = new byte[buf.Length];
  827. for(int i = 0; i < buf.Length; i++)
  828. {
  829. buf[i] = 0;
  830. }
  831. readIndex = 0;
  832. writeIndex = 0;
  833. markReadIndex = 0;
  834. markWirteIndex = 0;
  835. capacity = buf.Length;
  836. }
  837. /// <summary>
  838. /// 释放对象,清除字节缓存数组,如果此对象为可池化,那么调用此方法将会把此对象推入到池中等待下次调用
  839. /// </summary>
  840. public void Dispose()
  841. {
  842. if (isPool)
  843. {
  844. lock (pool)
  845. {
  846. if (pool.Count < poolMaxCount)
  847. {
  848. this.Clear();
  849. pool.Add(this);
  850. } else
  851. {
  852. readIndex = 0;
  853. writeIndex = 0;
  854. markReadIndex = 0;
  855. markWirteIndex = 0;
  856. capacity = 0;
  857. buf = null;
  858. }
  859. }
  860. }
  861. else
  862. {
  863. readIndex = 0;
  864. writeIndex = 0;
  865. markReadIndex = 0;
  866. markWirteIndex = 0;
  867. capacity = 0;
  868. buf = null;
  869. }
  870. }
  871. }