2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2008-08-08 03:27:24 +02:00
|
|
|
* JBoss, Home of Professional Open Source
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
|
|
* full listing of individual contributors.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
2008-08-08 02:37:18 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-08 03:27:24 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2008-08-08 02:37:18 +02:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2008-08-08 03:27:24 +02:00
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2008-08-08 03:40:10 +02:00
|
|
|
package org.jboss.netty.buffer;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2008-08-08 12:17:26 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2008-08-08 02:37:18 +02:00
|
|
|
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-08-08 12:17:26 +02:00
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* A skeletal implementation for Java heap buffers.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int capacity() {
|
|
|
|
return array.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte getByte(int index) {
|
|
|
|
return array[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
|
|
|
|
System.arraycopy(array, index, dst, dstIndex, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, ByteBuffer dst) {
|
|
|
|
dst.put(array, index, Math.min(capacity() - index, dst.remaining()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, OutputStream out, int length)
|
|
|
|
throws IOException {
|
|
|
|
out.write(array, index, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length)
|
|
|
|
throws IOException {
|
|
|
|
return out.write(ByteBuffer.wrap(array, index, length));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setByte(int index, byte value) {
|
|
|
|
array[index] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, byte[] src, int srcIndex, int length) {
|
|
|
|
System.arraycopy(src, srcIndex, array, index, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, ByteBuffer src) {
|
|
|
|
src.get(array, index, src.remaining());
|
|
|
|
}
|
|
|
|
|
2008-08-20 05:09:23 +02:00
|
|
|
public int setBytes(int index, InputStream in, int length) throws IOException {
|
|
|
|
int readBytes = 0;
|
2008-08-26 09:12:04 +02:00
|
|
|
do {
|
2008-08-20 05:09:23 +02:00
|
|
|
int localReadBytes = in.read(array, index, length);
|
|
|
|
if (localReadBytes < 0) {
|
|
|
|
if (readBytes == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
2008-08-20 05:09:23 +02:00
|
|
|
readBytes += localReadBytes;
|
|
|
|
index += localReadBytes;
|
|
|
|
length -= localReadBytes;
|
2008-08-26 09:12:04 +02:00
|
|
|
} while (length > 0);
|
2008-08-20 05:09:23 +02:00
|
|
|
|
|
|
|
return readBytes;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
|
|
|
ByteBuffer buf = ByteBuffer.wrap(array, index, length);
|
2008-08-20 05:09:23 +02:00
|
|
|
int readBytes = 0;
|
|
|
|
|
2008-08-26 09:12:04 +02:00
|
|
|
do {
|
2008-11-16 14:52:47 +01:00
|
|
|
int localReadBytes;
|
|
|
|
try {
|
|
|
|
localReadBytes = in.read(buf);
|
|
|
|
} catch (ClosedChannelException e) {
|
|
|
|
localReadBytes = -1;
|
|
|
|
}
|
2008-08-20 05:09:23 +02:00
|
|
|
if (localReadBytes < 0) {
|
|
|
|
if (readBytes == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (localReadBytes == 0) {
|
2008-08-08 02:37:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-08-20 05:09:23 +02:00
|
|
|
readBytes += localReadBytes;
|
2008-08-26 09:12:04 +02:00
|
|
|
} while (readBytes < length);
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-08-20 05:09:23 +02:00
|
|
|
return readBytes;
|
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) {
|
|
|
|
return duplicate();
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ByteBuffer toByteBuffer(int index, int length) {
|
|
|
|
return ByteBuffer.wrap(array, index, length);
|
|
|
|
}
|
2008-08-08 12:17:26 +02:00
|
|
|
|
|
|
|
public String toString(int index, int length, String charsetName) {
|
|
|
|
try {
|
|
|
|
return new String(array, index, length, charsetName);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new UnsupportedCharsetException(charsetName);
|
|
|
|
}
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|