java.nio.ReadOnlyBufferException Java Examples

The following examples show how to use java.nio.ReadOnlyBufferException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: OzoneFSInputStream.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * @param buf the ByteBuffer to receive the results of the read operation.
 * @return the number of bytes read, possibly zero, or -1 if
 *         reach end-of-stream
 * @throws IOException if there is some error performing the read
 */
@Override
public int read(ByteBuffer buf) throws IOException {
  if (buf.isReadOnly()){
    throw new ReadOnlyBufferException();
  }

  int readLen = Math.min(buf.remaining(), available());

  int bytesRead;
  if (buf.hasArray()) {
    int pos = buf.position();
    bytesRead = read(buf.array(), pos, readLen);
    if (bytesRead > 0) {
      buf.position(pos + bytesRead);
    }
  } else {
    byte[] readData = new byte[readLen];
    bytesRead = read(readData, 0, readLen);
    if (bytesRead > 0) {
      buf.put(readData);
    }
  }

  return bytesRead;
}
 
Example #3
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetMedium() throws IOException {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    try {
        buf.setMedium(0, 1);
    } finally {
        buf.release();
    }
}
 
Example #4
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetBoolean() {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    try {
        buf.setBoolean(0, true);
    } finally {
        buf.release();
    }
}
 
Example #5
Source File: ReadOnlySlicedNetworkBuffer.java    From flink with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #6
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetByte() {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    try {
        buf.setByte(0, 1);
    } finally {
        buf.release();
    }
}
 
Example #7
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesWithByteBuf() {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    ByteBuf src = wrappedBuffer(new byte[4]);
    try {
        buf.setBytes(0, src);
    } finally {
        buf.release();
        src.release();
    }
}
 
Example #8
Source File: ReadOnlyDirectByteBufferBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaArray() {
    ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
    try {
        buf.setBytes(0, "test".getBytes());
    } finally {
        buf.release();
    }
}
 
Example #9
Source File: ConnectionAttributesTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void test_get_immutable() {
    final String key = "test.key";
    final ByteBuffer value = ByteBuffer.wrap("test.value".getBytes());

    connectionAttributes.put(key, value);

    final Optional<ByteBuffer> returnValue1 = connectionAttributes.get(key);
    assertTrue(returnValue1.isPresent());
    assertEquals(value, returnValue1.get());

    returnValue1.get().put(0, (byte) 10);

}
 
Example #10
Source File: Validator.java    From java with Apache License 2.0 5 votes vote down vote up
public static <T> void writeArgs(DataBuffer<T> buffer, int arrayLength, int offset, int length) {
  if (length > buffer.size()) {
    throw new BufferOverflowException();
  }
  if (buffer.isReadOnly()) {
    throw new ReadOnlyBufferException();
  }
  arrayArgs(arrayLength, offset, length);
}
 
Example #11
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetLong() {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    try {
        buf.setLong(0, 1);
    } finally {
        buf.release();
    }
}
 
Example #12
Source File: ReadOnlyDirectByteBufferBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetLong() {
    ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
    try {
        buf.setLong(0, 1);
    } finally {
        buf.release();
    }
}
 
Example #13
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesWithInputStream() throws IOException {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    try {
        buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4);
    } finally {
        buf.release();
    }
}
 
Example #14
Source File: ReadOnlyDirectByteBufferBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetMedium() {
    ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
    try {
        buf.setMedium(0, 1);
    } finally {
        buf.release();
    }
}
 
Example #15
Source File: ConnectionAttributesTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@Test
public void test_getAll_immutable() {
    final ImmutableMap<String, ByteBuffer> values = ImmutableMap.of(
            "test.key1", ByteBuffer.wrap("test.value1".getBytes()),
            "test.key2", ByteBuffer.wrap("test.value2".getBytes()));

    for (final Map.Entry<String, ByteBuffer> value : values.entrySet()) {
        connectionAttributes.put(value.getKey(), value.getValue());
    }

    final Optional<Map<String, ByteBuffer>> returnValues1 = connectionAttributes.getAll();

    assertTrue(returnValues1.isPresent());
    assertAllEquals(values, returnValues1.get());

    final AtomicInteger exceptions = new AtomicInteger(0);

    for (final String key : values.keySet()) {
        try {
            returnValues1.get().get(key).put(0, (byte) 10);
        } catch (final ReadOnlyBufferException e) {
            exceptions.incrementAndGet();
        }
    }

    final Optional<Map<String, ByteBuffer>> returnValues2 = connectionAttributes.getAll();

    assertTrue(returnValues2.isPresent());
    assertAllEquals(values, returnValues2.get());
    assertEquals(values.size(), exceptions.get());
}
 
Example #16
Source File: FixedCompositeByteBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetFloat() throws IOException {
    ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
    try {
        buf.setFloat(0, 1);
    } finally {
        buf.release();
    }
}
 
Example #17
Source File: ReadOnlyDirectByteBufferBufTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaBuffer() {
    ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
    ByteBuf copy = Unpooled.copyInt(1);
    try {
        buf.setBytes(0, copy);
    } finally {
        buf.release();
        copy.release();
    }
}
 
Example #18
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setBytes(int index, ByteBuffer src) {
    throw new ReadOnlyBufferException();
}
 
Example #19
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void _setMedium(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #20
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setMedium(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #21
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void _setShortLE(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #22
Source File: ReadOnlyByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public int setBytes(int index, InputStream in, int length) {
    throw new ReadOnlyBufferException();
}
 
Example #23
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setShortLE(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #24
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void _setShort(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #25
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setShort(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #26
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void _setByte(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #27
Source File: ReadOnlyByteBufferBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setByte(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #28
Source File: ReadOnlyByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setMediumLE(int index, int value) {
    throw new ReadOnlyBufferException();
}
 
Example #29
Source File: FixedCompositeByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public int setBytes(int index, FileChannel in, long position, int length) {
    throw new ReadOnlyBufferException();
}
 
Example #30
Source File: FixedCompositeByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) {
    throw new ReadOnlyBufferException();
}