Я пытаюсь перенести объект с клиента (обычный IO) на сервер (NIO). Передача работает нормально до тех пор, пока объект не достигнет определенного размера, тогда сервер выкинет «StreamCorruptException», указав «недопустимый код типа».Java NIO Serialization StreamCorruptException Недопустимый тип кода
метод называется на сервере следующее:
private void read(SelectionKey key) throws IOException, ClassNotFoundException {
SocketChannel chan = (SocketChannel) key.channel();
//read message object
ByteBuffer buf = ByteBuffer.allocate(1024);
ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.capacity());
int length = 0;
int totalRead = 0;
int read = chan.read(buf);
System.out.println("read data " + read);
if (read >= 4) {
length = IoUtil.readInt(buf.array(), 0);
bos.write(buf.array());
buf.rewind();
totalRead += read;
LogUtil.log("length of data message is " + length);
}
while (totalRead < length) { //read until we get no more data
read = chan.read(buf);
if (read == -1) break;
if (read == 0) continue;
totalRead += read;
System.out.println("I've read " + read + " bytes. Total amount of bytes " + totalRead);
bos.write(buf.array());
buf.rewind();
}
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray(), 4, length);
ObjectInputStream inFromClient = new ObjectInputStream(bis);
Message msg = (Message) inFromClient.readObject();
SocketAddress sa = chan.socket().getRemoteSocketAddress();
LogUtil.log(msg.getIdentifier() + "-Message von " + sa);
commData.handleMessage(sa, msg);
}
Я предварять размер объекта, чтобы сделать NIO играть красиво. Что мне здесь не хватает?
Хмм, я думал, что сделал это уже в цикле while – mish