⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crbtgwifbytemessage.java

📁 中国联通炫铃业务接口开发
💻 JAVA
字号:
package com.wireless.crbt.gwif.netty2;
import java.nio.ByteBuffer;

import net.gleamynode.netty2.MessageParseException;
import net.gleamynode.netty2.Message;

public class CrbtGwifByteMessage implements Message{
	private boolean readHeader;
	  private boolean wroteHeader;
	  private int headLength = 4;
	  private int bodyLength = 0;
	  //字节内容
	  protected byte[] value;
	  public CrbtGwifByteMessage(){
	  }
	  public CrbtGwifByteMessage(int length){
		    this.headLength = length;
		  }

		  public byte[] getValue() {
		      return value;
		  }

		  public void setValue(byte[] value) {
		      this.value = value;
		  }
		  public final boolean read(ByteBuffer buf) throws MessageParseException {
		      // read a header if not read yet.
		      if (!readHeader) {
		          readHeader = readHeader(buf);
		          if (!readHeader)
		              return false;
		      }

		      // Header is read, now try to read body
		      if (readBody(buf)) {
		          // finished reading single complete message
		          readHeader = false; // reset state
		          return true;
		      } else
		          return false;
		  }
		  private boolean readHeader(ByteBuffer buf) throws MessageParseException {
		      // if header is not fully read, don't read it.
		      if (buf.remaining() < headLength) return false;

		      byte [] data = new byte[headLength];
		      buf.get(data, 0, data.length);
		      bodyLength = this.getCommandLength(data);
		      value = new byte[bodyLength];
		      System.arraycopy(data, 0, value, 0, data.length);

		      return true;
		  }

		  public boolean readBody(ByteBuffer buf) throws MessageParseException {
		      if( buf.remaining() < bodyLength - headLength){
		        return false;
		      }

		      byte[] data = new byte[bodyLength - headLength];
		      buf.get(data, 0, data.length);
		      System.arraycopy(data, 0, value, headLength, data.length);

		      return true;
		  }
		  public boolean write(ByteBuffer buf) {
		      // write a header if not written yet.
		      if (!wroteHeader) {
		          wroteHeader = writeHeader(buf);
		          if (!wroteHeader)
		              return false; // buffer is almost full perhaps
		      }

		      // Header is written, now try to write body
		      if (writeBody(buf)) {
		          // finished writing single complete message
		          wroteHeader = false;
		          return true;
		      } else {
		          return false;
		      }
		  }
		  private boolean writeHeader(ByteBuffer buf) {
		      // check if there is enough space to write header
		      if (buf.remaining() < headLength) {
		          return false;
		      }

		      return true;
		  }
		  public boolean writeBody(ByteBuffer buf) {
		      // check if there is enough space to write body
		      if (buf.remaining() < value.length)
		          return false;

		      buf.put(value);
		      return true;
		  }
		  private int getCommandLength(byte[] command) {
			    String L=new String();
			    for(int i=0;i<4;i++)
			    {
			    	L+=(char)command[i];
			    }
			    return Integer.parseInt(L);
			  }		  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -