本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.clear方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.clear方法的具體用法?Java IoBuffer.clear怎麽用?Java IoBuffer.clear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.clear方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: internalFlush
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Internal method that actually flushes the buffered data.
*
* @param nextFilter the {@link NextFilter} of this filter
* @param session the session where buffer will be written
* @param buf the data to write
* @throws Exception if a write operation fails
*/
private void internalFlush(NextFilter nextFilter, IoSession session, IoBuffer buf) throws Exception {
IoBuffer tmp = null;
synchronized (buf) {
buf.flip();
tmp = buf.duplicate();
buf.clear();
}
logger.debug("Flushing buffer: {}", tmp);
nextFilter.filterWrite(session, new DefaultWriteRequest(tmp));
}
示例2: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
//必須保證每一個數據包的字節緩存都和session綁定在一起,不然就讀取不了上一次剩餘的數據了
CodecContext context = SessionManager.INSTANCE.getSessionAttr(session, SessionProperties.CODEC_CONTEXT, CodecContext.class);
if (context == null) {
context = new CodecContext();
session.setAttribute(SessionProperties.CODEC_CONTEXT, context);
}
IoBuffer ioBuffer = context.getBuffer();
ioBuffer.put(in);
//在循環裏迭代,以處理數據粘包
for (; ;) {
ioBuffer.flip();
//消息元信息常量4表示消息body前麵的兩個short字段,一個表示moduel,一個表示cmd,
final int METE_SIZE = 4;
if (ioBuffer.remaining() < METE_SIZE) {
ioBuffer.compact();
return;
}
//----------------消息協議格式-------------------------
// packetLength | moduleId | cmd | body
// int short short byte[]
int length = ioBuffer.getInt();
//int packLen = length + 4;
//大於消息body長度,說明至少有一條完整的message消息
if (ioBuffer.remaining() >= length) {
short moduleId = ioBuffer.getShort();
short cmd = ioBuffer.getShort();
byte[] body = new byte[length-METE_SIZE];
ioBuffer.get(body);
Message msg = readMessage(moduleId, cmd, body);
if (moduleId > 0) {
out.write(msg);
} else { //屬於組合包
CombineMessage combineMessage = (CombineMessage)msg;
List<Packet> packets = combineMessage.getPackets();
for (Packet packet :packets) {
//依次拆包反序列化為具體的Message
out.write(Packet.asMessage(packet));
}
}
if (ioBuffer.remaining() == 0) {
ioBuffer.clear();
break;
}
ioBuffer.compact();
} else{
//數據包不完整,繼續等待數據到達
ioBuffer.rewind();
ioBuffer.compact();
break;
}
}
}
示例3: decodeAuto
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Decode a line using the default delimiter on the current system
*/
private void decodeAuto(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException, ProtocolDecoderException {
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
while (in.hasRemaining()) {
byte b = in.get();
boolean matched = false;
switch (b) {
case '\r':
// Might be Mac, but we don't auto-detect Mac EOL
// to avoid confusion.
matchCount++;
break;
case '\n':
// UNIX
matchCount++;
matched = true;
break;
default:
matchCount = 0;
}
if (matched) {
// Found a match.
int pos = in.position();
in.limit(pos);
in.position(oldPos);
ctx.append(in);
in.limit(oldLimit);
in.position(pos);
if (ctx.getOverflowPosition() == 0) {
IoBuffer buf = ctx.getBuffer();
buf.flip();
buf.limit(buf.limit() - matchCount);
try {
byte[] data = new byte[buf.limit()];
buf.get(data);
CharsetDecoder decoder = ctx.getDecoder();
CharBuffer buffer = decoder.decode(ByteBuffer.wrap(data));
String str = new String(buffer.array());
writeText(session, str, out);
} finally {
buf.clear();
}
} else {
int overflowPosition = ctx.getOverflowPosition();
ctx.reset();
throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
}
oldPos = pos;
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
示例4: decodeNormal
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Decode a line using the delimiter defined by the caller
*/
private void decodeNormal(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException, ProtocolDecoderException {
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
while (in.hasRemaining()) {
byte b = in.get();
if (delimBuf.get(matchCount) == b) {
matchCount++;
if (matchCount == delimBuf.limit()) {
// Found a match.
int pos = in.position();
in.limit(pos);
in.position(oldPos);
ctx.append(in);
in.limit(oldLimit);
in.position(pos);
if (ctx.getOverflowPosition() == 0) {
IoBuffer buf = ctx.getBuffer();
buf.flip();
buf.limit(buf.limit() - matchCount);
try {
writeText(session, buf.getString(ctx.getDecoder()), out);
} finally {
buf.clear();
}
} else {
int overflowPosition = ctx.getOverflowPosition();
ctx.reset();
throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
}
oldPos = pos;
matchCount = 0;
}
} else {
// fix for DIRMINA-506 & DIRMINA-536
in.position(Math.max(0, in.position() - matchCount));
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
示例5: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void decode ( final IoSession session, final IoBuffer in, final ProtocolDecoderOutput out ) throws Exception
{
in.clear ();
}