log.debug("Encrypting message: {}", message); byte[] plain = new byte[message.remaining()]; message.get(plain); message.clear();
@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { while (in.remaining() > 4) { // 前4位是长度 byte[] lengthBytes = new byte[4]; in.mark(); //标记当前位置,以便reset in.get(lengthBytes); //读取前4字节 int length = ByteBuffer.wrap(lengthBytes).getInt(); // 数据不够,返回false,需要继续读取 if (length == 0 || length > in.remaining()) { in.reset(); return false; } // 够了,开始解码 byte[] bytes = new byte[length]; in.get(bytes, 0, length); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { RemotingCommand remotingCommand = codec.decode(byteBuffer); out.write(remotingCommand); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("decode exception, {}", RemotingHelper.parseChannelRemoteAddr(channel), e); RemotingHelper.closeChannel(channel); } } return false; }
@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { while (in.remaining() > 4) { // 前4位是长度 byte[] lengthBytes = new byte[4]; in.mark(); //标记当前位置,以便reset in.get(lengthBytes); //读取前4字节 int length = ByteBuffer.wrap(lengthBytes).getInt(); // 数据不够,返回false,需要继续读取 if (length == 0 || length > in.remaining()) { in.reset(); return false; } // 够了,开始解码 byte[] bytes = new byte[length]; in.get(bytes, 0, length); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { RemotingCommand remotingCommand = codec.decode(byteBuffer); out.write(remotingCommand); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("decode exception, {}", RemotingHelper.parseChannelRemoteAddr(channel), e); RemotingHelper.closeChannel(channel); } } return false; }
byte[] bytes = new byte[parsedBuf.buffer.remaining()]; parsedBuf.buffer.get(bytes);
int remaining = buf.remaining(); if (remaining > 0) {
@Override public int available() { if (released) { return 0; } synchronized (mutex) { return buf.remaining(); } }
public void messageReceived(IoSession session, Object message) { if (message instanceof IoBuffer) { IoBuffer buffer = (IoBuffer) message; byte[] b = new byte[buffer.remaining()]; buffer.get(b); } }
/** * {@inheritDoc} */ @Override public void put(IoBuffer bb) { int size = bb.remaining(); BufferByteArray.this.put(index, bb); index += size; }
/** * {@inheritDoc} */ @Override public void fireMessageReceived(Object message) { if (message instanceof IoBuffer) { session.increaseReadBytes(((IoBuffer) message).remaining(), System.currentTimeMillis()); } callNextMessageReceived(head, session, message); }
/** * {@inheritDoc} */ @Override public void get(IoBuffer bb) { int size = Math.min(getRemaining(), bb.remaining()); BufferByteArray.this.get(index, bb); index += size; }
@Override public MessageDecoderResult decodable(IoSession session, IoBuffer in) { boolean hasHeader = HEADER_PATTERN.find(in, in.position()) != -1L; return hasHeader ? MessageDecoderResult.OK : (in.remaining() > MAX_UNDECODED_DATA_LENGTH ? MessageDecoderResult.NOT_OK : MessageDecoderResult.NEED_DATA); }
public void write(byte[] b, int off, int len) throws IOException { while (len > buffer.remaining()) { int nlen = buffer.remaining(); buffer.put(b, off, nlen); len -= nlen; off += nlen; send(); buffer = IoBuffer.allocate((64 * 1024) - 42); } buffer.put(b, off, len); } private void send() throws IOException {
public void write(byte[] b, int off, int len) throws IOException { while (len > buffer.remaining()) { int nlen = buffer.remaining(); buffer.put(b, off, nlen); len -= nlen; off += nlen; send(); buffer = IoBuffer.allocate((64 * 1024) - 42); } buffer.put(b, off, len); } private void send() throws IOException {
/** * {@inheritDoc} */ @Override public void put(IoBuffer bb) { prepareForAccess(bb.remaining()); cursor.put(bb); }
@Override public MessageDecoderResult decodable(IoSession session, IoBuffer in) { boolean hasHeader = HEADER_PATTERN.find(in, in.position()) != -1L; return hasHeader ? MessageDecoderResult.OK : (in.remaining() > MAX_UNDECODED_DATA_LENGTH ? MessageDecoderResult.NOT_OK : MessageDecoderResult.NEED_DATA); }
public void append(IoBuffer in) { if (overflowPosition != 0) { discard(in); } else if (buf.position() > maxLineLength - in.remaining()) { overflowPosition = buf.position(); buf.clear(); discard(in); } else { getBuffer().put(in); } }
@Override protected int write(NioSession session, IoBuffer buf, int length) throws IOException { if (buf.remaining() <= length) { return session.getChannel().write(buf.buf()); } int oldLimit = buf.limit(); buf.limit(buf.position() + length); try { return session.getChannel().write(buf.buf()); } finally { buf.limit(oldLimit); } }