public static void writeHeader( ChannelBuffer buffer, long requestId, byte status, Version version) { int index = buffer.readerIndex(); buffer.setByte(index, 'E'); index += 1; buffer.setByte(index, 'S'); index += 1; // write the size, the size indicates the remaining message size, not including the size int buffer.setInt(index, buffer.readableBytes() - 6); index += 4; buffer.setLong(index, requestId); index += 8; buffer.setByte(index, status); index += 1; buffer.setInt(index, version.id); }
protected void applyDataMask(byte[] mask, ChannelBuffer data) { if (!shouldMask()) { return; } int dataLen = data.readableBytes(); data.markReaderIndex(); for (int i = 0; i < dataLen; ++i) { byte cur = data.getByte(i); cur = (byte) (cur ^ mask[i % 4]); data.setByte(i, cur); } data.resetReaderIndex(); }
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (!(msg instanceof ChannelBuffer)) return msg; if (!enabled) return msg; final ChannelBuffer buffer = (ChannelBuffer) msg; final int offset = buffer.readerIndex() + 2; // skip header final int size = buffer.readableBytes() - 2; int temp = 0, temp2 = 0; synchronized (key) { for (int i = 0; i < size; i++) { temp2 = buffer.getByte(offset + i) & 0xFF; temp = temp2 ^ key.get(i) ^ temp; buffer.setByte(offset + i, (byte) temp); } key.update(size); } return msg; }
@Override public int write(ChannelBuffer cb) { int iLenStartIndex = cb.writerIndex(); cb.writeByte(FLAGS); cb.writeByte(getType()); if (isaspathSet()) { int iAsLenIndex = cb.writerIndex(); cb.writeByte(0); cb.writeByte(ASPATH_SEQ_TYPE); cb.writeByte(aspathSeq.size()); for (int j = 0; j < aspathSeq.size(); j++) { cb.writeShort(aspathSeq.get(j)); } int asLen = cb.writerIndex() - iAsLenIndex; cb.setByte(iAsLenIndex, (byte) (asLen - 1)); } else { cb.writeByte(0); } return cb.writerIndex() - iLenStartIndex; }