Facebook
From sdfasdf, 5 Years ago, written in Plain Text.
This paste is a reply to asdfasd from sdfasdf - view diff
Embed
Download Paste or View Raw
Hits: 385
  1. package net.md_5.bungee.protocol;
  2.  
  3. import java.util.UUID;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import com.google.common.base.Charsets;
  8. import io.netty.buffer.ByteBuf;
  9.  
  10. public abstract class DefinedPacket
  11. {
  12.     public static void writeString(final String s, final ByteBuf buf) {
  13.         if (s.length() > 32767) {
  14.             throw new OverflowPacketException(String.format("Cannot send string longer than Short.MAX_VALUE (got %s characters)", s.length()));
  15.         }
  16.         final byte[] b = s.getBytes(Charsets.UTF_8);
  17.         writeVarInt(b.length, buf);
  18.         buf.writeBytes(b);
  19.     }
  20.    
  21.     public static String readString(final ByteBuf buf) {
  22.         final int len = readVarInt(buf);
  23.         if (len > 32767) {
  24.             throw new OverflowPacketException(String.format("Cannot receive string longer than Short.MAX_VALUE (got %s characters)", len));
  25.         }
  26.         final byte[] b = new byte[len];
  27.         buf.readBytes(b);
  28.         return new String(b, Charsets.UTF_8);
  29.     }
  30.    
  31.     public static void writeArray(final byte[] b, final ByteBuf buf) {
  32.         if (b.length > 32767) {
  33.             throw new OverflowPacketException(String.format("Cannot send byte array longer than Short.MAX_VALUE (got %s bytes)", b.length));
  34.         }
  35.         writeVarInt(b.length, buf);
  36.         buf.writeBytes(b);
  37.     }
  38.    
  39.     public static byte[] toArray(final ByteBuf buf) {
  40.         final byte[] ret = new byte[buf.readableBytes()];
  41.         buf.readBytes(ret);
  42.         return ret;
  43.     }
  44.    
  45.     public static byte[] readArray(final ByteBuf buf) {
  46.         return readArray(buf, buf.readableBytes());
  47.     }
  48.    
  49.     public static byte[] readArray(final ByteBuf buf, final int limit) {
  50.         final int len = readVarInt(buf);
  51.         if (len > limit) {
  52.             throw new OverflowPacketException(String.format("Cannot receive byte array longer than %s (got %s bytes)", limit, len));
  53.         }
  54.         final byte[] ret = new byte[len];
  55.         buf.readBytes(ret);
  56.         return ret;
  57.     }
  58.    
  59.     public static void writeStringArray(final List<String> s, final ByteBuf buf) {
  60.         writeVarInt(s.size(), buf);
  61.         for (final String str : s) {
  62.             writeString(str, buf);
  63.         }
  64.     }
  65.    
  66.     public static List<String> readStringArray(final ByteBuf buf) {
  67.         final int len = readVarInt(buf);
  68.         final List<String> ret = new ArrayList<String>(len);
  69.         for (int i = 0; i < len; ++i) {
  70.             ret.add(readString(buf));
  71.         }
  72.         return ret;
  73.     }
  74.    
  75.     public static int readVarInt(final ByteBuf input) {
  76.         return readVarInt(input, 5);
  77.     }
  78.    
  79.     public static int readVarInt(final ByteBuf input, final int maxBytes) {
  80.         int out = 0;
  81.         int bytes = 0;
  82.         while (input.readerIndex() != input.writerIndex()) {
  83.             byte in;
  84.             try {
  85.                 in = input.readByte();
  86.             }
  87.             catch (IndexOutOfBoundsException ex) {
  88.                 return 47;
  89.             }
  90.             out |= (in & 0x7F) << bytes++ * 7;
  91.             if (bytes > maxBytes) {
  92.                 throw new RuntimeException("VarInt too big");
  93.             }
  94.             if ((in & 0x80) != 0x80) {
  95.                 return out;
  96.             }
  97.         }
  98.         return 47;
  99.     }
  100.    
  101.     public static void writeVarInt(int value, final ByteBuf output) {
  102.         do {
  103.             int part = value & 0x7F;
  104.             value >>>= 7;
  105.             if (value != 0) {
  106.                 part |= 0x80;
  107.             }
  108.             output.writeByte(part);
  109.         } while (value != 0);
  110.     }
  111.    
  112.     public static int readVarShort(final ByteBuf buf) {
  113.         int low = buf.readUnsignedShort();
  114.         int high = 0;
  115.         if ((low & 0x8000) != 0x0) {
  116.             low &= 0x7FFF;
  117.             high = buf.readUnsignedByte();
  118.         }
  119.         return (high & 0xFF) << 15 | low;
  120.     }
  121.    
  122.     public static void writeVarShort(final ByteBuf buf, final int toWrite) {
  123.         int low = toWrite & 0x7FFF;
  124.         final int high = (toWrite & 0x7F8000) >> 15;
  125.         if (high != 0) {
  126.             low |= 0x8000;
  127.         }
  128.         buf.writeShort(low);
  129.         if (high != 0) {
  130.             buf.writeByte(high);
  131.         }
  132.     }
  133.    
  134.     public static void writeUUID(final UUID value, final ByteBuf output) {
  135.         output.writeLong(value.getMostSignificantBits());
  136.         output.writeLong(value.getLeastSignificantBits());
  137.     }
  138.    
  139.     public static UUID readUUID(final ByteBuf input) {
  140.         return new UUID(input.readLong(), input.readLong());
  141.     }
  142.    
  143.     public void read(final ByteBuf buf) {
  144.         throw new UnsupportedOperationException("Packet must implement read method");
  145.     }
  146.    
  147.     public void read(final ByteBuf buf, final ProtocolConstants.Direction direction, final int protocolVersion) {
  148.         this.read(buf);
  149.     }
  150.    
  151.     public void write(final ByteBuf buf) {
  152.         throw new UnsupportedOperationException("Packet must implement write method");
  153.     }
  154.    
  155.     public void write(final ByteBuf buf, final ProtocolConstants.Direction direction, final int protocolVersion) {
  156.         this.write(buf);
  157.     }
  158.    
  159.     public abstract void handle(final AbstractPacketHandler p0) throws Exception;
  160.    
  161.     @Override
  162.     public abstract boolean equals(final Object p0);
  163.    
  164.     @Override
  165.     public abstract int hashCode();
  166.    
  167.     @Override
  168.     public abstract String toString();
  169. }
  170.