I’m trying to send an image over a socket, and I’ve come across a strange issue.. ImageIO.write is sending MORE data than ImageIO.read receives. For example if I have the code below in a loop:
(Client side)
out.writeByte(222);//magic num for testing
out.writeByte(blockSize);
out.writeByte(x / blockSize);
out.writeByte(y / blockSize);
ImageIO.write(part, "PNG", out);
(Server sided)
if (din.readUnsignedByte() != 222) {
throw new RuntimeException();
}
int partSize = din.readUnsignedByte();
int partX = partSize * din.readUnsignedByte();
int partY = partSize * din.readUnsignedByte();
BufferedImage part = ImageIO.read(din);
On the second iteration, the magic number will fail because ImageIO.read has not read all of the data sent from the other end. Why is this? It seems like a major issue. Or am I missing something?
EDIT: This seems to be a confirmed bug as of 2008-04-14. Bug ID 6687964. Why hasn’t this been fixed?.. agh.
What I came up with as a workaround:
(bout being a ByteArrayOutputStream, and out being the socket stream)
This code will send the size of the image as a short before writing the image, thus preventing the bug.