前些时候将mina1.0升级到mina2.0版本,在windows下测试没问题,可是迁移到AIX下发现报文截长度不对,只能截1024长度,使得所有超过1024的报文都出错.没办法只能查看MINA2.0源码,发现截报文的时候默认取I/O的长度,而且只取一次.如果要取超过超过默认的长度时,有二种方法:1,先告诉它你的报文长度,然后分断,将每次取的内容连接起来.
具体操作:
package com.erayt.utils.mina.codec;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import java.nio.charset.Charset;
public class FixedStringCodecFactory implements ProtocolCodecFactory {
private final FixedStringEncoder encoder;
private final FixedStringDecoder decoder;
public FixedStringCodecFactory(Charset charset) {
encoder = new FixedStringEncoder(charset);
decoder = new FixedStringDecoder(charset);
}
public FixedStringCodecFactory() {
this(Charset.defaultCharset());
}
public int getEncoderMaxDataLength() {
return encoder.getMaxDataLength();
}
public void setEncoderMaxDataLength(int maxDataLength) {
encoder.setMaxDataLength(maxDataLength);
}
public int getDecoderMaxDataLength() {
return decoder.getMaxDataLength();
}
public void setDecoderMaxDataLength(int maxDataLength) {
decoder.setMaxDataLength(maxDataLength);
}
public void setDecoderPrefixLength(int prefixLength) {
decoder.setPrefixLength(prefixLength);
}
public int getDecoderPrefixLength() {
return decoder.getPrefixLength();
}
public void setEncoderPrefixLength(int prefixLength) {
encoder.setPrefixLength(prefixLength);
}
public int getEncoderPrefixLength() {
return encoder.getPrefixLength();
}
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
return encoder;
}
public ProtocolDecoder getDecoder(IoSession session) throws Exception {
return decoder;
}
}
新增一个编码器:
具体操作:
package com.erayt.utils.mina.codec;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import java.nio.charset.Charset;
public class FixedStringCodecFactory implements ProtocolCodecFactory {
private final FixedStringEncoder encoder;
private final FixedStringDecoder decoder;
public FixedStringCodecFactory(Charset charset) {
encoder = new FixedStringEncoder(charset);
decoder = new FixedStringDecoder(charset);
}
public FixedStringCodecFactory() {
this(Charset.defaultCharset());
}
public int getEncoderMaxDataLength() {
return encoder.getMaxDataLength();
}
public void setEncoderMaxDataLength(int maxDataLength) {
encoder.setMaxDataLength(maxDataLength);
}
public int getDecoderMaxDataLength() {
return decoder.getMaxDataLength();
}
public void setDecoderMaxDataLength(int maxDataLength) {
decoder.setMaxDataLength(maxDataLength);
}
public void setDecoderPrefixLength(int prefixLength) {
decoder.setPrefixLength(prefixLength);
}
public int getDecoderPrefixLength() {
return decoder.getPrefixLength();
}
public void setEncoderPrefixLength(int prefixLength) {
encoder.setPrefixLength(prefixLength);
}
public int getEncoderPrefixLength() {
return encoder.getPrefixLength();
}
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
return encoder;
}
public ProtocolDecoder getDecoder(IoSession session) throws Exception {
return decoder;
}
}
新增一个编码器: