@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()); }
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); } }
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(); }
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); }
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); }
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); }
/** * 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); } }
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; }
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); } }
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); } }
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); } }
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)); }
@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; }
@Test(expected = ReadOnlyBufferException.class) public void testSetInt() { ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); buffers.add(buf); buf.setInt(0, 1); }
@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(); }
public static void putUTF(ByteBuffer buffer, String str) throws BufferOverflowException, ReadOnlyBufferException { if (str == null || str.isEmpty()) { buffer.putInt(0); return; } byte[] bytes = str.getBytes(UTF16LE); buffer.putInt(bytes.length); buffer.put(bytes); }
@Override public long write(ByteBuffer buffer) throws BufferOverflowException, ReadOnlyBufferException{ /* write entry header */ buffer.put((byte)ENTRY_TYPE_TAG); buffer.put((byte)entrySize); byte[] hashtagBytes = hashtag.getBytes(Charset.forName("UTF-8")); /* write entry payload */ buffer.put((byte)levelOfInterest); buffer.put(hashtagBytes, 0, (byte)hashtagBytes.length); return (HEADER_SIZE+TAG_INTEREST_SIZE+(byte)hashtagBytes.length); }
@Override public byte[] array() { throw new ReadOnlyBufferException(); }
/** * Continues a multiple-part encryption or decryption operation * (depending on how this cipher was initialized), processing another data * part. * * <p>All <code>input.remaining()</code> bytes starting at * <code>input.position()</code> are processed. The result is stored * in the output buffer. * Upon return, the input buffer's position will be equal * to its limit; its limit will not have changed. The output buffer's * position will have advanced by n, where n is the value returned * by this method; the output buffer's limit will not have changed. * * <p>If <code>output.remaining()</code> bytes are insufficient to * hold the result, a <code>ShortBufferException</code> is thrown. * In this case, repeat this call with a larger output buffer. Use * {@link #getOutputSize(int) getOutputSize} to determine how big * the output buffer should be. * * <p>Note: this method should be copy-safe, which means the * <code>input</code> and <code>output</code> buffers can reference * the same block of memory and no unprocessed input data is overwritten * when the result is copied into the output buffer. * * @param input the input ByteBuffer * @param output the output ByteByffer * * @return the number of bytes stored in <code>output</code> * * @exception IllegalStateException if this cipher is in a wrong state * (e.g., has not been initialized) * @exception IllegalArgumentException if input and output are the * same object * @exception ReadOnlyBufferException if the output buffer is read-only * @exception ShortBufferException if there is insufficient space in the * output buffer * @since 1.5 */ public final int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { checkCipherState(); if ((input == null) || (output == null)) { throw new IllegalArgumentException("Buffers must not be null"); } if (input == output) { throw new IllegalArgumentException("Input and output buffers must " + "not be the same object, consider using buffer.duplicate()"); } if (output.isReadOnly()) { throw new ReadOnlyBufferException(); } chooseFirstProvider(); return spi.engineUpdate(input, output); }
/** * Encrypts or decrypts data in a single-part operation, or finishes a * multiple-part operation. The data is encrypted or decrypted, * depending on how this cipher was initialized. * * <p>All <code>input.remaining()</code> bytes starting at * <code>input.position()</code> are processed. * If an AEAD mode such as GCM/CCM is being used, the authentication * tag is appended in the case of encryption, or verified in the * case of decryption. * The result is stored in the output buffer. * Upon return, the input buffer's position will be equal * to its limit; its limit will not have changed. The output buffer's * position will have advanced by n, where n is the value returned * by this method; the output buffer's limit will not have changed. * * <p>If <code>output.remaining()</code> bytes are insufficient to * hold the result, a <code>ShortBufferException</code> is thrown. * In this case, repeat this call with a larger output buffer. Use * {@link #getOutputSize(int) getOutputSize} to determine how big * the output buffer should be. * * <p>Upon finishing, this method resets this cipher object to the state * it was in when previously initialized via a call to <code>init</code>. * That is, the object is reset and available to encrypt or decrypt * (depending on the operation mode that was specified in the call to * <code>init</code>) more data. * * <p>Note: if any exception is thrown, this cipher object may need to * be reset before it can be used again. * * <p>Note: this method should be copy-safe, which means the * <code>input</code> and <code>output</code> buffers can reference * the same byte array and no unprocessed input data is overwritten * when the result is copied into the output buffer. * * @param input the input ByteBuffer * @param output the output ByteBuffer * * @return the number of bytes stored in <code>output</code> * * @exception IllegalStateException if this cipher is in a wrong state * (e.g., has not been initialized) * @exception IllegalArgumentException if input and output are the * same object * @exception ReadOnlyBufferException if the output buffer is read-only * @exception IllegalBlockSizeException if this cipher is a block cipher, * no padding has been requested (only in encryption mode), and the total * input length of the data processed by this cipher is not a multiple of * block size; or if this encryption algorithm is unable to * process the input data provided. * @exception ShortBufferException if there is insufficient space in the * output buffer * @exception BadPaddingException if this cipher is in decryption mode, * and (un)padding has been requested, but the decrypted data is not * bounded by the appropriate padding bytes * @exception AEADBadTagException if this cipher is decrypting in an * AEAD mode (such as GCM/CCM), and the received authentication tag * does not match the calculated value * * @since 1.5 */ public final int doFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { checkCipherState(); if ((input == null) || (output == null)) { throw new IllegalArgumentException("Buffers must not be null"); } if (input == output) { throw new IllegalArgumentException("Input and output buffers must " + "not be the same object, consider using buffer.duplicate()"); } if (output.isReadOnly()) { throw new ReadOnlyBufferException(); } chooseFirstProvider(); return spi.engineDoFinal(input, output); }
/** * Continues a multiple-part encryption or decryption operation * (depending on how this cipher was initialized), processing another data * part. * * <p>All {@code input.remaining()} bytes starting at * {@code input.position()} are processed. The result is stored * in the output buffer. * Upon return, the input buffer's position will be equal * to its limit; its limit will not have changed. The output buffer's * position will have advanced by n, where n is the value returned * by this method; the output buffer's limit will not have changed. * * <p>If {@code output.remaining()} bytes are insufficient to * hold the result, a {@code ShortBufferException} is thrown. * In this case, repeat this call with a larger output buffer. Use * {@link #getOutputSize(int) getOutputSize} to determine how big * the output buffer should be. * * <p>Note: this method should be copy-safe, which means the * {@code input} and {@code output} buffers can reference * the same block of memory and no unprocessed input data is overwritten * when the result is copied into the output buffer. * * @param input the input ByteBuffer * @param output the output ByteByffer * * @return the number of bytes stored in {@code output} * * @exception IllegalStateException if this cipher is in a wrong state * (e.g., has not been initialized) * @exception IllegalArgumentException if input and output are the * same object * @exception ReadOnlyBufferException if the output buffer is read-only * @exception ShortBufferException if there is insufficient space in the * output buffer * @since 1.5 */ public final int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { checkCipherState(); if ((input == null) || (output == null)) { throw new IllegalArgumentException("Buffers must not be null"); } if (input == output) { throw new IllegalArgumentException("Input and output buffers must " + "not be the same object, consider using buffer.duplicate()"); } if (output.isReadOnly()) { throw new ReadOnlyBufferException(); } chooseFirstProvider(); return spi.engineUpdate(input, output); }
/** * Encrypts or decrypts data in a single-part operation, or finishes a * multiple-part operation. The data is encrypted or decrypted, * depending on how this cipher was initialized. * * <p>All {@code input.remaining()} bytes starting at * {@code input.position()} are processed. * If an AEAD mode such as GCM/CCM is being used, the authentication * tag is appended in the case of encryption, or verified in the * case of decryption. * The result is stored in the output buffer. * Upon return, the input buffer's position will be equal * to its limit; its limit will not have changed. The output buffer's * position will have advanced by n, where n is the value returned * by this method; the output buffer's limit will not have changed. * * <p>If {@code output.remaining()} bytes are insufficient to * hold the result, a {@code ShortBufferException} is thrown. * In this case, repeat this call with a larger output buffer. Use * {@link #getOutputSize(int) getOutputSize} to determine how big * the output buffer should be. * * <p>Upon finishing, this method resets this cipher object to the state * it was in when previously initialized via a call to {@code init}. * That is, the object is reset and available to encrypt or decrypt * (depending on the operation mode that was specified in the call to * {@code init}) more data. * * <p>Note: if any exception is thrown, this cipher object may need to * be reset before it can be used again. * * <p>Note: this method should be copy-safe, which means the * {@code input} and {@code output} buffers can reference * the same byte array and no unprocessed input data is overwritten * when the result is copied into the output buffer. * * @param input the input ByteBuffer * @param output the output ByteBuffer * * @return the number of bytes stored in {@code output} * * @exception IllegalStateException if this cipher is in a wrong state * (e.g., has not been initialized) * @exception IllegalArgumentException if input and output are the * same object * @exception ReadOnlyBufferException if the output buffer is read-only * @exception IllegalBlockSizeException if this cipher is a block cipher, * no padding has been requested (only in encryption mode), and the total * input length of the data processed by this cipher is not a multiple of * block size; or if this encryption algorithm is unable to * process the input data provided. * @exception ShortBufferException if there is insufficient space in the * output buffer * @exception BadPaddingException if this cipher is in decryption mode, * and (un)padding has been requested, but the decrypted data is not * bounded by the appropriate padding bytes * @exception AEADBadTagException if this cipher is decrypting in an * AEAD mode (such as GCM/CCM), and the received authentication tag * does not match the calculated value * * @since 1.5 */ public final int doFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { checkCipherState(); if ((input == null) || (output == null)) { throw new IllegalArgumentException("Buffers must not be null"); } if (input == output) { throw new IllegalArgumentException("Input and output buffers must " + "not be the same object, consider using buffer.duplicate()"); } if (output.isReadOnly()) { throw new ReadOnlyBufferException(); } chooseFirstProvider(); return spi.engineDoFinal(input, output); }
static void checkROBE(ThrowingRunnable r) { checkWithThrowable(ReadOnlyBufferException.class, null, r); }
static void checkROBE(Object message, ThrowingRunnable r) { checkWithThrowable(ReadOnlyBufferException.class, message, r); }
@Override public ChannelBuffer put(byte b) { throw new ReadOnlyBufferException(); }
@Override public ChannelBuffer put(byte[] src) { throw new ReadOnlyBufferException(); }
@Override public ChannelBuffer put(byte[] src, int offset, int length) { throw new ReadOnlyBufferException(); }
@Override public ChannelBuffer put(ChannelBuffer src) { throw new ReadOnlyBufferException(); }
@Override public ChannelBuffer put(int index, byte b) { throw new ReadOnlyBufferException(); }
@Override public ChannelBuffer putChar(char value) { throw new ReadOnlyBufferException(); }