java.nio.charset.MalformedInputException: Input length = 2报错信息
重写CumulativeProtocolDecoder中的doDecode方法。具体代码如下:
package com.xlx.credit.util;
import java.nio.charset.Charset;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
public class FlightDecoder extends CumulativeProtocolDecoder {
private final Charset charset = Charset.forName("UTF-8");
@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
int n = in.limit();
StringBuffer sb = new StringBuffer(512);
byte [] sizeBytes = null;
if(in.hasRemaining()){
sizeBytes = new byte[n];
in.get(sizeBytes);
buf.flip();
sb.append(new String(sizeBytes, charset));
}
out.write(sb.toString());
return false;
}
}