本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.order方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.order方法的具體用法?Java IoBuffer.order怎麽用?Java IoBuffer.order使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.order方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: decodeData
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private Object decodeData ( final IoBuffer data ) throws ProtocolCodecException
{
data.order ( ByteOrder.LITTLE_ENDIAN );
final byte dataType = data.get ();
switch ( dataType )
{
case 0:
return null;
case 1:
return data.get () != 0x00;
case 2:
return data.getInt ();
case 3:
return data.getLong ();
case 4:
return data.getFloat ();
default:
throw new ProtocolCodecException ( String.format ( "Data type %02x is unknown", dataType ) );
}
}
示例3: encode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput output ) throws Exception
{
final IoBuffer data = IoBuffer.allocate ( 0 );
data.order ( ByteOrder.LITTLE_ENDIAN );
data.setAutoExpand ( true );
if ( message instanceof WriteRequestMessage )
{
encodeHeader ( data, (CommonMessage)message );
encodeWriteRequest ( data, (WriteRequestMessage)message );
}
else if ( message instanceof CommonMessage )
{
encodeHeader ( data, (CommonMessage)message );
}
data.flip ();
output.write ( data );
}
示例4: createMessage
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
protected IoBuffer createMessage ( final IoSession session, final byte command, final boolean longMessage )
{
final IoBuffer data = IoBuffer.allocate ( 3 );
data.setAutoExpand ( true );
if ( Sessions.isLittleEndian ( session ) )
{
data.order ( ByteOrder.LITTLE_ENDIAN );
}
data.put ( (byte)0x12 );
data.put ( (byte)0x02 );
data.put ( command );
if ( longMessage )
{
data.putShort ( (short)0 );
}
return data;
}
示例5: writeData
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void writeData ( final int blockAddress, byte[] data )
{
if ( this.request.getType () != RequestType.HOLDING )
{
throw new IllegalStateException ( String.format ( "Modbus can only write data when the block is of type %s", RequestType.HOLDING ) );
}
if ( data.length == 2 )
{
final IoBuffer buffer = IoBuffer.wrap ( data );
buffer.order ( this.dataOrder );
final int value = buffer.getUnsignedShort ();
this.slave.writeCommand ( new WriteSingleDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER, toWriteAddress ( blockAddress ), value ), this.request.getTimeout () );
}
else
{
data = ModbusProtocol.encodeData ( data, this.dataOrder ); // apply requested byte order
this.slave.writeCommand ( new WriteMultiDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS, toWriteAddress ( blockAddress ), data, data.length / 2 ), this.request.getTimeout () );
}
requestUpdate ();
}
示例6: processAnalogWrite
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void processAnalogWrite ( final int startAddress, final int numRegisters, final byte[] rawData )
{
final int[] regs = new int[numRegisters];
final IoBuffer data = IoBuffer.wrap ( rawData );
data.order ( this.order );
for ( int i = 0; i < numRegisters; i++ )
{
regs[i] = data.getUnsignedShort ();
}
handleAnalogWrite ( startAddress, regs );
}
示例7: makeReadReply
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
protected Object makeReadReply ( final BaseMessage baseMessage, final int[] data, final ByteOrder order )
{
final IoBuffer reply = IoBuffer.allocate ( data.length * 2 );
reply.order ( order );
for ( int i = 0; i < data.length; i++ )
{
reply.putUnsignedShort ( data[i] );
}
reply.flip ();
return new ReadResponse ( baseMessage.getTransactionId (), baseMessage.getUnitIdentifier (), baseMessage.getFunctionCode (), reply );
}
示例8: decodeTimeoutFrame
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void decodeTimeoutFrame ( final IoSession session, final IoBuffer currentFrame, final ProtocolDecoderOutput out )
{
logger.trace ( "timeout () frame = {}", currentFrame.getHexDump () );
if ( currentFrame.limit () <= Constants.RTU_HEADER_SIZE )
{
throw new ModbusProtocolError ( "frame must be at least 4 bytes long (address + data[] + crc low + crc high" );
}
currentFrame.order ( ByteOrder.LITTLE_ENDIAN );
final int receivedCrc = currentFrame.getUnsignedShort ( currentFrame.limit () - 2 );
currentFrame.order ( ByteOrder.BIG_ENDIAN );
final int actualCrc = Checksum.crc16 ( currentFrame.array (), 0, currentFrame.limit () - 2 );
if ( receivedCrc != actualCrc )
{
final String hd = currentFrame.getHexDump ();
logger.info ( "CRC error - received: {}, calculated: {} - data: {}", receivedCrc, actualCrc, hd );
throw new ChecksumProtocolException ( String.format ( "CRC error. received: %04x, but actually was: %04x - Data: %s", receivedCrc, actualCrc, hd ) );
}
currentFrame.position ( 0 );
// get unit id
final byte unitIdentifier = currentFrame.get ();
final int len = currentFrame.limit () - ( 2 /*crc*/+ 1/*unit id*/);
final IoBuffer pdu = IoBuffer.allocate ( len );
for ( int i = 0; i < len; i++ )
{
pdu.put ( currentFrame.get () );
}
pdu.flip ();
// decode and send
logger.trace ( "Decoded PDU - data: {}", pdu.getHexDump () );
out.write ( new Pdu ( 0, unitIdentifier, pdu ) );
}
示例9: encode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput out ) throws Exception
{
logger.debug ( "Encoding: {}", message );
final Pdu request = (Pdu)message;
final IoBuffer buffer = IoBuffer.allocate ( request.getData ().remaining () + 3 );
buffer.setAutoExpand ( true );
final IoBuffer pdu = request.getData ();
// put slave id
buffer.put ( request.getUnitIdentifier () );
// put data
buffer.put ( pdu );
// make and put crc
final int crc = Checksum.crc16 ( buffer.array (), 0, pdu.limit () + 1 ); // including slave address
buffer.order ( ByteOrder.LITTLE_ENDIAN );
buffer.putShort ( (short)crc );
buffer.flip ();
logger.trace ( "Encoded to: {}", buffer );
out.write ( buffer );
}
示例10: storeRemainingInSession
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void storeRemainingInSession(IoBuffer buf, IoSession session) {
final IoBuffer remainingBuf = IoBuffer.allocate(buf.capacity()).setAutoExpand(true);
remainingBuf.order(buf.order());
remainingBuf.put(buf);
session.setAttribute(BUFFER, remainingBuf);
}
示例11: storeRemainingInSession
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void storeRemainingInSession(IoBuffer buf, IoSession session) {
IoBuffer remainingBuf = IoBuffer.allocate(buf.capacity());
remainingBuf.setAutoExpand(true);
remainingBuf.order(buf.order());
remainingBuf.put(buf);
session.setAttribute(BUFFER, remainingBuf);
}
示例12: doDecode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
protected boolean doDecode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws Exception
{
byte marker1;
byte marker2;
do
{
if ( data.remaining () < 2 ) // we may only eat the start when there could be a packet after it
{
return false;
}
// peek marker
marker1 = data.get ( data.position () + 0 );
marker2 = data.get ( data.position () + 1 );
// TODO: re-think the idea of just skipping
if ( marker1 != 0x12 || marker2 != 0x02 )
{
data.skip ( 2 ); // eat garbage
}
} while ( marker1 != 0x12 || marker2 != 0x02 );
if ( data.remaining () < 3 )
{
return false;
}
data.order ( Sessions.isLittleEndian ( session ) ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN );
final byte command = data.get ( data.position () + 2 );
switch ( command )
{
case Messages.MC_HELLO:
return processHello ( session, data, out );
case Messages.MC_WELCOME:
return processWelcome ( session, data, out );
case Messages.MC_READ_ALL:
out.write ( new ReadAll () );
return true;
case Messages.MC_DATA_UPDATE:
return processDataUpdate ( session, data, out );
case Messages.MC_START_BROWSE:
out.write ( new SubscribeBrowse () );
return true;
case Messages.MC_STOP_BROWSE:
out.write ( new UnsubscribeBrowse () );
return true;
case Messages.MC_NS_ADDED:
return processBrowseAdded ( session, data, out );
case Messages.MC_WRITE_COMMAND:
return processWriteCommand ( session, data, out );
case Messages.MC_WRITE_RESULT:
return processWriteResult ( session, data, out );
}
throw new ProtocolCodecException ( String.format ( "Message code %02x is unkown", command ) );
}