本文整理汇总了Java中org.apache.mina.common.ByteBuffer.remaining方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.remaining方法的具体用法?Java ByteBuffer.remaining怎么用?Java ByteBuffer.remaining使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.common.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.remaining方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
public MessageDecoderResult decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out)
throws ProtocolCodecException {
int messageCount = 0;
while (parseMessage(in, out)) {
messageCount++;
}
if (messageCount > 0) {
// Mina will compact the buffer because we can't detect a header
if (in.remaining() < HEADER_PATTERN.length) {
position = 0;
}
return MessageDecoderResult.OK;
} else {
// Mina will compact the buffer
position -= in.position();
return MessageDecoderResult.NEED_DATA;
}
}
示例2: doDecode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception {
if (in.remaining() >= PAYLOAD_SIZE) {
byte[] buf = new byte[in.remaining()];
in.get(buf);
// first 7 bytes are the sensor ID, last is the status
// and the result message will look something like
// MachineID=2371748;Status=Good
StringBuilder sb = new StringBuilder();
sb.append("MachineID=")
.append(new String(buf, 0, PAYLOAD_SIZE - 1)).append(";")
.append("Status=");
if (buf[PAYLOAD_SIZE - 1] == '1') {
sb.append("Good");
} else {
sb.append("Failure");
}
out.write(sb.toString());
return true;
} else {
return false;
}
}
示例3: calculateContentBodyFrameCount
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
private int calculateContentBodyFrameCount(ByteBuffer payload)
{
// we substract one from the total frame maximum size to account for the end of frame marker in a body frame
// (0xCE byte).
int frameCount;
if ((payload == null) || (payload.remaining() == 0))
{
frameCount = 0;
}
else
{
int dataLength = payload.remaining();
final long framePayloadMax = _session.getAMQConnection().getMaximumFrameSize() - 1;
int lastFrame = ((dataLength % framePayloadMax) > 0) ? 1 : 0;
frameCount = (int) (dataLength / framePayloadMax) + lastFrame;
}
return frameCount;
}
示例4: extractByteMessageContent
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
/**
* Extract ByteMessage from ByteBuffer
*
* @param wrapMsgContent ByteBuffer which contains data
* @param byteMsgContent byte[] of message content
* @return extracted content as text
*/
private String extractByteMessageContent(ByteBuffer wrapMsgContent, byte[] byteMsgContent) {
String wholeMsg;
if (byteMsgContent == null) {
throw new IllegalArgumentException("byte array must not be null");
}
int count = (wrapMsgContent.remaining() >= byteMsgContent.length ?
byteMsgContent.length :
wrapMsgContent.remaining());
if (count == 0) {
wholeMsg = String.valueOf(-1);
} else {
wrapMsgContent.get(byteMsgContent, 0, count);
wholeMsg = String.valueOf(count);
}
return wholeMsg;
}
示例5: doDecode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
/**
* Delegates decoding AMQP from the data buffer that Mina has retrieved from the wire, to the data or protocol
* intiation decoders.
*
* @param session The Mina session.
* @param in The raw byte buffer.
* @param out The Mina object output gatherer to write decoded objects to.
*
* @return <tt>true</tt> if the data was decoded, <tt>false<tt> if more is needed and the data should accumulate.
*
* @throws Exception If the data cannot be decoded for any reason.
*/
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception
{
boolean decoded;
if (_expectProtocolInitiation
|| (firstDecode
&& (in.remaining() > 0)
&& (in.get(in.position()) == (byte)'A')))
{
decoded = doDecodePI(session, in, out);
}
else
{
decoded = doDecodeDataBlock(session, in, out);
}
if(firstDecode && decoded)
{
firstDecode = false;
}
return decoded;
}
示例6: toString
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
/**
* Turns a <code>org.apache.mina.common.ByteBuffer</code> into a hexadecimal
* string.
*
* @param buf The <code>org.apache.mina.common.ByteBuffer</code> to convert.
* @return The hexadecimal representation of <code>buf</code>
*/
public static String toString(final ByteBuffer buf) {
buf.flip();
final byte arr[] = new byte[buf.remaining()];
buf.get(arr);
String ret = toString(arr);
buf.flip();
buf.put(arr);
return ret;
}
示例7: doDecode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception {
final DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);
/* if (decoderState == null) {
decoderState = new DecoderState();
session.setAttribute(DECODER_STATE_KEY, decoderState);
}*/
final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
if (decoderState != null) {
if (decoderState.packetlength == -1) {
if (in.remaining() >= 4) {
final int packetHeader = in.getInt();
if (!client.getReceiveCrypto().checkPacket(packetHeader)) {
session.close();
return false;
}
decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
} else {
return false;
}
}
if (in.remaining() >= decoderState.packetlength) {
final byte decryptedPacket[] = new byte[decoderState.packetlength];
in.get(decryptedPacket, 0, decoderState.packetlength);
decoderState.packetlength = -1;
client.getReceiveCrypto().crypt(decryptedPacket);
// MapleCustomEncryption.decryptData(decryptedPacket);
out.write(decryptedPacket);
return true;
}
}
return false;
}
示例8: toString
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
/**
* Turns a {@link org.apache.mina.common.ByteBuffer ByteBuffer}
* into a hexadecimal string.
*
* @param buf The <code>ByteBuffer</code> to convert.
* @return The hexadecimal representation of <code>buf</code>.
*/
public static String toString(final ByteBuffer buf) {
buf.flip();
final byte[] arr = new byte[buf.remaining()];
buf.get(arr);
final String ret = toString(arr);
buf.flip();
buf.put(arr);
return ret;
}
示例9: doDecode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode(final IoSession session, final ByteBuffer in, final ProtocolDecoderOutput out) throws Exception {
final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);
if (decoderState == null) {
decoderState = new DecoderState();
session.setAttribute(DECODER_STATE_KEY, decoderState);
}
if (in.remaining() >= 4 && decoderState.packetlength == -1) {
final int packetHeader = in.getInt();
if (!client.getReceiveCrypto().checkPacket(packetHeader)) {
System.err.println(MapleClient.getLogMessage(client, "Client failed packet check -> disconnecting"));
session.close();
return false;
}
decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
} else if (in.remaining() < 4 && decoderState.packetlength == -1) {
//System.err.println("decode... not enough data");
return false;
}
if (in.remaining() >= decoderState.packetlength) {
final byte[] decryptedPacket = new byte[decoderState.packetlength];
in.get(decryptedPacket, 0, decoderState.packetlength);
decoderState.packetlength = -1;
client.getReceiveCrypto().crypt(decryptedPacket);
MapleCustomEncryption.decryptData(decryptedPacket);
out.write(decryptedPacket);
return true;
} else {
//System.err.println("decode... not enough data to decode (need " + decoderState.packetlength + ")");
return false;
}
}
示例10: toByteArray
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
@Converter
public static byte[] toByteArray(ByteBuffer buffer) {
buffer.mark();
try {
byte[] answer = new byte[buffer.remaining()];
buffer.get(answer);
return answer;
} finally {
buffer.reset();
}
}
示例11: getDecoder
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
public ProtocolDecoder getDecoder() throws Exception {
return new CumulativeProtocolDecoder() {
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception {
if (in.remaining() > 0) {
byte[] buf = new byte[in.remaining()];
in.get(buf);
out.write(new String(buf, "US-ASCII"));
return true;
} else {
return false;
}
}
};
}
示例12: doDecode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception {
final DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);
/* if (decoderState == null) {
decoderState = new DecoderState();
session.setAttribute(DECODER_STATE_KEY, decoderState);
}*/
final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
if (decoderState.packetlength == -1) {
if (in.remaining() >= 4) {
final int packetHeader = in.getInt();
if (!client.getReceiveCrypto().checkPacket(packetHeader)) {
session.close();
return false;
}
decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
} else {
return false;
}
}
if (in.remaining() >= decoderState.packetlength) {
final byte decryptedPacket[] = new byte[decoderState.packetlength];
in.get(decryptedPacket, 0, decoderState.packetlength);
decoderState.packetlength = -1;
client.getReceiveCrypto().crypt(decryptedPacket);
//MapleCustomEncryption.decryptData(decryptedPacket); // removed in v149.2
out.write(decryptedPacket);
return true;
}
return false;
}
示例13: decode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception {
int readable = in.limit();
if (readable <= 0) return;
ChannelBuffer frame;
if (buffer.readable()) {
if (buffer instanceof DynamicChannelBuffer) {
buffer.writeBytes(in.buf());
frame = buffer;
} else {
int size = buffer.readableBytes() + in.remaining();
frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize);
frame.writeBytes(buffer, buffer.readableBytes());
frame.writeBytes(in.buf());
}
} else {
frame = ChannelBuffers.wrappedBuffer(in.buf());
}
Channel channel = MinaChannel.getOrAddChannel(session, url, handler);
Object msg;
int savedReadIndex;
try {
do {
savedReadIndex = frame.readerIndex();
try {
msg = codec.decode(channel, frame);
} catch (Exception e) {
buffer = ChannelBuffers.EMPTY_BUFFER;
throw e;
}
if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) {
frame.readerIndex(savedReadIndex);
break;
} else {
if (savedReadIndex == frame.readerIndex()) {
buffer = ChannelBuffers.EMPTY_BUFFER;
throw new Exception("Decode without read data.");
}
if (msg != null) {
out.write(msg);
}
}
} while (frame.readable());
} finally {
if (frame.readable()) {
frame.discardReadBytes();
buffer = frame;
} else {
buffer = ChannelBuffers.EMPTY_BUFFER;
}
MinaChannel.removeChannelIfDisconnectd(session);
}
}
示例14: doDecode
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out)
throws Exception {
final int origonPos = in.position();
final int packetLength = in.remaining();
if(packetLength<PROTOCOL_HEADER_LENGTH){
in.position(origonPos);
return false;
}
int flag = in.getInt();
int chid = in.getInt();
int pcode = in.getInt();
int len = in.getInt();
if (flag != TairConstant.TAIR_PACKET_FLAG)
throw new IOException("flag error, except: " + TairConstant.TAIR_PACKET_FLAG + ", but get " + flag);
if (in.remaining() < len) {
in.position(origonPos);
return false;
}
if(isDebugEnabled){
final String remoteIP = ((InetSocketAddress) session.getRemoteAddress()).toString();
StringBuilder receiveTimeInfo = new StringBuilder();
receiveTimeInfo.append("receive response from [").append(remoteIP).append("],time is: ");
receiveTimeInfo.append(System.currentTimeMillis());
receiveTimeInfo.append(", channel id: ").append(chid);
LOGGER.debug(receiveTimeInfo.toString());
}
byte[] data = new byte[len];
in.get(data);
if (isDebugEnabled)
LOGGER.debug(TairUtil.toHex(data));
BasePacket returnPacket = pstreamer.decodePacket(pcode, data);
TairResponse response=new TairResponse();
response.setRequestId(chid);
response.setResponse(returnPacket);
out.write(response);
return true;
}
示例15: getBufferDebugInfo
import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
private String getBufferDebugInfo(ByteBuffer in) {
return "pos=" + in.position() + ",lim=" + in.limit() + ",rem=" + in.remaining()
+ ",offset=" + position + ",state=" + state;
}