Facebook
From sdfasdf, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 346
  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 int[] readVarIntArray(final ByteBuf buf) {
  60.         final int len = readVarInt(buf);
  61.         final int[] ret = new int[len];
  62.         for (int i = 0; i < len; ++i) {
  63.             ret[i] = readVarInt(buf);
  64.         }
  65.         return ret;
  66.     }
  67.    
  68.     public static void writeStringArray(final List<String> s, final ByteBuf buf) {
  69.         writeVarInt(s.size(), buf);
  70.         for (final String str : s) {
  71.             writeString(str, buf);
  72.         }
  73.     }
  74.    
  75.     public static List<String> readStringArray(final ByteBuf buf) {
  76.         final int len = readVarInt(buf);
  77.         final List<String> ret = new ArrayList<String>(len);
  78.         for (int i = 0; i < len; ++i) {
  79.             ret.add(readString(buf));
  80.         }
  81.         return ret;
  82.     }
  83.    
  84.     public static int readVarInt(final ByteBuf input) {
  85.         return readVarInt(input, 5);
  86.     }
  87.    
  88.     public static int readVarInt(final ByteBuf input, final int maxBytes) {
  89.         int out = 0;
  90.         int bytes = 0;
  91.         byte in;
  92.         do {
  93.             in = input.readByte();
  94.             out |= (in & 0x7F) << bytes++ * 7;
  95.             if (bytes > maxBytes) {
  96.                 throw new RuntimeException("VarInt too big");
  97.             }
  98.         } while ((in & 0x80) == 0x80);
  99.         return out;
  100.     }
  101.    
  102.     public static void writeVarInt(int value, final ByteBuf output) {
  103.         do {
  104.             int part = value & 0x7F;
  105.             value >>>= 7;
  106.             if (value != 0) {
  107.                 part |= 0x80;
  108.             }
  109.             output.writeByte(part);
  110.         } while (value != 0);
  111.     }
  112.    
  113.     public static int readVarShort(final ByteBuf buf) {
  114.         int low = buf.readUnsignedShort();
  115.         int high = 0;
  116.         if ((low & 0x8000) != 0x0) {
  117.             low &= 0x7FFF;
  118.             high = buf.readUnsignedByte();
  119.         }
  120.         return (high & 0xFF) << 15 | low;
  121.     }
  122.    
  123.     public static void writeVarShort(final ByteBuf buf, final int toWrite) {
  124.         int low = toWrite & 0x7FFF;
  125.         final int high = (toWrite & 0x7F8000) >> 15;
  126.         if (high != 0) {
  127.             low |= 0x8000;
  128.         }
  129.         buf.writeShort(low);
  130.         if (high != 0) {
  131.             buf.writeByte(high);
  132.         }
  133.     }
  134.    
  135.     public static void writeUUID(final UUID value, final ByteBuf output) {
  136.         output.writeLong(value.getMostSignificantBits());
  137.         output.writeLong(value.getLeastSignificantBits());
  138.     }
  139.    
  140.     public static UUID readUUID(final ByteBuf input) {
  141.         return new UUID(input.readLong(), input.readLong());
  142.     }
  143.    
  144.     public void read(final ByteBuf buf) {
  145.         throw new UnsupportedOperationException("Packet must implement read method");
  146.     }
  147.    
  148.     public void read(final ByteBuf buf, final ProtocolConstants.Direction direction, final int protocolVersion) {
  149.         this.read(buf);
  150.     }
  151.    
  152.     public void write(final ByteBuf buf) {
  153.         throw new UnsupportedOperationException("Packet must implement write method");
  154.     }
  155.    
  156.     public void write(final ByteBuf buf, final ProtocolConstants.Direction direction, final int protocolVersion) {
  157.         this.write(buf);
  158.     }
  159.    
  160.     public abstract void handle(final AbstractPacketHandler p0) throws Exception;
  161.    
  162.     @Override
  163.     public abstract boolean equals(final Object p0);
  164.    
  165.     @Override
  166.     public abstract int hashCode();
  167.    
  168.     @Override
  169.     public abstract String toString();
  170. }
  171.  

Replies to asdfasd rss

Title Name Language When
Re: asdfasd sdfasdf text 5 Years ago.