Guest User

Untitled

a guest
Nov 11th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. package org.solace.network.packet;
  2.  
  3. import java.nio.ByteBuffer;
  4.  
  5. import net.burtlebutle.bob.rand.isaac.ISAAC;
  6.  
  7. /**
  8. * Data packet builder.
  9. *
  10. * Helper class to build outgoing {@link Packet}s under RuneScape protocol
  11. * standards.
  12. *
  13. * @author kLeptO <http://www.rune-server.org/members/klepto/>
  14. */
  15. public class PacketBuilder extends Packet {
  16.  
  17. private int[] frameSizes = new int[10];
  18. private int frameSizePointer = 0;
  19.  
  20. private int bitPosition;
  21. private boolean bitAccess = false;
  22.  
  23. /**
  24. * Writes a boolean bit flag.
  25. *
  26. * @param flag
  27. * the flag
  28. */
  29. public PacketBuilder putBit(boolean flag) {
  30. putBits(1, flag ? 1 : 0);
  31. return this;
  32. }
  33.  
  34. /**
  35. * Appends the bytes buffer to this packet.
  36. *
  37. * @param buffer
  38. * the bytes buffer
  39. */
  40. public PacketBuilder put(ByteBuffer buffer) {
  41. buffer().put((ByteBuffer) buffer.flip());
  42. return this;
  43. }
  44.  
  45. /**
  46. * Appends the bytes array to this packet.
  47. *
  48. * @param buffer
  49. * the bytes array
  50. */
  51. public PacketBuilder put(byte[] buffer) {
  52. for (int i = 0; i < buffer.length; i++) {
  53. buffer().put(buffer[i]);
  54. }
  55. return this;
  56. }
  57.  
  58. /**
  59. * Puts the signed byte to the buffer.
  60. *
  61. * @param value
  62. * the signed byte
  63. */
  64. public PacketBuilder putByte(int value) {
  65. buffer().put((byte) value);
  66. return this;
  67. }
  68.  
  69. /**
  70. * Puts the signed byte with addition.
  71. *
  72. * @param value
  73. * the signed byte
  74. */
  75. public PacketBuilder putByteA(int value) {
  76. putByte(value + 128);
  77. return this;
  78. }
  79.  
  80. /**
  81. * Puts the signed byte as subtrahend.
  82. *
  83. * @param value
  84. * the signed byte
  85. */
  86. public PacketBuilder putByteS(int value) {
  87. putByte(128 - value);
  88. return this;
  89. }
  90.  
  91. /**
  92. * Puts the negated signed byte.
  93. *
  94. * @param value
  95. * the signed byte
  96. */
  97. public PacketBuilder putByteC(int value) {
  98. putByte(-value);
  99. return this;
  100. }
  101.  
  102.  
  103. /**
  104. * Puts the signed short.
  105. *
  106. * @param value
  107. * the signed short
  108. */
  109. public PacketBuilder putShort(int value) {
  110. buffer().putShort((short) value);
  111. return this;
  112. }
  113.  
  114. /**
  115. * Puts the big-endian short with addition.
  116. *
  117. * @param value
  118. * the signed short
  119. */
  120. public PacketBuilder putShortA(int value) {
  121. putByte(value >> 8).putByte(value + 128);
  122. return this;
  123. }
  124.  
  125. public PacketBuilder putBigA(int value) {
  126. putByte(value >> 8).putByte(value);
  127. return this;
  128. }
  129.  
  130. /**
  131. * Puts the signed little-endian short.
  132. *
  133. * @param value
  134. * the signed short
  135. */
  136. public PacketBuilder putLEShort(int value) {
  137. putByte(value).putByte(value >> 8);
  138. return this;
  139. }
  140.  
  141. /**
  142. * Puts the signed little-endian short with addition.
  143. *
  144. * @param value
  145. * the signed short
  146. */
  147. public PacketBuilder putLEShortA(int value) {
  148. putByte(value + 128).putByte(value >> 8);
  149. return this;
  150. }
  151.  
  152. /**
  153. * Puts the mixed-endian small integer.
  154. *
  155. * @param value
  156. * the signed integer
  157. */
  158. public PacketBuilder putInt1(int value) {
  159. putByte((byte) (value >> 8)).putByte((byte) value)
  160. .putByte((byte) (value >> 24)).putByte((byte) (value >> 16));
  161. return this;
  162. }
  163.  
  164. public PacketBuilder putInt(int value) {
  165. putByte((byte) (value >> 24)).putByte((byte) value >> 16)
  166. .putByte((byte) (value >> 8)).putByte((byte) (value));
  167. return this;
  168. }
  169.  
  170. public PacketBuilder putLEInt(int value) {
  171. putByte((byte) (value)).putByte((byte) value >> 8)
  172. .putByte((byte) (value >> 16)).putByte((byte) (value >> 24));
  173. return this;
  174. }
  175.  
  176. /**
  177. * Puts an integer value
  178. *
  179. * @param value
  180. * The signed integer
  181. * @return The packet builder
  182. */
  183. public PacketBuilder putInt2(int value) {
  184. putByte(value >> 16).putByte(value >> 24).putByte(value)
  185. .putByte(value >> 8);
  186. return this;
  187. }
  188.  
  189. public PacketBuilder putIntTest(int value) {
  190. putByte(value >> 8).putByte(value >> 16).putByte(value)
  191. .putByte(value >> 24);
  192. return this;
  193. }
  194.  
  195. public PacketBuilder putByte3(int value) {
  196. putByte((byte) value).putByte((byte) value >> 8).putByte(
  197. (byte) value >> 16);
  198. return this;
  199. }
  200.  
  201. /**
  202. * Puts the given amount of bytes with the same specified value.
  203. *
  204. * @param value
  205. * the value of the bytes
  206. *
  207. * @param amount
  208. * the amount of bytes
  209. */
  210. public PacketBuilder putBytes(int value, int amount) {
  211. for (; amount > 0; amount--) {
  212. putByte(value);
  213. }
  214. return this;
  215. }
  216.  
  217. /**
  218. * Writes the bytes into this buffer.
  219. *
  220. * @param from
  221. */
  222. public void writeBytes(byte[] from, int size) {
  223. buffer().put(from, 0, size);
  224. }
  225.  
  226. /**
  227. * Puts the signed long to the buffer.
  228. *
  229. * @param value
  230. * the signed long
  231. */
  232. public PacketBuilder putLong(long value) {
  233. buffer().putLong(value);
  234. return this;
  235. }
  236.  
  237. /**
  238. * Puts the string to the buffer.
  239. *
  240. * @param value
  241. * the string
  242. */
  243. public PacketBuilder putString(String value) {
  244. put(value.getBytes()).putByte(10);
  245. return this;
  246. }
  247.  
  248. public PacketBuilder putString2(String value) {
  249. for (byte values : value.getBytes()) {
  250. putByte(values);
  251. }
  252. putByte(10);
  253. return this;
  254. }
  255.  
  256. /**
  257. * Creates packet frame with given opcode.
  258. *
  259. * @param opcode
  260. * the packet opcode
  261. *
  262. * @param encryption
  263. * the ISAAC encryption
  264. */
  265. public PacketBuilder createFrame(int opcode, ISAAC encryption) {
  266. putByte(opcode + encryption.getNextKey()).opcode(opcode);
  267. return this;
  268. }
  269.  
  270. /**
  271. * Creates sized packet frame with specified opcode.
  272. *
  273. * @param opcode
  274. * the packet opcode
  275. *
  276. * @param encryption
  277. * the ISAAC encryption
  278. */
  279. public PacketBuilder createSizedFrame(int opcode, ISAAC encryption) {
  280. createFrame(opcode, encryption).putByte(0);
  281. frameSizes[++frameSizePointer] = buffer().position();
  282. return this;
  283. }
  284.  
  285. /**
  286. * Creates short sized packet frame with specified opcode.
  287. *
  288. * @param opcode
  289. * the packet opcode
  290. *
  291. * @param encryption
  292. * the ISAAC encryption
  293. */
  294. public PacketBuilder createShortSizedFrame(int opcode, ISAAC encryption) {
  295. createFrame(opcode, encryption).putShort(0);
  296. frameSizes[++frameSizePointer] = buffer().position();
  297. return this;
  298. }
  299.  
  300. /**
  301. * Finishes sized packet frame.
  302. */
  303. public PacketBuilder finishSizedFrame() {
  304. int position = buffer().position();
  305. int length = position - frameSizes[frameSizePointer--];
  306. buffer().put(position - length - 1, (byte) length);
  307. return this;
  308. }
  309.  
  310. /**
  311. * Finishes short sized packet frame.
  312. */
  313. public PacketBuilder finishShortSizedFrame() {
  314. int position = buffer().position();
  315. int length = position - frameSizes[frameSizePointer--];
  316. buffer().put(position - length - 2, (byte) (length >> 8));
  317. buffer().put(position - length - 1, (byte) length);
  318. return this;
  319. }
  320.  
  321. /**
  322. * Initializes bit access to the buffer.
  323. */
  324. public PacketBuilder bitAccess() {
  325. if (!bitAccess) {
  326. bitPosition = buffer().position() * 8;
  327. bitAccess = true;
  328. }
  329. return this;
  330. }
  331.  
  332. /**
  333. * Initializes byte access to the buffer only if bit access is currently
  334. * active.
  335. */
  336. public PacketBuilder byteAccess() {
  337. if (bitAccess) {
  338. buffer().position((bitPosition + 7) / 8);
  339. bitAccess = false;
  340. }
  341. return this;
  342. }
  343.  
  344. /**
  345. * Puts boolean as a bit into byte buffer.
  346. *
  347. * @param amount
  348. * the number of bits
  349. *
  350. * @param value
  351. * the boolean value
  352. */
  353. public PacketBuilder putBits(int amount, boolean value) {
  354. putBits(amount, value ? 1 : 0);
  355. return this;
  356. }
  357.  
  358. /**
  359. * Puts bits into byte buffer.
  360. *
  361. * @param amount
  362. * the number of bits
  363. *
  364. * @param value
  365. * the bits value
  366. */
  367. public PacketBuilder putBits(int amount, int value) {
  368. int i = 0, bytePos = bitPosition >> 3;
  369. int bitOffset = 8 - (bitPosition & 7);
  370. bitPosition += amount;
  371. for (; amount > bitOffset; bitOffset = 8) {
  372. i = buffer().get(bytePos) & ~BIT_MASK[bitOffset];
  373. buffer().put(bytePos, (byte) i);
  374. i = buffer().get(bytePos) | (value >> (amount - bitOffset))
  375. & BIT_MASK[bitOffset];
  376. buffer().put(bytePos++, (byte) i);
  377. amount -= bitOffset;
  378. }
  379. if (amount == bitOffset) {
  380. i = buffer().get(bytePos) & ~BIT_MASK[bitOffset];
  381. buffer().put(bytePos, (byte) i);
  382. i = buffer().get(bytePos) | value & BIT_MASK[bitOffset];
  383. buffer().put(bytePos, (byte) i);
  384. } else {
  385. i = buffer().get(bytePos)
  386. & ~(BIT_MASK[amount] << (bitOffset - amount));
  387. buffer().put(bytePos, (byte) i);
  388. i = buffer().get(bytePos)
  389. | (value & BIT_MASK[amount]) << (bitOffset - amount);
  390. buffer().put(bytePos, (byte) i);
  391. }
  392. return this;
  393. }
  394.  
  395. /**
  396. * Allocates packet builder with given capacity.
  397. *
  398. * @param capacity
  399. * the packet capacity
  400. *
  401. * @return the newly allocated packet builder
  402. */
  403. public static PacketBuilder allocate(int capacity) {
  404. return (PacketBuilder) new PacketBuilder().buffer(ByteBuffer
  405. .allocateDirect(capacity));
  406. }
  407.  
  408. public static int BIT_MASK[] = new int[32];
  409.  
  410. /**
  411. * Initializes the bit masks.
  412. */
  413. static {
  414. for (int i = 0; i < 32; i++)
  415. BIT_MASK[i] = (1 << i) - 1;
  416. }
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment