我正在尝试将对象从客户端(普通 IO)传输到服务器(NIO)。传输工作正常,直到对象达到一定大小,然后服务器将抛出“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 可以很好地发挥作用。我在这里想念什么?