/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package info.jerrinot.nettyloc; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> { private static final Recycler<PooledDirectByteBuf> RECYCLER = new Recycler<PooledDirectByteBuf>() { @Override protected PooledDirectByteBuf newObject(Handle handle) { return new PooledDirectByteBuf(handle); } }; static PooledDirectByteBuf newInstance() { PooledDirectByteBuf buf = RECYCLER.get(); buf.setRefCnt(1); return buf; } private PooledDirectByteBuf(Recycler.Handle recyclerHandle) { super(recyclerHandle); } @Override protected ByteBuffer newInternalNioBuffer(ByteBuffer memory) { return memory.duplicate(); } @Override public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { getBytes(index, dst, dstIndex, length, false); return this; } private void getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) { checkDstIndex(index, length, dstIndex, dst.length); ByteBuffer tmpBuf; if (internal) { tmpBuf = internalNioBuffer(); } else { tmpBuf = memory.duplicate(); } index = idx(index); tmpBuf.clear().position(index).limit(index + length); tmpBuf.get(dst, dstIndex, length); } @Override public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { checkReadableBytes(length); getBytes(0, dst, dstIndex, length, true); return this; } @Override public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { getBytes(index, out, length, false); return this; } private void getBytes(int index, OutputStream out, int length, boolean internal) throws IOException { checkIndex(index, length); if (length == 0) { return; } byte[] tmp = new byte[length]; ByteBuffer tmpBuf; if (internal) { tmpBuf = internalNioBuffer(); } else { tmpBuf = memory.duplicate(); } tmpBuf.clear().position(idx(index)); tmpBuf.get(tmp); out.write(tmp); } @Override public ByteBuf readBytes(OutputStream out, int length) throws IOException { checkReadableBytes(length); getBytes(0, out, length, true); return this; } @Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { return getBytes(index, out, length, false); } private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException { checkIndex(index, length); if (length == 0) { return 0; } ByteBuffer tmpBuf; if (internal) { tmpBuf = internalNioBuffer(); } else { tmpBuf = memory.duplicate(); } index = idx(index); tmpBuf.clear().position(index).limit(index + length); return out.write(tmpBuf); } @Override public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.length); ByteBuffer tmpBuf = internalNioBuffer(); index = idx(index); tmpBuf.clear().position(index).limit(index + length); tmpBuf.put(src, srcIndex, length); return this; } @Override public ByteBuf setBytes(int index, ByteBuffer src) { checkIndex(index); ByteBuffer tmpBuf = internalNioBuffer(); if (src == tmpBuf) { src = src.duplicate(); } index = idx(index); tmpBuf.clear().position(index).limit(index + src.remaining()); tmpBuf.put(src); return this; } @Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length); byte[] tmp = new byte[length]; int readBytes = in.read(tmp); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(idx(index)); tmpBuf.put(tmp, 0, readBytes); return readBytes; } @Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { checkIndex(index, length); ByteBuffer tmpBuf = internalNioBuffer(); index = idx(index); tmpBuf.clear().position(index).limit(index + length); try { return in.read(tmpBuf); } catch (ClosedChannelException e) { return -1; } } @Override public int nioBufferCount() { return 1; } @Override public ByteBuffer nioBuffer(int index, int length) { checkIndex(index, length); index = idx(index); return (ByteBuffer) memory.duplicate().position(index).limit(index + length); } @Override public ByteBuffer[] nioBuffers(int index, int length) { return new ByteBuffer[] { nioBuffer(index, length) }; } @Override protected Recycler<?> recycler() { return RECYCLER; } }