怎样使用Java网络通讯框架Mina自定义消息格式( 三 )
import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.IOException;import javax.imageio.ImageIO;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 ImageResponseDecoder extends CumulativeProtocolDecoder {\tprivate static final String DECODER_STATE_KEY = ImageResponseDecoder.class.getName() + ".STATE";// 存储decoding的进度\tpublic static final int MAX_IMAGE_SIZE = 5 * 1024 * 1024;\tprivate static class DecoderState {\t\tBufferedImage image1;\t}\tprotected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {\t\tDecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);\t\tif (decoderState == null) {\t\t\tdecoderState = new DecoderState();\t\t\tsession.setAttribute(DECODER_STATE_KEY, decoderState);\t\t}\t\tif (decoderState.image1 == null) {\t\t\t// try to read firstimage\t\t\tif (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {// 这个方法对于有长度前缀的message解析很好用\t\t\t\tdecoderState.image1 = readImage(in);\t\t\t} else {\t\t\t\t// not enough dataavailable to read first image\t\t\t\treturn false;\t\t\t}\t\t}\t\tif (decoderState.image1 != null) {\t\t\t// try to read second image\t\t\tif (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {\t\t\t\tBufferedImage image2 = readImage(in);\t\t\t\tImageResponse imageResponse = new ImageResponse(decoderState.image1, image2);\t\t\t\tout.write(imageResponse);\t\t\t\tdecoderState.image1 = null;\t\t\t\treturn true;\t\t\t} else {\t\t\t\t// not enough dataavailable to read second image\t\t\t\treturn false;\t\t\t}\t\t}\t\treturn false;\t}\tprivate BufferedImage readImage(IoBuffer in) throws IOException {\t\tint length = in.getInt();\t\tbyte bytes = new byte;\t\tin.get(bytes);\t\tByteArrayInputStream bais = new ByteArrayInputStream(bytes);\t\tSystem.out.println(String.format("readImage@length=%d", length));\t\treturn ImageIO.read(bais);\t}}import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolEncoderAdapter;import org.apache.mina.filter.codec.ProtocolEncoderOutput;public class ImageResponseEncoder extends ProtocolEncoderAdapter {\tpublic void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {\t\tImageResponse imageResponse = (ImageResponse) message;\t\tbyte bytes1 = getBytes(imageResponse.getImage1());\t\tbyte bytes2 = getBytes(imageResponse.getImage2());\t\tint capacity = bytes1.length + bytes2.length + 8;\t\tIoBuffer buffer = IoBuffer.allocate(capacity, false);\t\tbuffer.setAutoExpand(true);// 设置自动扩充\t\tbuffer.putInt(bytes1.length);\t\tbuffer.put(bytes1);\t\tbuffer.putInt(bytes2.length);\t\tbuffer.put(bytes2);\t\tSystem.out.println(String.format("encode@bytes1_length=%d|bytes1_length=%d", bytes1.length,\t\t\t\tbytes2.length));\t\tbuffer.flip();\t\tout.write(buffer);\t}\tprivate byte getBytes(BufferedImage image) throws IOException {\t\tByteArrayOutputStream baos = new ByteArrayOutputStream();\t\tImageIO.write(image, "PNG", baos);\t\treturn baos.toByteArray();\t}}
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 『先进』长江流域最先进洗舱站在宁投运 油可分离回收,水能循环使用
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
- 怎样评价扶他柠檬茶的小说《云养汉》的结尾
- 汽车|迈凯伦Artura不再使用迈凯伦祖传V8引擎了?
- 怎样成为一名合格的Python程序员?
