use of org.apache.mina.core.buffer.IoBuffer in project camel by apache.
the class Mina2UdpProtocolCodecFactory method toIoBuffer.
private IoBuffer toIoBuffer(Object message) throws CharacterCodingException, NoTypeConversionAvailableException {
//try to convert it to a byte array
byte[] value = context.getTypeConverter().tryConvertTo(byte[].class, message);
if (value != null) {
IoBuffer answer = IoBuffer.allocate(value.length).setAutoExpand(true);
answer.put(value);
return answer;
}
// fallback to use a byte buffer converter
return context.getTypeConverter().mandatoryConvertTo(IoBuffer.class, message);
}
use of org.apache.mina.core.buffer.IoBuffer in project opennms by OpenNMS.
the class LineOrientedEncoder method encode.
/**
* {@inheritDoc}
*/
@Override
public void encode(final IoSession session, final Object message, final ProtocolEncoderOutput out) throws Exception {
final LineOrientedRequest request = (LineOrientedRequest) message;
if (request.getRequest().contains("null")) {
return;
}
CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
if (encoder == null) {
encoder = m_charset.newEncoder();
session.setAttribute(ENCODER, encoder);
}
final String value = request.getRequest();
IoBuffer buffer = null;
try {
buffer = IoBuffer.allocate(value.length()).setAutoExpand(true);
buffer.putString(request.getRequest(), encoder);
buffer.flip();
LOG.debug("Client sending: {}", value.trim());
out.write(buffer);
} finally {
if (buffer != null) {
buffer.free();
}
}
}
use of org.apache.mina.core.buffer.IoBuffer in project pancm_project by xuwujing.
the class MyTextLineCodecDecoder method decodeNormal.
private void decodeNormal(Context ctx, IoBuffer in, ProtocolDecoderOutput out) throws CharacterCodingException {
// 取出未完成任务中已经匹配的文本换行符的个数
int matchCount = ctx.getMatchCount();
// 设置匹配文本换行符的IoBuffer变量
if (delimBuf == null) {
IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true);
tmp.putString(delimiter, charset.newEncoder());
tmp.flip();
delimBuf = tmp;
}
// 解码的IoBuffer中数据的原始信息
// 输出值为0
int oldPos = in.position();
// 输出值为1
int oldLimit = in.limit();
logger.info("******************************************************************************");
logger.info("开始进入解码方法-----------------------------------------------------------------");
logger.info("");
logger.info("init Start--------------------------------------------------------------------");
logger.info("in.postion() = " + oldPos);
logger.info("in.Limit() = " + oldLimit);
logger.info("in.capacity() = " + in.capacity());
logger.info("matchCount = " + matchCount);
logger.info("init End---------------------------------------------------------------------");
logger.info("");
// 变量解码的IoBuffer
while (in.hasRemaining()) {
byte b = in.get();
logger.info("");
logger.info("输入进来的字符为 = " + (char) b + ",对应的ascii值 = " + b);
logger.info("in.position() = " + in.position() + ",in.limit() = " + in.limit());
logger.info("");
// 当b的ascii值为13,10 即为\r,\n时,会进入下述if语句
if (delimBuf.get(matchCount) == b) {
// b='\r'时,matchCount=1, b='\n'时,matchCount=2
matchCount++;
logger.info("matchCount = " + matchCount);
if (matchCount == delimBuf.limit()) {
// 获得当前匹配到的position(position前所有数据有效)
// 值为2
int pos = in.position();
logger.info("pos = " + pos);
// 值为2
in.limit(pos);
// position回到原始位置
// 值为0
in.position(oldPos);
// 追加到Context对象未完成数据后面
// 将 \r\n这两个字符添加到 ctx.getBuf()中
ctx.append(in);
// in中匹配结束后剩余数据
// 值为2
in.limit(oldLimit);
// 值为2
in.position(pos);
// 此时是得到 he\r\n
IoBuffer buf = ctx.getBuf();
// 此时 buf.position=0,buf.limit()=4
buf.flip();
// 4-2 = 2
buf.limit(buf.limit() - matchCount);
try {
// 输出解码内容 ,即 he
out.write(buf.getString(ctx.getDecoder()));
} finally {
// 释放缓存空间
buf.clear();
}
matchCount = 0;
}
} else {
// h字符,e字符时,均会进入 此else逻辑判断中
// 把in中未解码内容放回buf中
// 下面会在 输入的字符不是 \r\n时会需要保存使用
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
}
}
use of org.apache.mina.core.buffer.IoBuffer in project pancm_project by xuwujing.
the class MyTextLineCodecEncoder method encode.
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
logger.info("开始进入编码方法-----------------------------------------------------------------");
// 如果文本换行符未指定,使用默认值
if (delimiter == null || "".equals(delimiter)) {
delimiter = "\r\n";
}
if (charset == null) {
charset = Charset.forName("utf-8");
}
String value = message.toString();
IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true);
// 真实数据
buf.putString(value, charset.newEncoder());
// 文本换行符
buf.putString(delimiter, charset.newEncoder());
buf.flip();
out.write(buf);
}
use of org.apache.mina.core.buffer.IoBuffer in project openmeetings by apache.
the class ScreenV1Encoder method getData.
private static VideoData getData(byte[] data) {
IoBuffer buf = IoBuffer.allocate(data.length);
buf.clear();
buf.put(data);
buf.flip();
return new VideoData(buf);
}
Aggregations