ByteBuffer.cs 24 KB

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