public static int readVarInt(ByteBuf byteBuf) {
        int out = 0;
        int bytes = 0;
        byte in;
        while (true) {
            in = byteBuf.readByte();
            out |= (in & 0x7F) << (bytes++ * 7);
            if (bytes > 5) {
                throw new IllegalStateException("Integer is bigger than maximum allowed!");
            }
            if ((in & 0x80) != 0x80) {
                break;
            }
        }
        return out;
    }