通过数据包将png从服务器发送到客户端:indexOutOfBoundsException

问题描述

当尝试从服务器向客户端发送png时,使用netty获取了indexOutOfBoundException。该错误发生在它读取客户端中的字节p.readBytes()的行上。

Exception in thread "LWJGL Application" java.lang.indexoutofboundsexception: readerIndex(97) + length(199852) exceeds writerIndex(185453): PooledUnsafeDirectByteBuf(ridx: 97,widx: 185453,cap: 185453)
    at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1395)
    at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1389)
    at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:850)
    at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:858)
    at com.benberi.cadesim.client.codec.util.Packet.readBytes(Packet.java:79)
    at com.benberi.cadesim.client.packet.in.ListAllMapsPacket.execute(ListAllMapsPacket.java:29)

服务器端:

  1. 在服务器所在的特定文件夹中获取图像
  2. 将图像转换为字节数组以发送数据
  3. 使用数据包将数据发送到客户端

客户端:

  1. 接收图像的字节数组//我相信这里会发生问题
  2. 将字节数组转换为pixmap(libgdx)
  3. 将像素图添加pixmap数组的特定索引中
  4. 稍后使用pixmap进行一些操作

服务器数据包:

    @Override
    public void encode() {
        setPacketLengthType(PacketLength.MEDIUM); //
        writeByte((byte)ServerConfiguration.getAvailableMaps().size());
        for (int i=0; i<ServerConfiguration.getAvailableMaps().size(); i++)
        {
            String map = ServerConfiguration.getAvailableMaps().get(i).replace(".txt","");
            writeByteString(map);
            String mapDir = String.format("maps/%s.png",map);
            try {
                File imageFile = new File(mapDir);
                BufferedImage img = ImageIO.read(imageFile);
                writeInt((int)imageFile.length()); //write size of image
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                ImageIO.write(img,"png",stream);
                writeBytes(stream.toByteArray()); // write image; byte array
                stream.flush();
                stream = null;
            } catch (IOException e) {
//              e.printstacktrace();
                writeInt(0);
            }
        }
        setLength(getBuffer().readableBytes());
    }


客户端数据包:

    @Override
    public void execute(Packet p) {
        size = (int)p.readByte();
        context.pixmapArray = new pixmap[size]; //create pixmap array with number of maps
        int i=0;
        while(i<size) {
            context.getMaps().add((String)p.readByteString()); //writes all map names to list
            Integer fileSize = p.readInt(); //read size of png
            if(fileSize == 0) {//incase image not found from server
                context.pixmapArray[i] = null;
            }else {
                pixmap pixmap = new pixmap(p.readBytes(fileSize),fileSize); //byte[] to pixmap
                context.pixmapArray[i] = pixmap; //add pixmap to pixmap array for future use
            }
            i++;
        }
    }

readBytes方法

    public byte[] readBytes(int length) {
        byte[] bytes = new byte[length];
        this.dataBuffer.readBytes(bytes); //netty ByteBuf 

        return bytes;
    }

解决方法

您的问题是您要重新编码图像,从而更改文件大小。当您读取映像ImageIO.read()时,它可能会丢失元数据,因此,当您使用ImageIO.write()写入映像时,不能保证逐字节地对其进行编码,使其与最初在磁盘上进行的编码完全相同。我建议直接从文件中直接复制字节:

File imageFile = new File(mapDir);
byte[] fileContent = Files.readAllBytes(imageFile.toPath());
writeInt((int)imageFile.length()); //this should be the same as fileContent.length
writeBytes(fileContent);