Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. package network.packet;
  2.  
  3. import org.apache.mina.common.IoSession;
  4.  
  5. /**
  6. * Immutable packet object.
  7. *
  8. * @author Graham
  9. */
  10. public final class Packet {
  11.  
  12. public static enum Size {
  13. Fixed,
  14. VariableByte,
  15. VariableShort
  16. }
  17.  
  18.  
  19. ;
  20.  
  21. /**
  22. * The associated IO session
  23. */
  24. private IoSession session;
  25.  
  26. /**
  27. * The ID of the packet
  28. */
  29. private int pID;
  30.  
  31. /**
  32. * The length of the payload
  33. */
  34. private int pLength;
  35.  
  36. /**
  37. * The payload
  38. */
  39. private byte[] pData;
  40.  
  41. /**
  42. * The current index into the payload buffer for reading
  43. */
  44. private int caret = 0;
  45.  
  46. /**
  47. * Whether this packet is without the standard packet header
  48. */
  49. private boolean bare;
  50.  
  51. private Size size = Size.Fixed;
  52.  
  53. public Packet(IoSession session, int pID, byte[] pData, boolean bare, Size s) {
  54. this.session = session;
  55. this.pID = pID;
  56. this.pData = pData;
  57. this.pLength = pData.length;
  58. this.bare = bare;
  59. this.size = s;
  60. }
  61.  
  62. /**
  63. * Creates a new packet with the specified parameters.
  64. *
  65. * @param session The session to associate with the packet
  66. * @param pID The ID of the packet
  67. * @param pData The payload of the packet
  68. * @param bare Whether this packet is bare, which means that it does
  69. * not include the standard packet header
  70. */
  71. public Packet(IoSession session, int pID, byte[] pData, boolean bare) {
  72. this(session, pID, pData, bare, Size.Fixed);
  73. }
  74.  
  75. /**
  76. * Creates a new packet with the specified parameters. The packet
  77. * is considered not to be a bare packet.
  78. *
  79. * @param session The session to associate with the packet
  80. * @param pID The ID of the packet
  81. * @param pData The payload the packet
  82. */
  83. public Packet(IoSession session, int pID, byte[] pData) {
  84. this(session, pID, pData, false);
  85. }
  86.  
  87. /**
  88. * Returns the IO session associated with the packet, if any.
  89. *
  90. * @return The <code>IoSession</code> object, or <code>null</code>
  91. * if none.
  92. */
  93. public IoSession getSession() {
  94. return session;
  95. }
  96.  
  97. /**
  98. * Checks if this packet is considered to be a bare packet, which
  99. * means that it does not include the standard packet header (ID
  100. * and length values).
  101. *
  102. * @return Whether this packet is a bare packet
  103. */
  104. public boolean isBare() {
  105. return bare;
  106. }
  107.  
  108. public Size getSize() {
  109. return size;
  110. }
  111.  
  112. /**
  113. * Returns the packet ID.
  114. *
  115. * @return The packet ID
  116. */
  117. public int getId() {
  118. return pID;
  119. }
  120.  
  121. /**
  122. * Returns the length of the payload of this packet.
  123. *
  124. * @return The length of the packet's payload
  125. */
  126. public int getLength() {
  127. return pLength;
  128. }
  129.  
  130. /**
  131. * Returns the entire payload data of this packet.
  132. *
  133. * @return The payload <code>byte</code> array
  134. */
  135. public byte[] getData() {
  136. return pData;
  137. }
  138.  
  139. /**
  140. * Returns the remaining payload data of this packet.
  141. *
  142. * @return The payload <code>byte</code> array
  143. */
  144. public byte[] getRemainingData() {
  145. byte[] data = new byte[pLength - caret];
  146. for (int i = 0; i < data.length; i++) {
  147. data[i] = pData[i + caret];
  148. }
  149. caret += data.length;
  150. return data;
  151.  
  152. }
  153.  
  154. /**
  155. * Reads the next <code>byte</code> from the payload.
  156. *
  157. * @return A <code>byte</code>
  158. */
  159. public byte readByte() {
  160. return pData[caret++];
  161. }
  162.  
  163. /**
  164. * Reads the next <code>short</code> from the payload.
  165. *
  166. * @return A <code>short</code>
  167. */
  168. public short readShort() {
  169. return (short) ((short) ((pData[caret++] & 0xff) << 8) | (short) (pData[caret++] & 0xff));
  170. }
  171.  
  172. public int readLEShortA() {
  173. int i = ((pData[caret++] - 128 & 0xff)) + ((pData[caret++] & 0xff) << 8);
  174. if (i > 32767)
  175. i -= 0x10000;
  176. return i;
  177. }
  178.  
  179. public int readLEShort() {
  180. int i = ((pData[caret++] & 0xff)) + ((pData[caret++] & 0xff) << 8);
  181. if (i > 32767)
  182. i -= 0x10000;
  183. return i;
  184. }
  185.  
  186. /**
  187. * Reads the next <code>int</code> from the payload.
  188. *
  189. * @return An <code>int</code>
  190. */
  191. public int readInt() {
  192. return ((pData[caret++] & 0xff) << 24) | ((pData[caret++] & 0xff) << 16) | ((pData[caret++] & 0xff) << 8) | (pData[caret++] & 0xff);
  193. }
  194.  
  195. public int readLEInt() {
  196. return (pData[caret++] & 0xff) | ((pData[caret++] & 0xff) << 8) | ((pData[caret++] & 0xff) << 16) | ((pData[caret++] & 0xff) << 24);
  197. }
  198.  
  199. /**
  200. * Reads the next <code>long</code> from the payload.
  201. *
  202. * @return A <code>long</code>
  203. */
  204. public long readLong() {
  205. return ((long) (pData[caret++] & 0xff) << 56) | ((long) (pData[caret++] & 0xff) << 48) | ((long) (pData[caret++] & 0xff) << 40) | ((long) (pData[caret++] & 0xff) << 32) | (
  206. (long) (pData[caret++] & 0xff) << 24) | ((long) (pData[caret++] & 0xff) << 16) | ((long) (pData[caret++] & 0xff) << 8) | ((pData[caret++] & 0xff));
  207. }
  208.  
  209. /**
  210. * Reads the string which is formed by the unread portion
  211. * of the payload.
  212. *
  213. * @return A <code>String</code>
  214. */
  215. public String readString() {
  216. return readString(pLength - caret);
  217. }
  218.  
  219. public String readRS2String() {
  220. int start = caret;
  221. while (pData[caret++] != 0)
  222. ;
  223. return new String(pData, start, caret - start - 1);
  224. }
  225.  
  226. public void readBytes(byte[] buf, int off, int len) {
  227. for (int i = 0; i < len; i++) {
  228. buf[off + i] = pData[caret++];
  229. }
  230. }
  231.  
  232. /**
  233. * Reads a string of the specified length from the payload.
  234. *
  235. * @param length The length of the string to be read
  236. * @return A <code>String</code>
  237. */
  238. public String readString(int length) {
  239. String rv = new String(pData, caret, length);
  240. caret += length;
  241. return rv;
  242. }
  243.  
  244. /**
  245. * Skips the specified number of bytes in the payload.
  246. *
  247. * @param x The number of bytes to be skipped
  248. */
  249. public void skip(int x) {
  250. caret += x;
  251. }
  252.  
  253.  
  254. public int remaining() {
  255. return pData.length - caret;
  256. }
  257.  
  258. /**
  259. * Returns this packet in string form.
  260. *
  261. * @return A <code>String</code> representing this packet
  262. */
  263. @Override
  264. public String toString() {
  265. StringBuilder sb = new StringBuilder();
  266. sb.append("[id=" + pID + ",len=" + pLength + ",data=0x");
  267. for (int x = 0; x < pLength; x++) {
  268. sb.append(byteToHex(pData[x], true));
  269. }
  270. sb.append("]");
  271. return sb.toString();
  272. }
  273.  
  274. private static String byteToHex(byte b, boolean forceLeadingZero) {
  275. StringBuilder out = new StringBuilder();
  276. int ub = b & 0xff;
  277. if (ub / 16 > 0 || forceLeadingZero)
  278. out.append(hex[ub / 16]);
  279. out.append(hex[ub % 16]);
  280. return out.toString();
  281. }
  282.  
  283. private static final char[] hex = "0123456789ABCDEF".toCharArray();
  284.  
  285. public int readShortA() {
  286. caret += 2;
  287. return ((pData[caret - 2] & 0xFF) << 8) + (pData[caret - 1] - 128 & 0xFF);
  288. }
  289.  
  290. public byte readByteC() {
  291. return (byte) -readByte();
  292. }
  293.  
  294. public byte readByteS() {
  295. return (byte) (128 - readByte());
  296. }
  297.  
  298. public int readUnsignedWord() {
  299. caret += 2;
  300. return ((pData[caret - 2] & 0xff) << 8) + (pData[caret - 1] & 0xff);
  301. }
  302.  
  303. public int readUnsignedWordA() {
  304. caret += 2;
  305. return ((pData[caret - 2] & 0xff) << 8) + (pData[caret - 1] - 128 & 0xff);
  306. }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement