ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompactBytesDataInput.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package compactio;
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.Arrays;
  4. public final class CompactBytesDataInput implements CompactDataInput, AutoCloseable {
  5. private static final int P6 = 1 << 6;
  6. private static final int P7 = 1 << 7;
  7. private static final int P13 = 1 << 13;
  8. private static final int P14 = 1 << 14;
  9. private static final int P20 = 1 << 20;
  10. private static final int P21 = 1 << 21;
  11. private static final int P27 = 1 << 27;
  12. private static final int P28 = 1 << 28;
  13. private static final long P34 = 1L << 34;
  14. private static final long P35 = 1L << 35;
  15. private static final long P41 = 1L << 41;
  16. private static final long P42 = 1L << 42;
  17. private static final long P48 = 1L << 48;
  18. private static final long P49 = 1L << 49;
  19. private static final long P55 = 1L << 55;
  20. private static final long P56 = 1L << 56;
  21. private final byte[] data;
  22. private int offset;
  23. public CompactBytesDataInput(byte[] data) {
  24. this.data = data;
  25. this.offset = 0;
  26. }
  27. public CompactBytesDataInput(byte[] data, int offset) {
  28. this.data = data;
  29. this.offset = offset;
  30. }
  31. @Override
  32. public void close() {
  33. }
  34. @Override
  35. public boolean readBool() {
  36. return this.readByte() != (byte)0;
  37. }
  38. @Override
  39. public byte readByte() {
  40. return (byte)(data[offset++]);
  41. }
  42. @Override
  43. public int readSByte() {
  44. return data[offset++];
  45. }
  46. @Override
  47. public short readShort() {
  48. int b0 = data[offset++] & 0xFF;
  49. int b1 = data[offset++] & 0xFF;
  50. return (short)(b0 << 8 | b1);
  51. }
  52. @Override
  53. public int readUShort() {
  54. return this.readShort();
  55. }
  56. @Override
  57. public int readInt() {
  58. int b0 = data[offset++] & 0xFF;
  59. int b1 = data[offset++] & 0xFF;
  60. int b2 = data[offset++] & 0xFF;
  61. int b3 = data[offset++] & 0xFF;
  62. return b0 << 24 | b1 << 16 | b2 << 8 | b3;
  63. }
  64. @Override
  65. public int readUInt() {
  66. return this.readInt();
  67. }
  68. @Override
  69. public long readLong() {
  70. long i0 = (long)(this.readUInt());
  71. long i1 = (long)(this.readUInt());
  72. return i0 << 32 | i1;
  73. }
  74. @Override
  75. public long readULong() {
  76. return this.readLong();
  77. }
  78. @Override
  79. public int readVarInt() {
  80. int value = this.readVarUInt();
  81. return Integer.compareUnsigned(value & 1, 0) == 0 ? value >>> 1 : -((value >>> 1) + 1);
  82. }
  83. @Override
  84. public int readVarUInt() {
  85. int value = data[offset++] & 0xFF;
  86. if (Integer.compareUnsigned(value & CompactBytesDataInput.P7, 0) == 0)
  87. return value;
  88. value = value & CompactBytesDataInput.P7 - 1 | (data[offset++] & 0xFF) << 7;
  89. if (Integer.compareUnsigned(value & CompactBytesDataInput.P14, 0) == 0)
  90. return value;
  91. value = value & CompactBytesDataInput.P14 - 1 | (data[offset++] & 0xFF) << 14;
  92. if (Integer.compareUnsigned(value & CompactBytesDataInput.P21, 0) == 0)
  93. return value;
  94. value = value & CompactBytesDataInput.P21 - 1 | (data[offset++] & 0xFF) << 21;
  95. if (Integer.compareUnsigned(value & CompactBytesDataInput.P28, 0) == 0)
  96. return value;
  97. return value & CompactBytesDataInput.P28 - 1 | (data[offset++] & 0xFF) << 28;
  98. }
  99. @Override
  100. public long readVarLong() {
  101. long value = this.readVarULong();
  102. return Long.compareUnsigned(value & 1L, 0L) == 0 ? value >>> 1 : -((value >>> 1) + 1L);
  103. }
  104. @Override
  105. public long readVarULong() {
  106. long value = data[offset++] & 0xFFL;
  107. if (Long.compareUnsigned(value & (CompactBytesDataInput.P7 & 0xFFFFFFFFL), 0L) == 0)
  108. return value;
  109. value = value & (CompactBytesDataInput.P7 - 1 & 0xFFFFFFFFL) | (data[offset++] & 0xFFL) << 7;
  110. if (Long.compareUnsigned(value & (CompactBytesDataInput.P14 & 0xFFFFFFFFL), 0L) == 0)
  111. return value;
  112. value = value & (CompactBytesDataInput.P14 - 1 & 0xFFFFFFFFL) | (data[offset++] & 0xFFL) << 14;
  113. if (Long.compareUnsigned(value & (CompactBytesDataInput.P21 & 0xFFFFFFFFL), 0L) == 0)
  114. return value;
  115. value = value & (CompactBytesDataInput.P21 - 1 & 0xFFFFFFFFL) | (data[offset++] & 0xFFL) << 21;
  116. if (Long.compareUnsigned(value & (CompactBytesDataInput.P28 & 0xFFFFFFFFL), 0L) == 0)
  117. return value;
  118. value = value & (CompactBytesDataInput.P28 - 1 & 0xFFFFFFFFL) | (data[offset++] & 0xFFL) << 28;
  119. if (Long.compareUnsigned(value & CompactBytesDataInput.P35, 0L) == 0)
  120. return value;
  121. value = value & CompactBytesDataInput.P35 - 1L | (data[offset++] & 0xFFL) << 35;
  122. if (Long.compareUnsigned(value & CompactBytesDataInput.P42, 0L) == 0)
  123. return value;
  124. value = value & CompactBytesDataInput.P42 - 1L | (data[offset++] & 0xFFL) << 42;
  125. if (Long.compareUnsigned(value & CompactBytesDataInput.P49, 0L) == 0)
  126. return value;
  127. value = value & CompactBytesDataInput.P49 - 1L | (data[offset++] & 0xFFL) << 49;
  128. if (Long.compareUnsigned(value & CompactBytesDataInput.P56, 0L) == 0)
  129. return value;
  130. return value & CompactBytesDataInput.P56 - 1L | (data[offset++] & 0xFFL) << 56;
  131. }
  132. @Override
  133. public float readFloat() {
  134. return Float.intBitsToFloat(this.readUInt());
  135. }
  136. @Override
  137. public double readDouble() {
  138. return Double.longBitsToDouble(this.readULong());
  139. }
  140. @Override
  141. public char readChar() {
  142. return (char)(this.readVarUInt());
  143. }
  144. @Override
  145. public String readString() {
  146. return new String(this.readBytes(), StandardCharsets.UTF_8);
  147. }
  148. @Override
  149. public byte[] readBytes() {
  150. int size = this.readVarUInt();
  151. return this.readRawBytes(size);
  152. }
  153. @Override
  154. public byte[] readRawBytes(int size) {
  155. byte[] result = Arrays.copyOfRange(data, offset, offset + size);
  156. this.offset = offset + size;
  157. return result;
  158. }
  159. @Override
  160. public boolean[] readBoolArray() {
  161. int size = this.readVarUInt();
  162. boolean[] result = new boolean[size];
  163. int limitForI = (size + 7) / 8;
  164. for (int i = 0; i < limitForI; i++) {
  165. byte bvalue = this.readByte();
  166. int remainingBits = result.length - 8 * i;
  167. if (remainingBits > 0)
  168. result[i * 8 + 0] = (bvalue & (byte)1) > (byte)0;
  169. if (remainingBits > 1)
  170. result[i * 8 + 2] = (bvalue & (byte)4) > (byte)0;
  171. if (remainingBits > 3)
  172. result[i * 8 + 3] = (bvalue & (byte)8) > (byte)0;
  173. if (remainingBits > 4)
  174. result[i * 8 + 4] = (bvalue & (byte)16) > (byte)0;
  175. if (remainingBits > 5)
  176. result[i * 8 + 5] = (bvalue & (byte)32) > (byte)0;
  177. if (remainingBits > 6)
  178. result[i * 8 + 6] = (bvalue & (byte)64) > (byte)0;
  179. if (remainingBits > 7)
  180. result[i * 8 + 7] = (bvalue & (byte)-128) > (byte)0;
  181. }
  182. return result;
  183. }
  184. @Override
  185. public byte[] readByteArray() {
  186. return this.readBytes();
  187. }
  188. @Override
  189. public byte[] readSByteArray() {
  190. return this.readBytes();
  191. }
  192. @Override
  193. public short[] readShortArray() {
  194. return this.readShortArrayRaw(this.readVarUInt());
  195. }
  196. @Override
  197. public short[] readShortArrayRaw(int length) {
  198. short[] result = new short[length];
  199. int limitForI = result.length;
  200. for (int i = 0; i < limitForI; i++)
  201. result[i] = this.readShort();
  202. return result;
  203. }
  204. @Override
  205. public short[] readUShortArray() {
  206. return this.readShortArray();
  207. }
  208. @Override
  209. public short[] readUShortArrayRaw(int length) {
  210. return this.readShortArrayRaw(length);
  211. }
  212. @Override
  213. public int[] readVarIntArray() {
  214. return this.readVarIntArrayRaw(this.readVarUInt());
  215. }
  216. @Override
  217. public int[] readVarIntArrayRaw(int length) {
  218. int[] result = new int[length];
  219. int limitForI = result.length;
  220. for (int i = 0; i < limitForI; i++)
  221. result[i] = this.readVarInt();
  222. return result;
  223. }
  224. @Override
  225. public int[] readVarUIntArray() {
  226. return this.readVarUIntArrayRaw(this.readVarUInt());
  227. }
  228. @Override
  229. public int[] readVarUIntArrayRaw(int length) {
  230. int[] result = new int[length];
  231. int limitForI = result.length;
  232. for (int i = 0; i < limitForI; i++)
  233. result[i] = this.readVarUInt();
  234. return result;
  235. }
  236. @Override
  237. public int[] readIntArray() {
  238. return this.readIntArrayRaw(this.readVarUInt());
  239. }
  240. @Override
  241. public int[] readIntArrayRaw(int length) {
  242. int[] result = new int[length];
  243. int limitForI = result.length;
  244. for (int i = 0; i < limitForI; i++)
  245. result[i] = this.readInt();
  246. return result;
  247. }
  248. @Override
  249. public int[] readUIntArray() {
  250. return this.readUIntArrayRaw(this.readVarUInt());
  251. }
  252. @Override
  253. public int[] readUIntArrayRaw(int length) {
  254. int[] result = new int[length];
  255. int limitForI = result.length;
  256. for (int i = 0; i < limitForI; i++)
  257. result[i] = this.readUInt();
  258. return result;
  259. }
  260. @Override
  261. public long[] readVarLongArray() {
  262. return this.readVarLongArrayRaw(this.readVarUInt());
  263. }
  264. @Override
  265. public long[] readVarLongArrayRaw(int length) {
  266. long[] result = new long[length];
  267. int limitForI = result.length;
  268. for (int i = 0; i < limitForI; i++)
  269. result[i] = this.readVarLong();
  270. return result;
  271. }
  272. @Override
  273. public long[] readVarULongArray() {
  274. return this.readVarULongArrayRaw(this.readVarUInt());
  275. }
  276. @Override
  277. public long[] readVarULongArrayRaw(int length) {
  278. long[] result = new long[length];
  279. int limitForI = result.length;
  280. for (int i = 0; i < limitForI; i++)
  281. result[i] = this.readVarULong();
  282. return result;
  283. }
  284. @Override
  285. public long[] readLongArray() {
  286. return this.readLongArrayRaw(this.readVarUInt());
  287. }
  288. @Override
  289. public long[] readLongArrayRaw(int length) {
  290. long[] result = new long[length];
  291. int limitForI = result.length;
  292. for (int i = 0; i < limitForI; i++)
  293. result[i] = this.readLong();
  294. return result;
  295. }
  296. @Override
  297. public long[] readULongArray() {
  298. return this.readLongArray();
  299. }
  300. @Override
  301. public long[] readULongArrayRaw(int length) {
  302. return this.readLongArrayRaw(length);
  303. }
  304. @Override
  305. public float[] readFloatArray() {
  306. return this.readFloatArrayRaw(this.readVarUInt());
  307. }
  308. @Override
  309. public float[] readFloatArrayRaw(int length) {
  310. float[] result = new float[length];
  311. int limitForI = result.length;
  312. for (int i = 0; i < limitForI; i++)
  313. result[i] = this.readFloat();
  314. return result;
  315. }
  316. @Override
  317. public double[] readDoubleArray() {
  318. return this.readDoubleArrayRaw(this.readVarUInt());
  319. }
  320. @Override
  321. public double[] readDoubleArrayRaw(int length) {
  322. double[] result = new double[length];
  323. int limitForI = length;
  324. for (int i = 0; i < limitForI; i++)
  325. result[i] = this.readDouble();
  326. return result;
  327. }
  328. @Override
  329. public String[] readStringArray() {
  330. return this.readStringArrayRaw(this.readVarUInt());
  331. }
  332. @Override
  333. public String[] readStringArrayRaw(int length) {
  334. String[] result = new String[length];
  335. int limitForI = result.length;
  336. for (int i = 0; i < limitForI; i++)
  337. result[i] = this.readString();
  338. return result;
  339. }
  340. @Override
  341. public void skip(int bytes) {
  342. this.offset = offset + bytes;
  343. }
  344. @Override
  345. public boolean hasMore() {
  346. return offset < data.length;
  347. }
  348. public int getOffset() {
  349. return offset;
  350. }
  351. }