Guest User

Stream.java

a guest
Aug 19th, 2015
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.55 KB | None | 0 0
  1. package server.util;
  2.  
  3. import server.Config;
  4.  
  5. public class Stream {
  6.  
  7.     public Stream() {
  8.     }
  9.  
  10.     public Stream(byte abyte0[]) {
  11.         buffer = abyte0;
  12.         currentOffset = 0;
  13.     }
  14.  
  15.     public byte readSignedByteA() {
  16.         return (byte) (buffer[currentOffset++] - 128);
  17.     }
  18.  
  19.     public byte readSignedByteC() {
  20.         return (byte) (-buffer[currentOffset++]);
  21.     }
  22.  
  23.     public byte readSignedByteS() {
  24.         return (byte) (128 - buffer[currentOffset++]);
  25.     }
  26.  
  27.     public int readUnsignedByteA() {
  28.         return buffer[currentOffset++] - 128 & 0xff;
  29.     }
  30.  
  31.     public int readUnsignedByteC() {
  32.         return -buffer[currentOffset++] & 0xff;
  33.     }
  34.  
  35.     public int readUnsignedByteS() {
  36.         return 128 - buffer[currentOffset++] & 0xff;
  37.     }
  38.  
  39.     public void writeByteA(int i) {
  40.         ensureCapacity(1);
  41.         buffer[currentOffset++] = (byte) (i + 128);
  42.     }
  43.  
  44.     public void writeByteS(int i) {
  45.         ensureCapacity(1);
  46.         buffer[currentOffset++] = (byte) (128 - i);
  47.     }
  48.  
  49.     public void writeByteC(int i) {
  50.         ensureCapacity(1);
  51.         buffer[currentOffset++] = (byte) (-i);
  52.     }
  53.  
  54.     public int readSignedWordBigEndian() {
  55.         currentOffset += 2;
  56.         int i = ((buffer[currentOffset - 1] & 0xff) << 8)
  57.                 + (buffer[currentOffset - 2] & 0xff);
  58.         if (i > 32767)
  59.             i -= 0x10000;
  60.         return i;
  61.     }
  62.  
  63.     public int readSignedWordA() {
  64.         currentOffset += 2;
  65.         int i = ((buffer[currentOffset - 2] & 0xff) << 8)
  66.                 + (buffer[currentOffset - 1] - 128 & 0xff);
  67.         if (i > 32767)
  68.             i -= 0x10000;
  69.         return i;
  70.     }
  71.  
  72.     public int readSignedWordBigEndianA() {
  73.         currentOffset += 2;
  74.         int i = ((buffer[currentOffset - 1] & 0xff) << 8)
  75.                 + (buffer[currentOffset - 2] - 128 & 0xff);
  76.         if (i > 32767)
  77.             i -= 0x10000;
  78.         return i;
  79.     }
  80.  
  81.     public int readUnsignedWordBigEndian() {
  82.         currentOffset += 2;
  83.         return ((buffer[currentOffset - 1] & 0xff) << 8)
  84.                 + (buffer[currentOffset - 2] & 0xff);
  85.     }
  86.  
  87.     public int readUnsignedWordA() {
  88.         currentOffset += 2;
  89.         return ((buffer[currentOffset - 2] & 0xff) << 8)
  90.                 + (buffer[currentOffset - 1] - 128 & 0xff);
  91.     }
  92.  
  93.     public int readUnsignedWordBigEndianA() {
  94.         currentOffset += 2;
  95.         return ((buffer[currentOffset - 1] & 0xff) << 8)
  96.                 + (buffer[currentOffset - 2] - 128 & 0xff);
  97.     }
  98.  
  99.     public void writeWordBigEndianA(int i) {
  100.         ensureCapacity(2);
  101.         buffer[currentOffset++] = (byte) (i + 128);
  102.         buffer[currentOffset++] = (byte) (i >> 8);
  103.     }
  104.  
  105.     public void writeWordA(int i) {
  106.         ensureCapacity(2);
  107.         buffer[currentOffset++] = (byte) (i >> 8);
  108.         buffer[currentOffset++] = (byte) (i + 128);
  109.     }
  110.  
  111.     public void writeWordBigEndian_dup(int i) {
  112.         ensureCapacity(2);
  113.         buffer[currentOffset++] = (byte) i;
  114.         buffer[currentOffset++] = (byte) (i >> 8);
  115.     }
  116.  
  117.     public int readDWord_v1() {
  118.         currentOffset += 4;
  119.         return ((buffer[currentOffset - 2] & 0xff) << 24)
  120.                 + ((buffer[currentOffset - 1] & 0xff) << 16)
  121.                 + ((buffer[currentOffset - 4] & 0xff) << 8)
  122.                 + (buffer[currentOffset - 3] & 0xff);
  123.     }
  124.  
  125.     public int readDWord_v2() {
  126.         currentOffset += 4;
  127.         return ((buffer[currentOffset - 3] & 0xff) << 24)
  128.                 + ((buffer[currentOffset - 4] & 0xff) << 16)
  129.                 + ((buffer[currentOffset - 1] & 0xff) << 8)
  130.                 + (buffer[currentOffset - 2] & 0xff);
  131.     }
  132.  
  133.     public void writeDWord_v1(int i) {
  134.         ensureCapacity(4);
  135.         buffer[currentOffset++] = (byte) (i >> 8);
  136.         buffer[currentOffset++] = (byte) i;
  137.         buffer[currentOffset++] = (byte) (i >> 24);
  138.         buffer[currentOffset++] = (byte) (i >> 16);
  139.     }
  140.  
  141.     public void writeDWord_v2(int i) {
  142.         ensureCapacity(4);
  143.         buffer[currentOffset++] = (byte) (i >> 16);
  144.         buffer[currentOffset++] = (byte) (i >> 24);
  145.         buffer[currentOffset++] = (byte) i;
  146.         buffer[currentOffset++] = (byte) (i >> 8);
  147.     }
  148.  
  149.     public void readBytes_reverse(byte abyte0[], int i, int j) {
  150.         for (int k = (j + i) - 1; k >= j; k--)
  151.             abyte0[k] = buffer[currentOffset++];
  152.  
  153.     }
  154.  
  155.     public void writeBytes_reverse(byte abyte0[], int i, int j) {
  156.         ensureCapacity(i);
  157.         for (int k = (j + i) - 1; k >= j; k--)
  158.             buffer[currentOffset++] = abyte0[k];
  159.  
  160.     }
  161.  
  162.     public void readBytes_reverseA(byte abyte0[], int i, int j) {
  163.         ensureCapacity(i);
  164.         for (int k = (j + i) - 1; k >= j; k--)
  165.             abyte0[k] = (byte) (buffer[currentOffset++] - 128);
  166.  
  167.     }
  168.  
  169.     public void writeBytes_reverseA(byte abyte0[], int i, int j) {
  170.         ensureCapacity(i);
  171.         for (int k = (j + i) - 1; k >= j; k--)
  172.             buffer[currentOffset++] = (byte) (abyte0[k] + 128);
  173.  
  174.     }
  175.  
  176.     public void createFrame(int id) {
  177.         ensureCapacity(1);
  178.         buffer[currentOffset++] = (byte) (id + packetEncryption.getNextKey());
  179.     }
  180.  
  181.     private static final int frameStackSize = 10;
  182.     private int frameStackPtr = -1;
  183.     private int frameStack[] = new int[frameStackSize];
  184.  
  185.     public void createFrameVarSize(int id) {
  186.         ensureCapacity(3);
  187.         buffer[currentOffset++] = (byte) (id + packetEncryption.getNextKey());
  188.         buffer[currentOffset++] = 0;
  189.         if (frameStackPtr >= frameStackSize - 1) {
  190.             throw new RuntimeException("Stack overflow");
  191.         } else
  192.             frameStack[++frameStackPtr] = currentOffset;
  193.     }
  194.  
  195.     public void createFrameVarSizeWord(int id) {
  196.         ensureCapacity(2);
  197.         buffer[currentOffset++] = (byte) (id + packetEncryption.getNextKey());
  198.         writeWord(0);
  199.         if (frameStackPtr >= frameStackSize - 1) {
  200.             throw new RuntimeException("Stack overflow");
  201.         } else
  202.             frameStack[++frameStackPtr] = currentOffset;
  203.     }
  204.  
  205.     public void endFrameVarSize() {
  206.         if (frameStackPtr < 0)
  207.             throw new RuntimeException("Stack empty");
  208.         else
  209.             writeFrameSize(currentOffset - frameStack[frameStackPtr--]);
  210.     }
  211.  
  212.     public void endFrameVarSizeWord() {
  213.         if (frameStackPtr < 0)
  214.             throw new RuntimeException("Stack empty");
  215.         else
  216.             writeFrameSizeWord(currentOffset - frameStack[frameStackPtr--]);
  217.     }
  218.  
  219.     public void writeByte(int i) {
  220.         ensureCapacity(1);
  221.         buffer[currentOffset++] = (byte) i;
  222.     }
  223.  
  224.     public void writeWord(int i) {
  225.         ensureCapacity(2);
  226.         buffer[currentOffset++] = (byte) (i >> 8);
  227.         buffer[currentOffset++] = (byte) i;
  228.     }
  229.  
  230.     public void writeWordBigEndian(int i) {
  231.         ensureCapacity(2);
  232.         buffer[currentOffset++] = (byte) i;
  233.         buffer[currentOffset++] = (byte) (i >> 8);
  234.     }
  235.  
  236.     public void write3Byte(int i) {
  237.         ensureCapacity(3);
  238.         buffer[currentOffset++] = (byte) (i >> 16);
  239.         buffer[currentOffset++] = (byte) (i >> 8);
  240.         buffer[currentOffset++] = (byte) i;
  241.     }
  242.  
  243.     public void writeDWord(int i) {
  244.         ensureCapacity(4);
  245.         buffer[currentOffset++] = (byte) (i >> 24);
  246.         buffer[currentOffset++] = (byte) (i >> 16);
  247.         buffer[currentOffset++] = (byte) (i >> 8);
  248.         buffer[currentOffset++] = (byte) i;
  249.     }
  250.  
  251.     public void writeDWordBigEndian(int i) {
  252.         ensureCapacity(4);
  253.         buffer[currentOffset++] = (byte) i;
  254.         buffer[currentOffset++] = (byte) (i >> 8);
  255.         buffer[currentOffset++] = (byte) (i >> 16);
  256.         buffer[currentOffset++] = (byte) (i >> 24);
  257.     }
  258.  
  259.     public void writeQWord(long l) {
  260.         ensureCapacity(8);
  261.         buffer[currentOffset++] = (byte) (int) (l >> 56);
  262.         buffer[currentOffset++] = (byte) (int) (l >> 48);
  263.         buffer[currentOffset++] = (byte) (int) (l >> 40);
  264.         buffer[currentOffset++] = (byte) (int) (l >> 32);
  265.         buffer[currentOffset++] = (byte) (int) (l >> 24);
  266.         buffer[currentOffset++] = (byte) (int) (l >> 16);
  267.         buffer[currentOffset++] = (byte) (int) (l >> 8);
  268.         buffer[currentOffset++] = (byte) (int) l;
  269.     }
  270.  
  271.     public void writeString(java.lang.String s) {
  272.         ensureCapacity(s.length());
  273.         System.arraycopy(s.getBytes(), 0, buffer, currentOffset, s.length());
  274.         currentOffset += s.length();
  275.         buffer[currentOffset++] = 10;
  276.     }
  277.  
  278.     public void writeBytes(byte abyte0[], int i, int j) {
  279.         ensureCapacity(i);
  280.         for (int k = j; k < j + i; k++)
  281.             buffer[currentOffset++] = abyte0[k];
  282.     }
  283.  
  284.     public void writeFrameSize(int i) {
  285.         buffer[currentOffset - i - 1] = (byte) i;
  286.     }
  287.  
  288.     public void writeFrameSizeWord(int i) {
  289.         buffer[currentOffset - i - 2] = (byte) (i >> 8);
  290.         buffer[currentOffset - i - 1] = (byte) i;
  291.     }
  292.  
  293.     public int readUnsignedByte() {
  294.         return buffer[currentOffset++] & 0xff;
  295.     }
  296.  
  297.     public byte readSignedByte() {
  298.         return buffer[currentOffset++];
  299.     }
  300.  
  301.     public int readUnsignedWord() {
  302.         currentOffset += 2;
  303.         return ((buffer[currentOffset - 2] & 0xff) << 8)
  304.                 + (buffer[currentOffset - 1] & 0xff);
  305.     }
  306.  
  307.     public int readSignedWord() {
  308.         currentOffset += 2;
  309.         int i = ((buffer[currentOffset - 2] & 0xff) << 8)
  310.                 + (buffer[currentOffset - 1] & 0xff);
  311.         if (i > 32767)
  312.             i -= 0x10000;
  313.         return i;
  314.     }
  315.  
  316.     public int readDWord() {
  317.         currentOffset += 4;
  318.         return ((buffer[currentOffset - 4] & 0xff) << 24)
  319.                 + ((buffer[currentOffset - 3] & 0xff) << 16)
  320.                 + ((buffer[currentOffset - 2] & 0xff) << 8)
  321.                 + (buffer[currentOffset - 1] & 0xff);
  322.     }
  323.  
  324.     public long readQWord() {
  325.         long l = readDWord() & 0xffffffffL;
  326.         long l1 = readDWord() & 0xffffffffL;
  327.         return (l << 32) + l1;
  328.     }
  329.  
  330.     public java.lang.String readString() {
  331.         int i = currentOffset;
  332.         while (buffer[currentOffset++] != 10)
  333.             ;
  334.         return new String(buffer, i, currentOffset - i - 1);
  335.     }
  336.  
  337.     public void readBytes(byte abyte0[], int i, int j) {
  338.         for (int k = j; k < j + i; k++)
  339.             abyte0[k] = buffer[currentOffset++];
  340.  
  341.     }
  342.  
  343.     public void initBitAccess() {
  344.         bitPosition = currentOffset * 8;
  345.     }
  346.  
  347.     public void writeBits(int numBits, int value) {
  348.         ensureCapacity(((int) Math.ceil(numBits * 8)) * 4);
  349.         int bytePos = bitPosition >> 3;
  350.         int bitOffset = 8 - (bitPosition & 7);
  351.         bitPosition += numBits;
  352.  
  353.         for (; numBits > bitOffset; bitOffset = 8) {
  354.             buffer[bytePos] &= ~bitMaskOut[bitOffset];
  355.             buffer[bytePos++] |= (value >> (numBits - bitOffset))
  356.                     & bitMaskOut[bitOffset];
  357.  
  358.             numBits -= bitOffset;
  359.         }
  360.         if (numBits == bitOffset) {
  361.             buffer[bytePos] &= ~bitMaskOut[bitOffset];
  362.             buffer[bytePos] |= value & bitMaskOut[bitOffset];
  363.         } else {
  364.             buffer[bytePos] &= ~(bitMaskOut[numBits] << (bitOffset - numBits));
  365.             buffer[bytePos] |= (value & bitMaskOut[numBits]) << (bitOffset - numBits);
  366.         }
  367.     }
  368.  
  369.     public void finishBitAccess() {
  370.         currentOffset = (bitPosition + 7) / 8;
  371.     }
  372.  
  373.     public byte buffer[] = null;
  374.     public int currentOffset = 0;
  375.     public int bitPosition = 0;
  376.  
  377.     public static int bitMaskOut[] = new int[32];
  378.     static {
  379.         for (int i = 0; i < 32; i++)
  380.             bitMaskOut[i] = (1 << i) - 1;
  381.     }
  382.  
  383.     public void ensureCapacity(int len) {
  384.         if ((currentOffset + len + 1) >= buffer.length) {
  385.             byte[] oldBuffer = buffer;
  386.             int newLength = (buffer.length * 2);
  387.             buffer = new byte[newLength];
  388.             System.arraycopy(oldBuffer, 0, buffer, 0, oldBuffer.length);
  389.             ensureCapacity(len);
  390.         }
  391.     }
  392.  
  393.     public void reset() {
  394.         if (!(currentOffset > Config.BUFFER_SIZE)) {
  395.             byte[] oldBuffer = buffer;
  396.             buffer = new byte[Config.BUFFER_SIZE];
  397.             for (int i = 0; i < currentOffset; i++) {
  398.                 buffer[i] = oldBuffer[i];
  399.             }
  400.         }
  401.     }
  402.  
  403.     public ISAACRandomGen packetEncryption = null;
  404.  
  405. }
Advertisement
Add Comment
Please, Sign In to add comment