本文整理汇总了Java中org.apache.mina.core.buffer.IoBuffer.remaining方法的典型用法代码示例。如果您正苦于以下问题:Java IoBuffer.remaining方法的具体用法?Java IoBuffer.remaining怎么用?Java IoBuffer.remaining使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.remaining方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertData
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Encode the data from Java byte order to requested modbus byte order
*
* @param data
* the data to encode
* @param dataOrder
* the target modbus byte order
* @return the converted data, or the input data if no conversion was
* necessary
*/
public static IoBuffer convertData ( final IoBuffer data, final ByteOrder dataOrder )
{
if ( dataOrder == ByteOrder.BIG_ENDIAN )
{
return data;
}
final IoBuffer result = IoBuffer.allocate ( data.capacity () );
result.order ( dataOrder );
for ( int i = 0; i < data.remaining () / 2; i++ )
{
// convert to LITTLE_ENDIAN
result.putUnsignedShort ( data.getUnsignedShort ( i * 2 ) );
}
// the byte order we use is BIG_ENDIAN
result.order ( ByteOrder.BIG_ENDIAN );
return result;
}
示例2: decode
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public synchronized void decode ( final IoSession session, final IoBuffer in, final ProtocolDecoderOutput out ) throws Exception
{
IoBuffer currentFrame = (IoBuffer)session.getAttribute ( SESSION_KEY_CURRENT_FRAME );
if ( currentFrame == null )
{
currentFrame = IoBuffer.allocate ( Constants.MAX_PDU_SIZE + Constants.RTU_HEADER_SIZE );
session.setAttribute ( SESSION_KEY_CURRENT_FRAME, currentFrame );
}
logger.trace ( "decode () current frame = {} data = {}", currentFrame.toString (), currentFrame.getHexDump () );
logger.trace ( "decode () new frame = {} data = {}", in.toString (), in.getHexDump () );
final int expectedSize = currentFrame.position () + in.remaining ();
if ( expectedSize > MAX_SIZE + 1 )
{
throw new ModbusProtocolError ( String.format ( "received size (%s) exceeds max size (%s)", expectedSize, MAX_SIZE ) );
}
currentFrame.put ( in );
tick ( session, out );
}
示例3: insertBytesToNewIoBuffer
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private IoBuffer insertBytesToNewIoBuffer(IoSession session, IoBuffer buffer) {
if (insertByteProbability > rng.nextInt(1000)) {
logger.info(buffer.getHexDump());
// where to insert bytes ?
int pos = rng.nextInt(buffer.remaining()) - 1;
// how many byte to insert ?
int count = rng.nextInt(maxInsertByte - 1) + 1;
IoBuffer newBuff = IoBuffer.allocate(buffer.remaining() + count);
for (int i = 0; i < pos; i++)
newBuff.put(buffer.get());
for (int i = 0; i < count; i++) {
newBuff.put((byte) (rng.nextInt(256)));
}
while (buffer.remaining() > 0) {
newBuff.put(buffer.get());
}
newBuff.flip();
logger.info("Inserted " + count + " bytes.");
logger.info(newBuff.getHexDump());
return newBuff;
}
return null;
}
示例4: write
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Writes <code>data</code> {@link IoBuffer} to the <code>buf</code>
* {@link IoBuffer} which buffers write requests for the
* <code>session</code> {@ link IoSession} until buffer is full
* or manually flushed.
*
* @param session the session where buffer will be written
* @param data the data to buffer
* @param buf the buffer where data will be temporarily written
*/
private void write(IoSession session, IoBuffer data, IoBuffer buf) {
try {
int len = data.remaining();
if (len >= buf.capacity()) {
/*
* If the request length exceeds the size of the output buffer,
* flush the output buffer and then write the data directly.
*/
NextFilter nextFilter = session.getFilterChain().getNextFilter(this);
internalFlush(nextFilter, session, buf);
nextFilter.filterWrite(session, new DefaultWriteRequest(data));
return;
}
if (len > (buf.limit() - buf.position())) {
internalFlush(session.getFilterChain().getNextFilter(this), session, buf);
}
synchronized (buf) {
buf.put(data);
}
} catch (Throwable e) {
session.getFilterChain().fireExceptionCaught(e);
}
}
示例5: messageLength
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private int messageLength ( final IoBuffer data )
{
final int len = data.getUnsignedShort ( data.position () + 3 );
if ( data.remaining () < 5 + len )
{
return -1;
}
data.skip ( 5 );
return len;
}
示例6: writeBuffer
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private int writeBuffer(S session, WriteRequest req, boolean hasFragmentation, int maxLength, long currentTime)
throws Exception {
IoBuffer buf = (IoBuffer) req.getMessage();
int localWrittenBytes = 0;
if (buf.hasRemaining()) {
int length;
if (hasFragmentation) {
length = Math.min(buf.remaining(), maxLength);
} else {
length = buf.remaining();
}
try {
localWrittenBytes = write(session, buf, length);
} catch (IOException ioe) {
// We have had an issue while trying to send data to the
// peer : let's close the session.
session.close(true);
}
}
session.increaseWrittenBytes(localWrittenBytes, currentTime);
if (!buf.hasRemaining() || (!hasFragmentation && (localWrittenBytes != 0))) {
// Buffer has been sent, clear the current request.
int pos = buf.position();
buf.reset();
fireMessageSent(session, req);
// And set it back to its position
buf.position(pos);
}
return localWrittenBytes;
}
示例7: decodeAsSlave
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Decode a PDU as a slave
*
* @param message
* the message PDU
* @return the decoded messages
* @throws IllegalStateException
* if the function code is not supported
*/
public static Object decodeAsSlave ( final Pdu message ) throws ProtocolCodecException
{
final IoBuffer data = message.getData ();
final byte functionCode = data.get ();
switch ( functionCode )
{
case Constants.FUNCTION_CODE_READ_COILS:
case Constants.FUNCTION_CODE_READ_DISCRETE_INPUTS:
case Constants.FUNCTION_CODE_READ_HOLDING_REGISTERS:
case Constants.FUNCTION_CODE_READ_INPUT_REGISTERS:
return new ReadRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), data.getUnsignedShort () );
case Constants.FUNCTION_CODE_WRITE_SINGLE_COIL:
case Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER:
return new WriteSingleDataRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), readBytes ( data, 2 ) );
case Constants.FUNCTION_CODE_WRITE_MULTIPLE_COILS:
case Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS:
final int startAddress = data.getUnsignedShort ();
final int numRegisters = data.getUnsignedShort (); /* number of registers */
final byte num = data.get ();
if ( data.remaining () != num )
{
throw new ProtocolCodecException ( String.format ( "Wrong amount of data. Announced %s (bytes), found %s (bytes)", num, data.remaining () ) );
}
final byte[] b = new byte[data.remaining ()];
data.get ( b );
return new WriteMultiDataRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, startAddress, b, numRegisters );
default:
throw new IllegalStateException ( String.format ( "Function code %02x is not supported", functionCode ) );
}
}
示例8: doDecode
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode(IoSession session, IoBuffer in, 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) {
int packetHeader = in.getInt();
if (!client.getReceiveCrypto().checkPacket(packetHeader)) {
session.close(true);
return false;
}
decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
} else if (in.remaining() < 4 && decoderState.packetlength == -1) {
return false;
}
if (in.remaining() >= decoderState.packetlength) {
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;
}
示例9: discard
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void discard(IoBuffer in) {
if (Integer.MAX_VALUE - in.remaining() < overflowPosition) {
overflowPosition = Integer.MAX_VALUE;
} else {
overflowPosition += in.remaining();
}
in.position(in.limit());
}
示例10: decodable
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public MessageDecoderResult decodable(IoSession session, IoBuffer in)
{
Context context = (Context) session.getAttribute(CONTEXT);
if(context!=null){
if(context.limit_data){
in.buf().position(0);
}
}
//表示数据不够,需要读到新的数据后,再次调用decode()方法。
if (in.remaining() < 2){
return MessageDecoderResult.NEED_DATA;
}
else{
context = new Context();
//获取一个字符表示新的数据开始 用于判断异常数据
in.skip(1);//这个数据是十六进制的 01 也就是1
//获取第一个字符用于判断是否可以被当前解码器解码
context.dataType = in.getInt();
if(context.dataType == BeanUtil.UPLOAD_STR){
//读取标题长度
context.strLength = in.getInt();
//声明数组长度
context.byteStr = new byte[context.strLength];
//System.out.println("我收到2了");
session.setAttribute(CONTEXT, context);
//表示可以解码
return MessageDecoderResult.OK;
}else{
//System.out.println("服务端收到意外数据");
return MessageDecoderResult.NOT_OK;
}
}
}
示例11: deflate
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Compress the input. The result will be put in a new buffer.
*
* @param inBuffer the buffer to be compressed. The contents are transferred
* into a local byte array and the buffer is flipped and returned intact.
* @return the buffer with the compressed data
* @throws IOException if the compression of teh buffer failed for some reason
* @throws IllegalStateException if the mode is not <code>MODE_DEFLATER</code>
*/
public IoBuffer deflate(IoBuffer inBuffer) throws IOException {
if (mode == MODE_INFLATER) {
throw new IllegalStateException("not initialized as DEFLATER");
}
byte[] inBytes = new byte[inBuffer.remaining()];
inBuffer.get(inBytes).flip();
// according to spec, destination buffer should be 0.1% larger
// than source length plus 12 bytes. We add a single byte to safeguard
// against rounds that round down to the smaller value
int outLen = (int) Math.round(inBytes.length * 1.001) + 1 + 12;
byte[] outBytes = new byte[outLen];
synchronized (zStream) {
zStream.next_in = inBytes;
zStream.next_in_index = 0;
zStream.avail_in = inBytes.length;
zStream.next_out = outBytes;
zStream.next_out_index = 0;
zStream.avail_out = outBytes.length;
int retval = zStream.deflate(JZlib.Z_SYNC_FLUSH);
if (retval != JZlib.Z_OK) {
outBytes = null;
inBytes = null;
throw new IOException("Compression failed with return value : " + retval);
}
IoBuffer outBuf = IoBuffer.wrap(outBytes, 0, zStream.next_out_index);
return outBuf;
}
}
示例12: getValue
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public Variant getValue ( final int localOffset, final IoBuffer value )
{
if ( localOffset == AbstractSourceType.COMMON_HEADER && value.remaining () == DATA_LENGTH )
{
return Variant.valueOf ( value.getUnsignedInt () );
}
return null;
}
示例13: messageReceived
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Handle incoming data during the handshake process. Should consume only the
* handshake data from the buffer, leaving any extra data in place.
*
* @param nextFilter the next filter
* @param buf the server response data buffer
*/
public void messageReceived(final NextFilter nextFilter, final IoBuffer buf) {
try {
if (buf.remaining() >= SocksProxyConstants.SOCKS_4_RESPONSE_SIZE) {
handleResponse(buf);
}
} catch (Exception ex) {
closeSession("Proxy handshake failed: ", ex);
}
}
示例14: getValue
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public Variant getValue ( final int localOffset, final IoBuffer value )
{
if ( localOffset == AbstractSourceType.COMMON_HEADER && value.remaining () == DATA_LENGTH )
{
return Variant.valueOf ( value.getShort () );
}
return null;
}
示例15: getValue
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public Variant getValue ( final int localOffset, final IoBuffer value )
{
if ( localOffset == AbstractSourceType.COMMON_HEADER && value.remaining () == DATA_LENGTH )
{
return Variant.valueOf ( value.getUnsignedShort () );
}
return null;
}