本文整理匯總了Java中java.nio.ReadOnlyBufferException類的典型用法代碼示例。如果您正苦於以下問題:Java ReadOnlyBufferException類的具體用法?Java ReadOnlyBufferException怎麽用?Java ReadOnlyBufferException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ReadOnlyBufferException類屬於java.nio包,在下文中一共展示了ReadOnlyBufferException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readOnly
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
@Test
public void readOnly() throws Exception {
assertFalse(Bytes.from(example_bytes_twentyfour).isReadOnly());
assertTrue(Bytes.from(example_bytes_twentyfour).readOnly().isReadOnly());
assertTrue(Bytes.from(example_bytes_twentyfour).readOnly().copy().isReadOnly());
assertArrayEquals(example_bytes_twentyfour, Bytes.from(example_bytes_twentyfour).readOnly().internalArray());
try {
Bytes.from(example_bytes_twentyfour).readOnly().array();
fail();
} catch (ReadOnlyBufferException e) {
}
Bytes b = Bytes.from(example_bytes_twentyfour).readOnly();
assertSame(b, b.readOnly());
}
示例2: putString
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
public static void putString(ByteBuffer buffer, String str, Charset charset) throws BufferOverflowException, ReadOnlyBufferException {
if (str == null || str.isEmpty())
putCompactInt(buffer, 0);
else if (charset != null)
putBytes(buffer, str, charset);
else {
try {
if (defaultCharset.newEncoder().canEncode(str)) {
putBytes(buffer, str, defaultCharset);
return;
}
} catch (UnsupportedOperationException ignore) {
}
putChars(buffer, str);
}
}
示例3: testGetReadOnlyDst
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
private void testGetReadOnlyDst(boolean direct) {
byte[] bytes = { 'a', 'b', 'c', 'd' };
ByteBuf buffer = newBuffer(bytes.length);
buffer.writeBytes(bytes);
ByteBuffer dst = direct ? ByteBuffer.allocateDirect(bytes.length) : ByteBuffer.allocate(bytes.length);
ByteBuffer readOnlyDst = dst.asReadOnlyBuffer();
try {
buffer.getBytes(0, readOnlyDst);
fail();
} catch (ReadOnlyBufferException e) {
// expected
}
assertEquals(0, readOnlyDst.position());
buffer.release();
}
示例4: encode
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
private static void encode(CharSequence paramCharSequence, ByteBuffer paramByteBuffer)
{
if (paramByteBuffer.isReadOnly()) {
throw new ReadOnlyBufferException();
}
if (paramByteBuffer.hasArray()) {
try
{
paramByteBuffer.position(encode(paramCharSequence, paramByteBuffer.array(), paramByteBuffer.arrayOffset() + paramByteBuffer.position(), paramByteBuffer.remaining()) - paramByteBuffer.arrayOffset());
return;
}
catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException)
{
BufferOverflowException localBufferOverflowException = new BufferOverflowException();
localBufferOverflowException.initCause(localArrayIndexOutOfBoundsException);
throw localBufferOverflowException;
}
}
encodeDirect(paramCharSequence, paramByteBuffer);
}
示例5: zza
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
private static void zza(CharSequence paramCharSequence, ByteBuffer paramByteBuffer)
{
if (paramByteBuffer.isReadOnly()) {
throw new ReadOnlyBufferException();
}
if (paramByteBuffer.hasArray()) {
try
{
paramByteBuffer.position(zza(paramCharSequence, paramByteBuffer.array(), paramByteBuffer.arrayOffset() + paramByteBuffer.position(), paramByteBuffer.remaining()) - paramByteBuffer.arrayOffset());
return;
}
catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException)
{
BufferOverflowException localBufferOverflowException = new BufferOverflowException();
localBufferOverflowException.initCause(localArrayIndexOutOfBoundsException);
throw localBufferOverflowException;
}
}
zzb(paramCharSequence, paramByteBuffer);
}
示例6: a
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
private static void a(CharSequence paramCharSequence, ByteBuffer paramByteBuffer)
{
if (paramByteBuffer.isReadOnly()) {
throw new ReadOnlyBufferException();
}
if (paramByteBuffer.hasArray()) {
try
{
paramByteBuffer.position(a(paramCharSequence, paramByteBuffer.array(), paramByteBuffer.arrayOffset() + paramByteBuffer.position(), paramByteBuffer.remaining()) - paramByteBuffer.arrayOffset());
return;
}
catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException)
{
BufferOverflowException localBufferOverflowException = new BufferOverflowException();
localBufferOverflowException.initCause(localArrayIndexOutOfBoundsException);
throw localBufferOverflowException;
}
}
b(paramCharSequence, paramByteBuffer);
}
示例7: encode
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
/**
* Encodes {@code sequence} into UTF-8, in {@code byteBuffer}. For a string, this method is
* equivalent to {@code buffer.put(string.getBytes(UTF_8))}, but is more efficient in both time
* and space. Bytes are written starting at the current position. This method requires paired
* surrogates, and therefore does not support chunking.
*
* <p>To ensure sufficient space in the output buffer, either call {@link #encodedLength} to
* compute the exact amount needed, or leave room for {@code 3 * sequence.length()}, which is the
* largest possible number of bytes that any input can be encoded to.
*
* @throws IllegalArgumentException if {@code sequence} contains ill-formed UTF-16 (unpaired
* surrogates)
* @throws BufferOverflowException if {@code sequence} encoded in UTF-8 does not fit in
* {@code byteBuffer}'s remaining space.
* @throws ReadOnlyBufferException if {@code byteBuffer} is a read-only buffer.
*/
private static void encode(CharSequence sequence, ByteBuffer byteBuffer) {
if (byteBuffer.isReadOnly()) {
throw new ReadOnlyBufferException();
} else if (byteBuffer.hasArray()) {
try {
int encoded = encode(sequence,
byteBuffer.array(),
byteBuffer.arrayOffset() + byteBuffer.position(),
byteBuffer.remaining());
byteBuffer.position(encoded - byteBuffer.arrayOffset());
} catch (ArrayIndexOutOfBoundsException e) {
BufferOverflowException boe = new BufferOverflowException();
boe.initCause(e);
throw boe;
}
} else {
encodeDirect(sequence, byteBuffer);
}
}
示例8: calcDstsLength
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
private static int calcDstsLength(ByteBuffer[] dsts, int dstsOffset, int dstsLength) {
int capacity = 0;
for (int i = 0; i < dsts.length; i++) {
ByteBuffer dst = dsts[i];
checkArgument(dst != null, "dsts[%d] is null", i);
if (dst.isReadOnly()) {
throw new ReadOnlyBufferException();
}
if (i >= dstsOffset && i < dstsOffset + dstsLength) {
capacity += dst.remaining();
}
}
return capacity;
}
示例9: putBytes
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
public static void putBytes(ByteBuffer buffer, String str, Charset charset) throws BufferOverflowException, ReadOnlyBufferException {
if (str == null || str.isEmpty())
putCompactInt(buffer, 0);
else {
byte[] strBytes = (str + '\0').getBytes(charset);
putCompactInt(buffer, strBytes.length);
buffer.put(strBytes);
}
}
示例10: putChars
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
public static void putChars(ByteBuffer buffer, String str) throws BufferOverflowException, ReadOnlyBufferException {
if (str == null || str.isEmpty())
putCompactInt(buffer, 0);
else {
byte[] strBytes = (str + '\0').getBytes(UTF16LE);
putCompactInt(buffer, -strBytes.length);
buffer.put(strBytes);
}
}
示例11: putUTF
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
public static void putUTF(ByteBuffer buffer, String str) throws BufferOverflowException, ReadOnlyBufferException {
if (str == null || str.isEmpty()) {
buffer.putInt(0);
} else {
byte[] bytes = str.getBytes(UTF16LE);
buffer.putInt(bytes.length);
buffer.put(bytes);
}
}
示例12: testForwarding
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
public void testForwarding() {
GrowableByteBuffer g = new GrowableByteBuffer(1063);
int first = g.arrayOffset();
g.put(0, (byte) 37);
assertTrue(g.hasArray());
assertEquals((byte) 37, g.array()[first]);
g.putChar(0, 'a');
assertEquals('a', g.getChar(0));
assertEquals('a', g.asCharBuffer().get(0));
g.putDouble(0, 10.0d);
assertEquals(10.0d, g.getDouble(0));
assertEquals(10.0d, g.asDoubleBuffer().get(0));
g.putFloat(0, 10.0f);
assertEquals(10.0f, g.getFloat(0));
assertEquals(10.0f, g.asFloatBuffer().get(0));
g.putInt(0, 10);
assertEquals(10, g.getInt(0));
assertEquals(10, g.asIntBuffer().get(0));
g.putLong(0, 10L);
assertEquals(10L, g.getLong(0));
assertEquals(10L, g.asLongBuffer().get(0));
boolean caught = false;
try {
g.asReadOnlyBuffer().put((byte) 10);
} catch (ReadOnlyBufferException e) {
caught = true;
}
assertTrue(caught);
g.putShort(0, (short) 10);
assertEquals((short) 10, g.getShort(0));
assertEquals((short) 10, g.asShortBuffer().get(0));
g.position(0);
g.put((byte) 0);
g.put((byte) 10);
g.limit(2);
g.position(1);
g.compact();
assertEquals((byte) 10, g.get(0));
}
示例13: ensureWritable
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
@Override
public ByteBuf ensureWritable(int minWritableBytes) {
// note: ReadOnlyByteBuf allows this but in most cases this does not make sense
if (minWritableBytes != 0) {
throw new ReadOnlyBufferException();
}
return this;
}
示例14: testSetInt
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
@Test(expected = ReadOnlyBufferException.class)
public void testSetInt() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setInt(0, 1);
}
示例15: testSetBytesViaStream
import java.nio.ReadOnlyBufferException; //導入依賴的package包/類
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaStream() throws IOException {
ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
buf.release();
}