YBytes.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package serialporttest.utils;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * byte拼接类
  6. *
  7. */
  8. @SuppressWarnings("unused")
  9. public class YBytes {
  10. private byte[] bytes;
  11. /**
  12. * 构造函数,创建一个长度为0的byte数组
  13. */
  14. public YBytes() {
  15. bytes = new byte[0];
  16. }
  17. /**
  18. * 构造函数,创建一个长度为i的byte数组
  19. *
  20. * @param i byte[]长度
  21. */
  22. public YBytes(int i) {
  23. bytes = new byte[i];
  24. }
  25. /**
  26. * 构造函数创建一个初始的byte数组
  27. *
  28. * @param b 初始数组
  29. */
  30. public YBytes(byte[] b) {
  31. this.bytes = new byte[b.length];
  32. System.arraycopy(b, 0, this.bytes, 0, b.length);
  33. }
  34. /**
  35. * 在byte数组末尾添加一个byte
  36. *
  37. * @param b b
  38. * @return YBytes
  39. */
  40. public YBytes addByte(byte b) {
  41. byte[] temp = new byte[bytes.length + 1];
  42. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  43. temp[temp.length - 1] = b;
  44. bytes = temp;
  45. return this;
  46. }
  47. /**
  48. * 在byte数组末尾添加一个byte[]
  49. *
  50. * @param bs bs
  51. * @return YBytes
  52. */
  53. public YBytes addByte(byte[] bs) {
  54. byte[] temp = new byte[bytes.length + bs.length];
  55. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  56. System.arraycopy(bs, 0, temp, bytes.length, bs.length);
  57. bytes = temp;
  58. return this;
  59. }
  60. /**
  61. * 在byte数组末尾添加一个byte[],给定添加的长度
  62. *
  63. * @param bs 添加的数组
  64. * @param length 添加的长度
  65. * @return Bytes
  66. */
  67. public YBytes addByte(byte[] bs, int length) {
  68. byte[] temp = new byte[bytes.length + length];
  69. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  70. System.arraycopy(bs, 0, temp, bytes.length, length);
  71. bytes = temp;
  72. return this;
  73. }
  74. /**
  75. * 在byte数组末尾添加一个byte[],给定添加的长度
  76. *
  77. * @param bs 添加的数组
  78. * @param start 开始位置
  79. * @param length 添加的长度
  80. * @return Bytes
  81. */
  82. public YBytes addByte(byte[] bs, int start, int length) {
  83. byte[] temp = new byte[bytes.length + length];
  84. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  85. System.arraycopy(bs, start, temp, bytes.length, length);
  86. bytes = temp;
  87. return this;
  88. }
  89. /**
  90. * 在byte数组末尾添加一组Byte
  91. *
  92. * @param bs bs
  93. * @return YBytes
  94. */
  95. public YBytes addByte(List<Byte> bs) {
  96. byte[] temp = new byte[bytes.length + bs.size()];
  97. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  98. for (int i = 0; i < bs.size(); i++) {
  99. temp[bytes.length + i] = bs.get(i);
  100. }
  101. bytes = temp;
  102. return this;
  103. }
  104. /**
  105. * 在byte数组末尾添加一个List
  106. *
  107. * @param bs 添加的集合
  108. * @param length 添加的长度
  109. * @return Bytes
  110. */
  111. public YBytes addByte(List<Byte> bs, int length) {
  112. byte[] temp = new byte[bytes.length + length];
  113. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  114. for (int i = 0; i < length; i++) {
  115. temp[bytes.length + i] = bs.get(i);
  116. }
  117. bytes = temp;
  118. return this;
  119. }
  120. /**
  121. * 在byte数组末尾添加一个List
  122. *
  123. * @param bs 添加的集合
  124. * @param start 开始位置
  125. * @param length 添加的长度
  126. * @return Bytes
  127. */
  128. public YBytes addByte(List<Byte> bs, int start, int length) {
  129. byte[] temp = new byte[bytes.length + length];
  130. System.arraycopy(bytes, 0, temp, 0, bytes.length);
  131. for (int i = 0; i < length; i++) {
  132. temp[bytes.length + i] = bs.get(i + start);
  133. }
  134. bytes = temp;
  135. return this;
  136. }
  137. /**
  138. * 修改byte数组中一位的值为byte
  139. *
  140. * @param b 数据
  141. * @param index 位置
  142. * @return YBytes
  143. */
  144. public YBytes changeByte(byte b, int index) {
  145. if (index >= 0 && index < bytes.length) {
  146. bytes[index] = b;
  147. }
  148. return this;
  149. }
  150. /**
  151. * 修改byte数组中第index位起值为b
  152. *
  153. * @param b 数据
  154. * @param index 位置
  155. * @return YBytes
  156. */
  157. public YBytes changeByte(byte[] b, int index) {
  158. return changeByte(b, index, index + b.length);
  159. }
  160. /**
  161. * 修改byte数组中第start位起值为b,连续修改length位
  162. *
  163. * @param b 数据
  164. * @param start 起始位置
  165. * @param length 结束位置
  166. * @return YBytes
  167. */
  168. public YBytes changeByte(byte[] b, int start, int length) {
  169. if (start >= 0 && length > 0) {
  170. for (int i = 0; i < length; i++) {
  171. if (start + i < bytes.length) {
  172. bytes[start + i] = b[i];
  173. }
  174. }
  175. }
  176. return this;
  177. }
  178. /**
  179. * 修改byte数组中第index位起值为b
  180. *
  181. * @param b 数据
  182. * @param index 位置
  183. * @return YBytes
  184. */
  185. public YBytes changeByte(List<Byte> b, int index) {
  186. return changeByte(b, index, b.size());
  187. }
  188. /**
  189. * 修改byte数组中第start位起值为b,连续修改length位
  190. *
  191. * @param b 数据
  192. * @param start 起始位置
  193. * @param length 结束位置
  194. * @return YBytes
  195. */
  196. public YBytes changeByte(List<Byte> b, int start, int length) {
  197. if (start >= 0 && length > 0) {
  198. for (int i = 0; i < length; i++) {
  199. if (start + i < bytes.length) {
  200. bytes[start + i] = b.get(i);
  201. }
  202. }
  203. }
  204. return this;
  205. }
  206. /**
  207. * 替换bytes数组
  208. *
  209. * @param bytes bytes
  210. */
  211. public void setBytes(byte[] bytes) {
  212. this.bytes = bytes;
  213. }
  214. /**
  215. * 拆分byte数组为多个byte数组
  216. *
  217. * @param bytes 需要拆分的数组
  218. * @param length 每组最大长度
  219. * @return 最终拆分的数据
  220. */
  221. public static List<byte[]> split(byte[] bytes, int length) {
  222. List<byte[]> list = new ArrayList<>();
  223. int count = 0;//统计已经发送长度
  224. while (true) {
  225. //剩余长度
  226. int sy = bytes.length - count;
  227. //如果剩余长度小于等于0,说明发送完成
  228. if (sy <= 0) break;
  229. //如果剩余长度大于每次写入长度,就写入对应长度,如果不大于就写入剩余长度
  230. byte[] current = new byte[Math.min(sy, length)];
  231. //数组copy
  232. System.arraycopy(bytes, count, current, 0, current.length);
  233. //写入
  234. list.add(current);
  235. //统计已经发送长度
  236. count += current.length;
  237. }
  238. return list;
  239. }
  240. /**
  241. * 拆分byte数组为多个byte数组
  242. *
  243. * @param length 每组最大长度
  244. * @return 最终拆分的数据
  245. */
  246. public List<byte[]> split(int length) {
  247. return split(bytes, length);
  248. }
  249. /**
  250. * 获取bytes数组
  251. *
  252. * @return byte[]
  253. */
  254. public byte[] getBytes() {
  255. return bytes;
  256. }
  257. }