Here I am reading Binary data, but this gives me "java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.mina.core.buffer.IoBuffer"
IoBuffer buffer = (IoBuffer) message; // Exception at This line
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName("ISO-8859-1"))));
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
IoBuffer buffer = (IoBuffer) message;
b = new byte[buffer.remaining()];
buffer.get(b);
}
This issue as it assigned string values to IOBuffer IoBuffer buffer = (IoBuffer) message;
. You can't assign directly. You have to use putString
method for it. I have added little sample. I don't know what you are doing but this may help you.
String message=(String)message;
IoBuffer buf = IoBuffer.allocate(message.length()).setAutoExpand(true);
buf.putString(message, ENCODER);
I have Done like this:
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName("ISO-8859-1"))));
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
byte[] b = new byte[100];
b = message.toString().getBytes(Charset.forName("ISO-8859-1"));// Read Binary Message
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.