/** * 赋值所有字段 * * @param input liuzeyin 2009-12-3 */ public void wrap(byte[] input) { ByteBuffer bb = ByteBuffer.allocate(NSHEAD_LEN); bb.order(ByteOrder.LITTLE_ENDIAN); bb.put(input); bb.flip(); id = UnsignedSwitch.UShortToInt(bb.getShort()); version = UnsignedSwitch.UShortToInt(bb.getShort()); logId = UnsignedSwitch.UintToLong(bb.getInt()); byte[] bf = new byte[16]; bb.get(bf); provider = NetBytesSwitch.bytesToString(bf); magicNum = UnsignedSwitch.UintToLong(bb.getInt()); reserved = UnsignedSwitch.UintToLong(bb.getInt()); bodyLen = UnsignedSwitch.UintToLong(bb.getInt()); }
/** * 将请求转为字节流 * * @return piggie 2009-3-14 */ public byte[] toBytes() throws RuntimeException { ByteBuffer bb = ByteBuffer.allocate(NSHEAD_LEN); bb.order(ByteOrder.LITTLE_ENDIAN); try { bb.putShort(UnsignedSwitch.IntToUshort(id)); bb.putShort(UnsignedSwitch.IntToUshort(version)); bb.putInt(UnsignedSwitch.LongToUint(logId)); byte[] prvd = NetBytesSwitch.StringToBytes(provider, 16); bb.put(prvd); bb.putInt(UnsignedSwitch.LongToUint(magicNum)); bb.putInt(UnsignedSwitch.LongToUint(reserved)); bb.putInt(UnsignedSwitch.LongToUint(bodyLen)); } catch (Exception e) { throw new RuntimeException("exception when putting bytes for nshead...", e); } return bb.array(); }