本文整理汇总了Java中org.apache.mina.core.buffer.IoBuffer.flip方法的典型用法代码示例。如果您正苦于以下问题:Java IoBuffer.flip方法的具体用法?Java IoBuffer.flip怎么用?Java IoBuffer.flip使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.flip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setDelimiter
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Dynamically sets a new delimiter. Next time
* {@link IoBufferDecoder#decodeOnce(IoSession, int) } will be called it will use the new
* delimiter. Delimiter matching is reset only if <tt>resetMatchCount</tt> is true but
* decoding will continue from current position.
*
* NB : Delimiter {@link LineDelimiter#AUTO} is not allowed.
*
* @param delim the new delimiter as a byte array
* @param resetMatchCount delimiter matching is reset if true
*/
public void setDelimiter(byte[] delim, boolean resetMatchCount) {
if (delim == null) {
throw new IllegalArgumentException("Null delimiter not allowed");
}
// Convert delimiter to IoBuffer.
IoBuffer delimiter = IoBuffer.allocate(delim.length);
delimiter.put(delim);
delimiter.flip();
ctx.setDelimiter(delimiter);
ctx.setContentLength(-1);
if (resetMatchCount) {
ctx.setMatchCount(0);
}
}
示例2: 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);
}
示例3: encode
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) throws Exception {
TcpPacket packet = (TcpPacket) obj;
byte[] byteData = packet.getByteData();
int len = 18 + byteData.length;
IoBuffer buf = IoBuffer.allocate(len);
buf.put(HEAD);
buf.putInt(len);
buf.putInt(packet.gettOpCode());
buf.putInt(packet.lockedId);
buf.putInt(packet.unlockedId);
buf.put(byteData);
buf.flip();
out.write(buf);
}
示例4: encodeProperties
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private IoBuffer encodeProperties ( final IoSession session, final Map<String, String> properties ) throws CharacterCodingException
{
final IoBuffer data = IoBuffer.allocate ( 0 );
data.setAutoExpand ( true );
data.putInt ( properties.size () );
final CharsetEncoder encoder = getCharsetEncoder ( session );
for ( final Map.Entry<String, String> entry : properties.entrySet () )
{
final String key = entry.getKey ();
final String value = entry.getValue ();
data.putString ( key, encoder );
data.put ( (byte)0x00 );
data.putString ( value, encoder );
data.put ( (byte)0x00 );
}
data.flip ();
return data;
}
示例5: encode
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput output ) throws Exception
{
if ( ! ( message instanceof Frame ) )
{
throw new IllegalStateException ( String.format ( "Can only encode messages of type Frame but got %s", message.getClass () ) );
}
final Frame frame = (Frame)message;
if ( logger.isTraceEnabled () )
{
logger.trace ( "Encode frame - type: {}, data: {}", frame.getType (), frame.getData () );
}
final IoBuffer buffer = IoBuffer.allocate ( 1 + 1 + 4 + frame.getData ().remaining () );
buffer.put ( (byte)0x01 ); // version - #0
buffer.put ( (byte)frame.getType ().ordinal () ); // frame type - #1
buffer.putInt ( frame.getData ().remaining () ); // data size - #2
buffer.put ( frame.getData () ); // data - #6
buffer.flip ();
output.write ( buffer );
}
示例6: encode
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
if (!(message instanceof Serializable)) {
throw new NotSerializableException();
}
IoBuffer buf = IoBuffer.allocate(64);
buf.setAutoExpand(true);
buf.putObject(message);
int objectSize = buf.position() - 4;
if (objectSize > maxObjectSize) {
throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize
+ ')');
}
buf.flip();
out.write(buf);
}
示例7: writeMessage
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private IoBuffer writeMessage(Message message) {
//----------------消息协议格式-------------------------
// packetLength | moduleId | cmd | body
// int short short byte[]
IoBuffer buffer = IoBuffer.allocate(CodecContext.WRITE_CAPACITY);
buffer.setAutoExpand(true);
//消息内容长度,先占个坑
buffer.putInt(0);
short moduleId = message.getModule();
short cmd = message.getCmd();
//写入module类型
buffer.putShort(moduleId);
//写入cmd类型
buffer.putShort(cmd);
//写入具体消息的内容
byte[] body = wrapMessageBody(moduleId, cmd, message);
buffer.put(body);
//回到buff字节数组头部
buffer.flip();
//消息元信息,两个short,共4个字节
final int METE_SIZE = 4;
//重新写入包体长度
buffer.putInt(buffer.limit() - METE_SIZE);
buffer.rewind();
return buffer;
}
示例8: writeFrame
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* 协议00发送消息
* @param out
* @param content
* @throws IOException
*/
public static void writeFrame(IoSession session, String content)throws IOException {
byte[] ab = content.getBytes(CHARSET_UTF8);
IoBuffer buffer=IoBuffer.allocate(ab.length + 2);
buffer.put((byte)0x00);
buffer.put(ab);
buffer.put((byte)0xFF);
buffer.flip();
session.write(buffer);
}
示例9: sessionOpened
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void sessionOpened ( final NextFilter nextFilter, final IoSession session ) throws Exception
{
logger.info ( "Sending hello" );
final byte[] data = new byte[] { //
0x00, 0x00, //
(byte)0xff, (byte)0xff,//
0x00, 0x08, // plen
0x00, 0x00, // dlen
(byte)0xf0, 0x00, //
0x00, 0x01, //
0x00, 0x01, //
(byte)0xFF, (byte)0xFF // our PDU
};
final IoBuffer buffer = IoBuffer.allocate ( 0 ).setAutoExpand ( true );
buffer.put ( PACKET_START_MAGIC );
buffer.put ( (byte)0x01 ); // type
buffer.put ( data );
buffer.flip ();
session.write ( new DataTPDU ( buffer ) );
nextFilter.sessionOpened ( session );
}
示例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: TextLineDecoder
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Creates a new instance with the specified <tt>charset</tt>
* and the specified <tt>delimiter</tt>.
*/
public TextLineDecoder(Charset charset, LineDelimiter delimiter) {
if (charset == null) {
throw new IllegalArgumentException("charset parameter shuld not be null");
}
if (delimiter == null) {
throw new IllegalArgumentException("delimiter parameter should not be null");
}
this.charset = charset;
this.delimiter = delimiter;
// Convert delimiter to ByteBuffer if not done yet.
if (delimBuf == null) {
IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true);
try {
tmp.putString(delimiter.getValue(), charset.newEncoder());
} catch (CharacterCodingException cce) {
}
tmp.flip();
delimBuf = tmp;
}
}
示例12: 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 );
}
示例13: makeData
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
protected Object makeData ( final BaseMessage message, final IoBuffer data )
{
data.flip ();
logger.trace ( "Create data message - data: {}", data ); //$NON-NLS-1$
return new ReadResponse ( message.getTransactionId (), message.getUnitIdentifier (), message.getFunctionCode (), data );
}
示例14: writeRequest
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
* Encode a SOCKS4/SOCKS4a request and writes it to the next filter
* so it can be sent to the proxy server.
*
* @param nextFilter the next filter
* @param request the request to send.
*/
protected void writeRequest(final NextFilter nextFilter, final SocksProxyRequest request) {
try {
boolean isV4ARequest = Arrays.equals(request.getIpAddress(), SocksProxyConstants.FAKE_IP);
byte[] userID = request.getUserName().getBytes("ASCII");
byte[] host = isV4ARequest ? request.getHost().getBytes("ASCII") : null;
int len = 9 + userID.length;
if (isV4ARequest) {
len += host.length + 1;
}
IoBuffer buf = IoBuffer.allocate(len);
buf.put(request.getProtocolVersion());
buf.put(request.getCommandCode());
buf.put(request.getPort());
buf.put(request.getIpAddress());
buf.put(userID);
buf.put(SocksProxyConstants.TERMINATOR);
if (isV4ARequest) {
buf.put(host);
buf.put(SocksProxyConstants.TERMINATOR);
}
if (isV4ARequest) {
logger.debug(" sending SOCKS4a request");
} else {
logger.debug(" sending SOCKS4 request");
}
buf.flip();
writeData(nextFilter, buf);
} catch (Exception ex) {
closeSession("Unable to send Socks request: ", ex);
}
}
示例15: filterWrite
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void filterWrite ( final NextFilter nextFilter, final IoSession session, final WriteRequest writeRequest ) throws Exception
{
// we only handle IoBuffers
if ( writeRequest.getMessage () instanceof IoBuffer )
{
final IoBuffer inData = (IoBuffer)writeRequest.getMessage ();
final IoBuffer outData = IoBuffer.allocate ( inData.remaining () + 4 );
// put the version, the reserved
outData.put ( (byte)this.version );
outData.put ( (byte)0 );
// and the data length
outData.putShort ( (short) ( inData.remaining () + 4 ) );
// append the data itself
outData.put ( inData );
outData.flip ();
logger.debug ( "TPKT out: {}", outData );
// pass on data buffer
nextFilter.filterWrite ( session, new WriteRequestWrapper ( writeRequest ) {
@Override
public Object getMessage ()
{
return outData;
}
} );
}
else
{
nextFilter.filterWrite ( session, writeRequest );
}
}