@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { LOGGER.error("Message is null"); return; } if (!(message instanceof RemotingCommand)) { LOGGER.error("{} is not instanceof RemotingCommand", message); return; } RemotingCommand remotingCommand = (RemotingCommand) message; try { ByteBuffer byteBuffer = codec.encode(remotingCommand); IoBuffer ioBuffer = bufferAllocator.wrap(byteBuffer); out.write(ioBuffer); out.flush(); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("encode exception, addr={}, remotingCommand={}", RemotingHelper.parseChannelRemoteAddr(channel), remotingCommand.toString(), e); RemotingHelper.closeChannel(channel); } } };
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.position(0); oldBuf.limit(limit);
/** * Sets the allocator used by existing and new buffers */ public static void setAllocator(IoBufferAllocator newAllocator) { if (newAllocator == null) { throw new NullPointerException("allocator"); } IoBufferAllocator oldAllocator = allocator; allocator = newAllocator; if (null != oldAllocator) { oldAllocator.dispose(); } }
/** * Returns a direct or heap IoBuffer which can contain the specified number of bytes. * * @param capacity the capacity of the buffer * @param useDirectBuffer <tt>true</tt> to get a direct buffer, <tt>false</tt> to get a * heap buffer. * @return a direct or heap IoBuffer which can hold up to capacity bytes */ public static IoBuffer allocate(int capacity, boolean useDirectBuffer) { if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } return allocator.allocate(capacity, useDirectBuffer); }
.allocateNioBuffer(newCapacity, isDirect()); oldBuf.position(0); oldBuf.limit(limit);
/** * Sets the allocator used by existing and new buffers * * @param newAllocator the new allocator to use */ public static void setAllocator(IoBufferAllocator newAllocator) { if (newAllocator == null) { throw new IllegalArgumentException("allocator"); } IoBufferAllocator oldAllocator = allocator; allocator = newAllocator; if (null != oldAllocator) { oldAllocator.dispose(); } }
/** * Returns the buffer which is capable of the specified size. * * @param capacity the capacity of the buffer * @param direct <tt>true</tt> to get a direct buffer, * <tt>false</tt> to get a heap buffer. */ public static IoBuffer allocate(int capacity, boolean direct) { if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } return allocator.allocate(capacity, direct); }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { LOGGER.error("Message is null"); return; } if (!(message instanceof RemotingCommand)) { LOGGER.error("{} is not instanceof RemotingCommand", message); return; } RemotingCommand remotingCommand = (RemotingCommand) message; try { ByteBuffer byteBuffer = codec.encode(remotingCommand); IoBuffer ioBuffer = bufferAllocator.wrap(byteBuffer); out.write(ioBuffer); out.flush(); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("encode exception, addr={}, remotingCommand={}", RemotingHelper.parseChannelRemoteAddr(channel), remotingCommand.toString(), e); RemotingHelper.closeChannel(channel); } } };
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.clear(); newBuf.put(oldBuf);
/** * Sets the allocator used by existing and new buffers * * @param newAllocator the new allocator to use */ public static void setAllocator(IoBufferAllocator newAllocator) { if (newAllocator == null) { throw new IllegalArgumentException("allocator"); } IoBufferAllocator oldAllocator = allocator; allocator = newAllocator; if (null != oldAllocator) { oldAllocator.dispose(); } }
/** * Returns a direct or heap IoBuffer which can contain the specified number of bytes. * * @param capacity the capacity of the buffer * @param useDirectBuffer <tt>true</tt> to get a direct buffer, <tt>false</tt> to get a * heap buffer. * @return a direct or heap IoBuffer which can hold up to capacity bytes */ public static IoBuffer allocate(int capacity, boolean useDirectBuffer) { if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } return allocator.allocate(capacity, useDirectBuffer); }
/** * Wraps the specified NIO {@link ByteBuffer} into a MINA buffer (either direct or heap). * * @param nioBuffer The {@link ByteBuffer} to wrap * @return a IoBuffer containing the bytes stored in the {@link ByteBuffer} */ public static IoBuffer wrap(ByteBuffer nioBuffer) { return allocator.wrap(nioBuffer); }
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.clear();
/** * Wraps the specified NIO {@link ByteBuffer} into a MINA buffer (either direct or heap). * * @param nioBuffer The {@link ByteBuffer} to wrap * @return a IoBuffer containing the bytes stored in the {@link ByteBuffer} */ public static IoBuffer wrap(ByteBuffer nioBuffer) { return allocator.wrap(nioBuffer); }
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.position(0); oldBuf.limit(limit);
/** * Wraps the specified NIO {@link ByteBuffer} into MINA buffer. */ public static IoBuffer wrap(ByteBuffer nioBuffer) { return allocator.wrap(nioBuffer); }
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.clear(); newBuf.put(oldBuf);
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { LOGGER.error("Message is null"); return; } if (!(message instanceof RemotingCommand)) { LOGGER.error("{} is not instanceof RemotingCommand", message); return; } RemotingCommand remotingCommand = (RemotingCommand) message; try { ByteBuffer byteBuffer = codec.encode(remotingCommand); IoBuffer ioBuffer = bufferAllocator.wrap(byteBuffer); out.write(ioBuffer); out.flush(); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("encode exception, addr={}, remotingCommand={}", RemotingHelper.parseChannelRemoteAddr(channel), remotingCommand.toString(), e); RemotingHelper.closeChannel(channel); } } };
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); newBuf.put(oldBuf); buf(newBuf);
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); newBuf.put(oldBuf); buf(newBuf);