本文整理匯總了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;
}