本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.allocate方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.allocate方法的具體用法?Java IoBuffer.allocate怎麽用?Java IoBuffer.allocate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.allocate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: send
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/***
* Sends a msgpack message to the provider.
* @param val msgpack encoded message
*/
private synchronized void send(Value val) {
logger.debug("Sending packet", val);
if (!session.isConnected())
logger.warn("Trying to send when session is not connected.");
Packer packer = Packer.getPacker();
try {
packer.packValue(val);
} catch (IOException e) {
throw new RuntimeException("Failed to pack value");
}
byte[] encodedPacket = packer.toByteArray();
IoBuffer buffer = IoBuffer.allocate(encodedPacket.length);
buffer.put(encodedPacket);
buffer.flip();
session.write(buffer);
}
示例2: sessionOpened
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void sessionOpened ( final NextFilter nextFilter, final IoSession session ) throws Exception
{
logger.debug ( "Session opened: {}", session );
final IoBuffer buffer = IoBuffer.allocate ( 1 );
buffer.setAutoExpand ( true );
buffer.put ( new byte[] { 0x11, (byte)0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, (byte)0xC1, 2, 1, 0, (byte)0xC2, 2, 0, 1, (byte)0xC0, 1, 9 } );
buffer.put ( 13, (byte) ( this.rack + 1 ) );
buffer.put ( 14, this.slot );
buffer.flip ();
session.write ( buffer );
}
示例3: handleWrite
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void handleWrite ( final Variant value )
{
final MemoryRequestBlock block = this.block;
if ( block == null )
{
throw new IllegalStateException ( "Device is not connected" );
}
final T cvtValue = getValue ( value );
if ( cvtValue != null )
{
final IoBuffer data = IoBuffer.allocate ( this.accessor.getBufferSize ( cvtValue ) );
if ( this.order != null )
{
this.order.put ( data, this.accessor, cvtValue );
}
else
{
this.accessor.put ( data, cvtValue );
}
block.writeData ( toAddress ( this.index ), data.array () );
}
}
示例4: performRead
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private IoBuffer performRead ( final int offset, final int length )
{
logger.debug ( "Perform read - offset: {}, length: {}, capacity: {}", offset, length, this.buffer.capacity () );
if ( length == 0 )
{
return null;
}
if ( offset < 0 )
{
return null;
}
if ( offset + length > this.buffer.capacity () )
{
return null;
}
final IoBuffer result = IoBuffer.allocate ( length );
result.put ( this.buffer.getSlice ( offset, length ) );
return result;
}
示例5: sessionOpened
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void sessionOpened(IoSession session) throws Exception {
String ipAddr = SessionManager.INSTANCE.getRemoteIp(session);
if (!isInWhiteIps(ipAddr)) {
logger.error("非法後台登錄,remoteIp=[{}]", ipAddr);
byte[] body = "too young too simple".getBytes("UTF-8");
IoBuffer out = IoBuffer.allocate(body.length);
out.put(body);
out.flip();
session.write(out);
session.close(false);
}
}
示例6: BitRequest
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
public BitRequest ( final byte area, final short block, final short start, final boolean[] bits )
{
super ( AddressType.BIT, area, block, start, (short)bits.length );
this.data = IoBuffer.allocate ( bits.length );
for ( int i = 0; i < bits.length; i++ )
{
this.data.put ( bits[i] ? (byte)0x01 : (byte)0x00 );
}
this.data.flip ();
}
示例7: encode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* 基本信息編碼
* */
@Override
public void encode(IoSession session, BaseMessage message,ProtocolEncoderOutput outPut) throws Exception {
IoBuffer buffer = IoBuffer.allocate(1024);
//創建自動縮小的Buffer Buffer會保持在上麵設置的1024*1024*1,但是一旦需求超過會自動增加容量
buffer.setAutoExpand(true);
//傳入數據類型
buffer.putInt(message.getDataType());
//存儲的業務數據
StringBean bean = (StringBean) message.getData();
//文件名
byte[] byteStr = bean.getFileName().getBytes(BeanUtil.charset);
//傳入文件名長度
buffer.putInt(byteStr.length);
//傳入文件名
//CharSequence charSequrnce = "你好";
//CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
//buffer.putString(charSequrnce, encoder);
buffer.put(byteStr);
//打包,歸0數組指針
buffer.flip();
//發送
outPut.write(buffer);
log.debug("編碼完成");
//自動調整一下buffer的內存空間
buffer.shrink();
}
示例8: encodeMessage
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public IoBuffer encodeMessage ( final BinaryContext context, final Object objectMessage ) throws Exception
{
final IoBuffer data = IoBuffer.allocate ( 5 );
data.putInt ( MESSAGE_CODE );
data.put ( (byte)0 ); // number of fields
data.flip ();
return data;
}
示例9: finishDecode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
IoBuffer product;
// When input contained only CR or LF rather than actual data...
if (buffer == null) {
product = IoBuffer.allocate(0);
} else {
product = buffer.flip();
buffer = null;
}
return finishDecode(product, out);
}
示例10: 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 );
}
示例11: write
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void write(int b) throws IOException {
IoBuffer buf = IoBuffer.allocate(1);
buf.put((byte) b);
buf.flip();
write(buf);
}
示例12: finishDecode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
IoBuffer readData;
if (buffer == null) {
readData = IoBuffer.allocate(0);
} else {
readData = buffer.flip();
buffer = null;
}
return finishDecode(readData, out);
}
示例13: 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 );
}
示例14: 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 () + 7 );
buffer.setAutoExpand ( true );
final IoBuffer pdu = request.getData ();
// put transaction identifier
buffer.putUnsignedShort ( request.getTransactionId () );
// put modbus protocol identifier (always 0)
buffer.putUnsignedShort ( 0 );
// put length, including slave id
buffer.putUnsignedShort ( request.getData ().remaining () + 1 );
// put slave id
buffer.put ( request.getUnitIdentifier () );
// put data
buffer.put ( pdu );
buffer.flip ();
logger.trace ( "Encoded to: {}", buffer );
out.write ( buffer );
}
示例15: finishDecode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
IoBuffer product;
// When input contained only terminator rather than actual data...
if (buffer == null) {
product = IoBuffer.allocate(0);
} else {
product = buffer.flip();
buffer = null;
}
return finishDecode(product, out);
}