2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 The Netty Project
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 03:27:24 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.buffer;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.nio.ByteBuffer;
|
2008-11-16 14:52:47 +01:00
|
|
|
import java.nio.channels.ClosedChannelException;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
|
|
|
|
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* A skeletal implementation for Java heap buffers.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
|
|
|
public abstract class HeapChannelBuffer extends AbstractChannelBuffer {
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* The underlying heap byte array that this buffer is wrapping.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
protected final byte[] array;
|
|
|
|
|
2012-05-25 15:10:10 +02:00
|
|
|
protected final ByteBuffer nioBuf;
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Creates a new heap buffer with a newly allocated byte array.
|
|
|
|
*
|
|
|
|
* @param length the length of the new byte array
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
public HeapChannelBuffer(int length) {
|
|
|
|
this(new byte[length], 0, 0);
|
|
|
|
}
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Creates a new heap buffer with an existing byte array.
|
|
|
|
*
|
|
|
|
* @param array the byte array to wrap
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
public HeapChannelBuffer(byte[] array) {
|
|
|
|
this(array, 0, array.length);
|
|
|
|
}
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Creates a new heap buffer with an existing byte array.
|
|
|
|
*
|
|
|
|
* @param array the byte array to wrap
|
|
|
|
* @param readerIndex the initial reader index of this buffer
|
|
|
|
* @param writerIndex the initial writer index of this buffer
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
protected HeapChannelBuffer(byte[] array, int readerIndex, int writerIndex) {
|
|
|
|
if (array == null) {
|
|
|
|
throw new NullPointerException("array");
|
|
|
|
}
|
|
|
|
this.array = array;
|
|
|
|
setIndex(readerIndex, writerIndex);
|
2012-05-25 15:10:10 +02:00
|
|
|
nioBuf = ByteBuffer.wrap(array);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2010-01-27 06:07:32 +01:00
|
|
|
public boolean isDirect() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int capacity() {
|
|
|
|
return array.length;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-11-25 08:08:52 +01:00
|
|
|
public boolean hasArray() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-11-25 08:08:52 +01:00
|
|
|
public byte[] array() {
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-11-25 08:08:52 +01:00
|
|
|
public int arrayOffset() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public byte getByte(int index) {
|
|
|
|
return array[index];
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
|
|
|
|
if (dst instanceof HeapChannelBuffer) {
|
|
|
|
getBytes(index, ((HeapChannelBuffer) dst).array, dstIndex, length);
|
|
|
|
} else {
|
|
|
|
dst.setBytes(dstIndex, array, index, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
|
|
|
|
System.arraycopy(array, index, dst, dstIndex, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, ByteBuffer dst) {
|
|
|
|
dst.put(array, index, Math.min(capacity() - index, dst.remaining()));
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, OutputStream out, int length)
|
|
|
|
throws IOException {
|
|
|
|
out.write(array, index, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length)
|
|
|
|
throws IOException {
|
2012-05-25 15:10:10 +02:00
|
|
|
return out.write((ByteBuffer) nioBuf.clear().position(index).limit(index + length));
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2010-03-03 06:35:01 +01:00
|
|
|
public void setByte(int index, int value) {
|
|
|
|
array[index] = (byte) value;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
|
|
|
|
if (src instanceof HeapChannelBuffer) {
|
|
|
|
setBytes(index, ((HeapChannelBuffer) src).array, srcIndex, length);
|
|
|
|
} else {
|
|
|
|
src.getBytes(srcIndex, array, index, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setBytes(int index, byte[] src, int srcIndex, int length) {
|
|
|
|
System.arraycopy(src, srcIndex, array, index, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setBytes(int index, ByteBuffer src) {
|
|
|
|
src.get(array, index, src.remaining());
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-20 05:09:23 +02:00
|
|
|
public int setBytes(int index, InputStream in, int length) throws IOException {
|
2012-05-25 22:58:56 +02:00
|
|
|
return in.read(array, index, length);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
2012-05-25 15:10:10 +02:00
|
|
|
try {
|
|
|
|
return in.read((ByteBuffer) nioBuf.clear().position(index).limit(index + length));
|
|
|
|
} catch (ClosedChannelException e) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelBuffer slice(int index, int length) {
|
|
|
|
if (index == 0) {
|
2008-08-26 17:37:08 +02:00
|
|
|
if (length == 0) {
|
|
|
|
return ChannelBuffers.EMPTY_BUFFER;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
if (length == array.length) {
|
2010-06-21 09:00:03 +02:00
|
|
|
ChannelBuffer slice = duplicate();
|
|
|
|
slice.setIndex(0, length);
|
|
|
|
return slice;
|
2008-08-08 02:37:18 +02:00
|
|
|
} else {
|
|
|
|
return new TruncatedChannelBuffer(this, length);
|
|
|
|
}
|
|
|
|
} else {
|
2008-08-26 17:37:08 +02:00
|
|
|
if (length == 0) {
|
|
|
|
return ChannelBuffers.EMPTY_BUFFER;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
return new SlicedChannelBuffer(this, index, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2012-06-02 10:30:55 +02:00
|
|
|
public boolean hasNioBuffer() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuffer nioBuffer(int index, int length) {
|
2009-06-17 10:08:11 +02:00
|
|
|
return ByteBuffer.wrap(array, index, length).order(order());
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|