Related issue: NETTY-257 Add the getters and setters for a String and a char[] to ChannelBuffer
* Reverted the previous addition of get/readString(...)
This commit is contained in:
parent
7d40d47270
commit
57bdb3a4a5
@ -19,15 +19,11 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A skeletal implementation of a buffer.
|
* A skeletal implementation of a buffer.
|
||||||
@ -175,44 +171,6 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
dst.writerIndex(dst.writerIndex() + length);
|
dst.writerIndex(dst.writerIndex() + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getString(int index, int length, Charset charset) {
|
|
||||||
if (length == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
|
|
||||||
final CharBuffer dst = CharBuffer.allocate(
|
|
||||||
(int) ((double) length * decoder.maxCharsPerByte()));
|
|
||||||
|
|
||||||
int decodedChars = getString(index, length, dst, charset);
|
|
||||||
if (decodedChars == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return dst.flip().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(int index, int length, char[] dst, Charset charset) {
|
|
||||||
return getString(index, length, CharBuffer.wrap(dst), charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(
|
|
||||||
int index, int length,
|
|
||||||
char[] dst, int dstOffset, int dstLength, Charset charset) {
|
|
||||||
return getString(index, length, CharBuffer.wrap(
|
|
||||||
dst, dstOffset, dstLength), charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(int index, int length, Appendable out, Charset charset)
|
|
||||||
throws IOException {
|
|
||||||
if (out instanceof CharBuffer) {
|
|
||||||
return getString(index, length, out, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
String str = getString(index, length, charset);
|
|
||||||
out.append(str);
|
|
||||||
return str.length();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setChar(int index, char value) {
|
public void setChar(int index, char value) {
|
||||||
setShort(index, (short) value);
|
setShort(index, (short) value);
|
||||||
}
|
}
|
||||||
@ -422,35 +380,6 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
readerIndex += length;
|
readerIndex += length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readString(int length, char[] dst, Charset charset) {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
int result = getString(readerIndex, length, dst, charset);
|
|
||||||
readerIndex += length;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int readString(int length, char[] dst, int dstOffset, int dstLength, Charset charset) {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
int result = getString(readerIndex, length, dst, dstOffset, dstLength, charset);
|
|
||||||
readerIndex += length;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int readString(int length, Appendable out, Charset charset)
|
|
||||||
throws IOException {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
int result = getString(readerIndex, length, out, charset);
|
|
||||||
readerIndex += length;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String readString(int length, Charset charset) {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
String result = getString(readerIndex, length, charset);
|
|
||||||
readerIndex += length;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void skipBytes(int length) {
|
public void skipBytes(int length) {
|
||||||
int newReaderIndex = readerIndex + length;
|
int newReaderIndex = readerIndex + length;
|
||||||
if (newReaderIndex > writerIndex) {
|
if (newReaderIndex > writerIndex) {
|
||||||
@ -603,9 +532,10 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString(Charset charset) {
|
public String toString(Charset charset) {
|
||||||
return getString(readerIndex, readableBytes(), charset);
|
return toString(readerIndex, readableBytes(), charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public String toString(int index, int length, String charsetName,
|
public String toString(int index, int length, String charsetName,
|
||||||
ChannelBufferIndexFinder terminatorFinder) {
|
ChannelBufferIndexFinder terminatorFinder) {
|
||||||
if (terminatorFinder == null) {
|
if (terminatorFinder == null) {
|
||||||
@ -620,15 +550,18 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
return toString(index, terminatorIndex - index, charsetName);
|
return toString(index, terminatorIndex - index, charsetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public String toString(int index, int length, String charsetName) {
|
public String toString(int index, int length, String charsetName) {
|
||||||
return getString(index, length, Charset.forName(charsetName));
|
return toString(index, length, Charset.forName(charsetName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public String toString(String charsetName,
|
public String toString(String charsetName,
|
||||||
ChannelBufferIndexFinder terminatorFinder) {
|
ChannelBufferIndexFinder terminatorFinder) {
|
||||||
return toString(readerIndex, readableBytes(), charsetName, terminatorFinder);
|
return toString(readerIndex, readableBytes(), charsetName, terminatorFinder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public String toString(String charsetName) {
|
public String toString(String charsetName) {
|
||||||
return toString(Charset.forName(charsetName));
|
return toString(Charset.forName(charsetName));
|
||||||
}
|
}
|
||||||
|
@ -20,14 +20,10 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
|
||||||
|
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link ChannelBuffers#directBuffer(int)}
|
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link ChannelBuffers#directBuffer(int)}
|
||||||
@ -303,18 +299,14 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
|
return ChannelBuffers.decodeString(
|
||||||
final int start = dst.position();
|
((ByteBuffer) buffer.duplicate().position(index).limit(index + length)),
|
||||||
final ByteBuffer src =
|
charset);
|
||||||
((ByteBuffer) buffer.duplicate().position(
|
|
||||||
index).limit(index + length)).order(order());
|
|
||||||
ChannelBuffers.decodeString(src, dst, decoder);
|
|
||||||
return dst.position() - start;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChannelBuffer slice(int index, int length) {
|
public ChannelBuffer slice(int index, int length) {
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -708,22 +707,6 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
int getBytes(int index, GatheringByteChannel out, int length) throws IOException;
|
int getBytes(int index, GatheringByteChannel out, int length) throws IOException;
|
||||||
|
|
||||||
// FIXME Document me
|
|
||||||
int getString(int index, int length, char[] dst, Charset charset);
|
|
||||||
// FIXME Document me
|
|
||||||
int getString(int index, int length, char[] dst, int dstOffset, int dstLength, Charset charset);
|
|
||||||
// FIXME Document me
|
|
||||||
int getString(int index, int length, Appendable out, Charset charset) throws IOException;
|
|
||||||
// FIXME Document me
|
|
||||||
int getString(int index, int length, CharBuffer dst, Charset charset);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decodes this buffer's sub-region into a string with the specified
|
|
||||||
* character set. This method does not modify {@code readerIndex} or
|
|
||||||
* {@code writerIndex} of this buffer.
|
|
||||||
*/
|
|
||||||
String getString(int index, int length, Charset charset);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the specified byte at the specified absolute {@code index} in this
|
* Sets the specified byte at the specified absolute {@code index} in this
|
||||||
* buffer.
|
* buffer.
|
||||||
@ -1253,15 +1236,6 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
int readBytes(GatheringByteChannel out, int length) throws IOException;
|
int readBytes(GatheringByteChannel out, int length) throws IOException;
|
||||||
|
|
||||||
// FIXME Document me
|
|
||||||
int readString(int length, char[] dst, Charset charset);
|
|
||||||
// FIXME Document me
|
|
||||||
int readString(int length, char[] dst, int dstOffset, int dstLength, Charset charset);
|
|
||||||
// FIXME Document me
|
|
||||||
int readString(int length, Appendable out, Charset charset) throws IOException ;
|
|
||||||
// FIXME Document me
|
|
||||||
String readString(int length, Charset charset);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increases the current {@code readerIndex} by the specified
|
* Increases the current {@code readerIndex} by the specified
|
||||||
* {@code length} in this buffer.
|
* {@code length} in this buffer.
|
||||||
@ -1648,6 +1622,13 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
String toString(Charset charset);
|
String toString(Charset charset);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes this buffer's sub-region into a string with the specified
|
||||||
|
* character set. This method does not modify {@code readerIndex} or
|
||||||
|
* {@code writerIndex} of this buffer.
|
||||||
|
*/
|
||||||
|
String toString(int index, int length, Charset charset);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link #toString(Charset)} instead.
|
* @deprecated Use {@link #toString(Charset)} instead.
|
||||||
*/
|
*/
|
||||||
@ -1655,20 +1636,20 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
String toString(String charsetName);
|
String toString(String charsetName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link #getString(int, int, Charset)} instead.
|
* @deprecated Use {@link #toString(int, int, Charset)} instead.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
String toString(
|
String toString(
|
||||||
String charsetName, ChannelBufferIndexFinder terminatorFinder);
|
String charsetName, ChannelBufferIndexFinder terminatorFinder);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link #getString(int, int, Charset)} instead.
|
* @deprecated Use {@link #toString(int, int, Charset)} instead.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
String toString(int index, int length, String charsetName);
|
String toString(int index, int length, String charsetName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link #getString(int, int, Charset)} instead.
|
* @deprecated Use {@link #toString(int, int, Charset)} instead.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
String toString(
|
String toString(
|
||||||
|
@ -792,12 +792,9 @@ public class ChannelBuffers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static ChannelBuffer copiedBuffer(ByteOrder endianness, CharBuffer buffer, Charset charset) {
|
private static ChannelBuffer copiedBuffer(ByteOrder endianness, CharBuffer buffer, Charset charset) {
|
||||||
CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
|
|
||||||
CharBuffer src = buffer;
|
CharBuffer src = buffer;
|
||||||
ByteBuffer dst = ByteBuffer.allocate(
|
ByteBuffer dst = ChannelBuffers.encodeString(src, charset);
|
||||||
(int) ((double) buffer.remaining() * encoder.maxBytesPerChar()));
|
ChannelBuffer result = wrappedBuffer(endianness, dst.array(), 0, dst.remaining());
|
||||||
ChannelBuffers.encodeString(src, dst, encoder);
|
|
||||||
ChannelBuffer result = wrappedBuffer(endianness, dst.array());
|
|
||||||
result.writerIndex(dst.position());
|
result.writerIndex(dst.position());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -1114,7 +1111,10 @@ public class ChannelBuffers {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void encodeString(CharBuffer src, ByteBuffer dst, CharsetEncoder encoder) {
|
static ByteBuffer encodeString(CharBuffer src, Charset charset) {
|
||||||
|
final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
|
||||||
|
final ByteBuffer dst = ByteBuffer.allocate(
|
||||||
|
(int) ((double) src.remaining() * encoder.maxBytesPerChar()));
|
||||||
try {
|
try {
|
||||||
CoderResult cr = encoder.encode(src, dst, true);
|
CoderResult cr = encoder.encode(src, dst, true);
|
||||||
if (!cr.isUnderflow()) {
|
if (!cr.isUnderflow()) {
|
||||||
@ -1127,9 +1127,14 @@ public class ChannelBuffers {
|
|||||||
} catch (CharacterCodingException x) {
|
} catch (CharacterCodingException x) {
|
||||||
throw new IllegalStateException(x);
|
throw new IllegalStateException(x);
|
||||||
}
|
}
|
||||||
|
dst.flip();
|
||||||
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void decodeString(ByteBuffer src, CharBuffer dst, CharsetDecoder decoder) {
|
static String decodeString(ByteBuffer src, Charset charset) {
|
||||||
|
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
|
||||||
|
final CharBuffer dst = CharBuffer.allocate(
|
||||||
|
(int) ((double) src.remaining() * decoder.maxCharsPerByte()));
|
||||||
try {
|
try {
|
||||||
CoderResult cr = decoder.decode(src, dst, true);
|
CoderResult cr = decoder.decode(src, dst, true);
|
||||||
if (!cr.isUnderflow()) {
|
if (!cr.isUnderflow()) {
|
||||||
@ -1142,6 +1147,7 @@ public class ChannelBuffers {
|
|||||||
} catch (CharacterCodingException x) {
|
} catch (CharacterCodingException x) {
|
||||||
throw new IllegalStateException(x);
|
throw new IllegalStateException(x);
|
||||||
}
|
}
|
||||||
|
return dst.flip().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ChannelBuffers() {
|
private ChannelBuffers() {
|
||||||
|
@ -20,17 +20,13 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A virtual buffer which shows multiple buffers as a single merged buffer. It
|
* A virtual buffer which shows multiple buffers as a single merged buffer. It
|
||||||
@ -590,15 +586,15 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
|
|||||||
return buffers.toArray(new ByteBuffer[buffers.size()]);
|
return buffers.toArray(new ByteBuffer[buffers.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
int componentId = componentId(index);
|
if (length == 0) {
|
||||||
if (index + length <= indices[componentId + 1]) {
|
return "";
|
||||||
return components[componentId].getString(
|
|
||||||
index - indices[componentId], length, dst, charset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length == 0) {
|
int componentId = componentId(index);
|
||||||
return 0;
|
if (index + length <= indices[componentId + 1]) {
|
||||||
|
return components[componentId].toString(
|
||||||
|
index - indices[componentId], length, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] data = new byte[length];
|
byte[] data = new byte[length];
|
||||||
@ -617,11 +613,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
|
|||||||
i ++;
|
i ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
|
return ChannelBuffers.decodeString(
|
||||||
final int start = dst.position();
|
ByteBuffer.wrap(data), charset);
|
||||||
final ByteBuffer src = ByteBuffer.wrap(data);
|
|
||||||
ChannelBuffers.decodeString(src, dst, decoder);
|
|
||||||
return dst.position() - start;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int componentId(int index) {
|
private int componentId(int index) {
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -182,7 +181,7 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr
|
|||||||
return buffer.toByteBuffer(index, length);
|
return buffer.toByteBuffer(index, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
return buffer.getString(index, length, dst, charset);
|
return buffer.toString(index, length, charset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -292,7 +291,7 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
|
|||||||
return buffer.toByteBuffer(index, length);
|
return buffer.toByteBuffer(index, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
return buffer.getString(index, length, dst, charset);
|
return buffer.toString(index, length, charset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,10 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
|
||||||
|
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A skeletal implementation for Java heap buffers.
|
* A skeletal implementation for Java heap buffers.
|
||||||
@ -122,16 +118,13 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer {
|
|||||||
return out.write(ByteBuffer.wrap(array, index, length));
|
return out.write(ByteBuffer.wrap(array, index, length));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
|
return ChannelBuffers.decodeString(
|
||||||
final int start = dst.position();
|
ByteBuffer.wrap(array, index, length), charset);
|
||||||
final ByteBuffer src = ByteBuffer.wrap(array, index, length);
|
|
||||||
ChannelBuffers.decodeString(src, dst, decoder);
|
|
||||||
return dst.position() - start;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setByte(int index, byte value) {
|
public void setByte(int index, byte value) {
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.ReadOnlyBufferException;
|
import java.nio.ReadOnlyBufferException;
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
@ -196,7 +195,7 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap
|
|||||||
return buffer.capacity();
|
return buffer.capacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
return buffer.getString(index, length, dst, charset);
|
return buffer.toString(index, length, charset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -215,9 +214,9 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
|
|||||||
return buffer.toByteBuffer(index + adjustment, length);
|
return buffer.toByteBuffer(index + adjustment, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
return buffer.getString(index + adjustment, length, dst, charset);
|
return buffer.toString(index + adjustment, length, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIndex(int index) {
|
private void checkIndex(int index) {
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -209,9 +208,9 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
|
|||||||
return buffer.toByteBuffer(index, length);
|
return buffer.toByteBuffer(index, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
return buffer.getString(index, length, dst, charset);
|
return buffer.toString(index, length, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIndex(int index) {
|
private void checkIndex(int index) {
|
||||||
|
@ -20,7 +20,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -325,28 +324,6 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
throw new UnreplayableOperationException();
|
throw new UnreplayableOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readString(int length, Appendable out, Charset charset)
|
|
||||||
throws IOException {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
return buffer.readString(length, out, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int readString(int length, char[] dst, Charset charset) {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
return buffer.readString(length, dst, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int readString(int length, char[] dst, int dstOffset, int dstLength, Charset charset) {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
return buffer.readString(length, dst, dstOffset, dstLength, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String readString(int length, Charset charset) {
|
|
||||||
checkReadableBytes(length);
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int readerIndex() {
|
public int readerIndex() {
|
||||||
return buffer.readerIndex();
|
return buffer.readerIndex();
|
||||||
}
|
}
|
||||||
@ -528,31 +505,9 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
return buffer.toByteBuffers(index, length);
|
return buffer.toByteBuffers(index, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getString(int index, int length, Charset charset) {
|
public String toString(int index, int length, Charset charset) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
return buffer.getString(index, length, charset);
|
return buffer.toString(index, length, charset);
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(int index, int length, Appendable out, Charset charset)
|
|
||||||
throws IOException {
|
|
||||||
checkIndex(index, length);
|
|
||||||
return buffer.getString(index, length, out, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(int index, int length, char[] dst, Charset charset) {
|
|
||||||
checkIndex(index, length);
|
|
||||||
return buffer.getString(index, length, dst, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(int index, int length, char[] dst, int dstOffset, int dstLength,
|
|
||||||
Charset charset) {
|
|
||||||
checkIndex(index, length);
|
|
||||||
return buffer.getString(index, length, dst, dstOffset, dstLength, charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getString(int index, int length, CharBuffer dst, Charset charset) {
|
|
||||||
checkIndex(index, length);
|
|
||||||
return buffer.getString(index, length, dst, charset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(Charset charsetName) {
|
public String toString(Charset charsetName) {
|
||||||
|
@ -23,7 +23,6 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.channels.GatheringByteChannel;
|
import java.nio.channels.GatheringByteChannel;
|
||||||
import java.nio.channels.ScatteringByteChannel;
|
import java.nio.channels.ScatteringByteChannel;
|
||||||
|
|
||||||
@ -96,11 +95,10 @@ public class ReadOnlyChannelBufferTest {
|
|||||||
|
|
||||||
ByteBuffer bb = ByteBuffer.allocate(100);
|
ByteBuffer bb = ByteBuffer.allocate(100);
|
||||||
ByteBuffer[] bbs = new ByteBuffer[] { ByteBuffer.allocate(101), ByteBuffer.allocate(102) };
|
ByteBuffer[] bbs = new ByteBuffer[] { ByteBuffer.allocate(101), ByteBuffer.allocate(102) };
|
||||||
CharBuffer cb = CharBuffer.allocate(100);
|
|
||||||
|
|
||||||
expect(buf.toByteBuffer(23, 24)).andReturn(bb);
|
expect(buf.toByteBuffer(23, 24)).andReturn(bb);
|
||||||
expect(buf.toByteBuffers(25, 26)).andReturn(bbs);
|
expect(buf.toByteBuffers(25, 26)).andReturn(bbs);
|
||||||
expect(buf.getString(27, 28, cb, CharsetUtil.UTF_8)).andReturn(29);
|
expect(buf.toString(27, 28, CharsetUtil.UTF_8)).andReturn("29");
|
||||||
expect(buf.capacity()).andReturn(30);
|
expect(buf.capacity()).andReturn(30);
|
||||||
|
|
||||||
replay(buf);
|
replay(buf);
|
||||||
@ -128,7 +126,7 @@ public class ReadOnlyChannelBufferTest {
|
|||||||
assertEquals(102, roBBs[1].capacity());
|
assertEquals(102, roBBs[1].capacity());
|
||||||
assertTrue(roBBs[1].isReadOnly());
|
assertTrue(roBBs[1].isReadOnly());
|
||||||
|
|
||||||
assertEquals(29, roBuf.getString(27, 28, cb, CharsetUtil.UTF_8));
|
assertEquals("29", roBuf.toString(27, 28, CharsetUtil.UTF_8));
|
||||||
assertEquals(30, roBuf.capacity());
|
assertEquals(30, roBuf.capacity());
|
||||||
|
|
||||||
verify(buf);
|
verify(buf);
|
||||||
|
Loading…
Reference in New Issue
Block a user