Java Code Examples for java.nio.ByteBuffer.isReadOnly()

The following are Jave code examples for showing how to use isReadOnly() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: IOUtil.java   View Source Code Vote up 6 votes
static int read(FileDescriptor fd, ByteBuffer dst, long position,
                NativeDispatcher nd)
    throws IOException
{
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    if (dst instanceof DirectBuffer)
        return readIntoNativeBuffer(fd, dst, position, nd);

    // Substitute a native buffer
    ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
    try {
        int n = readIntoNativeBuffer(fd, bb, position, nd);
        bb.flip();
        if (n > 0)
            dst.put(bb);
        return n;
    } finally {
        Util.offerFirstTemporaryDirectBuffer(bb);
    }
}
 
Example 2
Project: OpenJSharp   File: IOUtil.java   View Source Code Vote up 6 votes
static int read(FileDescriptor fd, ByteBuffer dst, long position,
                NativeDispatcher nd)
    throws IOException
{
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    if (dst instanceof DirectBuffer)
        return readIntoNativeBuffer(fd, dst, position, nd);

    // Substitute a native buffer
    ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
    try {
        int n = readIntoNativeBuffer(fd, bb, position, nd);
        bb.flip();
        if (n > 0)
            dst.put(bb);
        return n;
    } finally {
        Util.offerFirstTemporaryDirectBuffer(bb);
    }
}
 
Example 3
Project: jdk8u-jdk   File: IOUtil.java   View Source Code Vote up 6 votes
static int read(FileDescriptor fd, ByteBuffer dst, long position,
                NativeDispatcher nd)
    throws IOException
{
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    if (dst instanceof DirectBuffer)
        return readIntoNativeBuffer(fd, dst, position, nd);

    // Substitute a native buffer
    ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
    try {
        int n = readIntoNativeBuffer(fd, bb, position, nd);
        bb.flip();
        if (n > 0)
            dst.put(bb);
        return n;
    } finally {
        Util.offerFirstTemporaryDirectBuffer(bb);
    }
}
 
Example 4
Project: dubbo2   File: DirectChannelBufferFactory.java   View Source Code Vote up 5 votes
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
    if (!nioBuffer.isReadOnly() && nioBuffer.isDirect()) {
        return ChannelBuffers.wrappedBuffer(nioBuffer);
    }

    ChannelBuffer buf = getBuffer(nioBuffer.remaining());
    int pos = nioBuffer.position();
    buf.writeBytes(nioBuffer);
    nioBuffer.position(pos);
    return buf;
}
 
Example 5
Project: dubbocloud   File: DirectChannelBufferFactory.java   View Source Code Vote up 5 votes
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
    if (!nioBuffer.isReadOnly() && nioBuffer.isDirect()) {
        return ChannelBuffers.wrappedBuffer(nioBuffer);
    }

    ChannelBuffer buf = getBuffer(nioBuffer.remaining());
    int pos = nioBuffer.position();
    buf.writeBytes(nioBuffer);
    nioBuffer.position(pos);
    return buf;
}
 
Example 6
Project: dubbox-hystrix   File: DirectChannelBufferFactory.java   View Source Code Vote up 5 votes
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
    if (!nioBuffer.isReadOnly() && nioBuffer.isDirect()) {
        return ChannelBuffers.wrappedBuffer(nioBuffer);
    }

    ChannelBuffer buf = getBuffer(nioBuffer.remaining());
    int pos = nioBuffer.position();
    buf.writeBytes(nioBuffer);
    nioBuffer.position(pos);
    return buf;
}
 
Example 7
Project: memory   File: WritableMemory.java   View Source Code Vote up 5 votes
/**
 * Accesses the given ByteBuffer for write operations.
 * @param byteBuf the given ByteBuffer
 * @return the given ByteBuffer for write operations.
 */
public static WritableMemory wrap(final ByteBuffer byteBuf) {
  if (byteBuf.isReadOnly()) {
    throw new ReadOnlyException("ByteBuffer is read-only.");
  }
  if (byteBuf.capacity() == 0) {
    return WritableMemoryImpl.ZERO_SIZE_MEMORY;
  }
  final ResourceState state = new ResourceState();
  state.putByteBuffer(byteBuf);
  AccessByteBuffer.wrap(state);
  final WritableMemoryImpl impl = new WritableMemoryImpl(state);
  return impl;
}
 
Example 8
Project: memory   File: WritableBuffer.java   View Source Code Vote up 5 votes
/**
 * Accesses the given ByteBuffer for write operations.
 * @param byteBuf the given ByteBuffer
 * @return the given ByteBuffer for write operations.
 */
public static WritableBuffer wrap(final ByteBuffer byteBuf) {
  if (byteBuf.isReadOnly()) {
    throw new ReadOnlyException("ByteBuffer is read-only.");
  }
  if (byteBuf.capacity() == 0) {
    return WritableBufferImpl.ZERO_SIZE_BUFFER;
  }
  final ResourceState state = new ResourceState();
  state.putByteBuffer(byteBuf);
  AccessByteBuffer.wrap(state);
  final WritableBufferImpl impl = new WritableBufferImpl(state);
  impl.setStartPositionEnd(0, byteBuf.position(), byteBuf.limit());
  return impl;
}
 
Example 9
Project: GitHub   File: ByteBufferUtil.java   View Source Code Vote up 4 votes
private static SafeArray getSafeArray(ByteBuffer byteBuffer) {
  if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) {
    return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
  }
  return null;
}
 
Example 10
Project: GitHub   File: ByteBufferUtil.java   View Source Code Vote up 4 votes
private static SafeArray getSafeArray(ByteBuffer byteBuffer) {
  if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) {
    return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
  }
  return null;
}
 
Example 11
Project: openjdk-jdk10   File: Cipher.java   View Source Code Vote up 4 votes
/**
 * 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);
}
 
Example 12
Project: openjdk-jdk10   File: AsynchronousSocketChannelImpl.java   View Source Code Vote up 4 votes
@Override
public final Future<Integer> read(ByteBuffer dst) {
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    return read(false, dst, null, 0L, TimeUnit.MILLISECONDS, null, null);
}
 
Example 13
Project: openjdk-jdk10   File: WindowsAsynchronousFileChannelImpl.java   View Source Code Vote up 4 votes
@Override
<A> Future<Integer> implRead(ByteBuffer dst,
                             long position,
                             A attachment,
                             CompletionHandler<Integer,? super A> handler)
{
    if (!reading)
        throw new NonReadableChannelException();
    if (position < 0)
        throw new IllegalArgumentException("Negative position");
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");

    // check if channel is closed
    if (!isOpen()) {
        Throwable exc = new ClosedChannelException();
        if (handler == null)
            return CompletedFuture.withFailure(exc);
        Invoker.invoke(this, handler, attachment, null, exc);
        return null;
    }

    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    // no space remaining
    if (rem == 0) {
        if (handler == null)
            return CompletedFuture.withResult(0);
        Invoker.invoke(this, handler, attachment, 0, null);
        return null;
    }

    // create Future and task that initiates read
    PendingFuture<Integer,A> result =
        new PendingFuture<Integer,A>(this, handler, attachment);
    ReadTask<A> readTask = new ReadTask<A>(dst, pos, rem, position, result);
    result.setContext(readTask);

    // initiate I/O
    readTask.run();
    return result;
}
 
Example 14
Project: openjdk-jdk10   File: Cipher.java   View Source Code Vote up 4 votes
/**
 * 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);
}
 
Example 15
Project: OpenJSharp   File: Cipher.java   View Source Code Vote up 4 votes
/**
 * 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);
}
 
Example 16
Project: OpenJSharp   File: WindowsAsynchronousFileChannelImpl.java   View Source Code Vote up 4 votes
@Override
<A> Future<Integer> implRead(ByteBuffer dst,
                             long position,
                             A attachment,
                             CompletionHandler<Integer,? super A> handler)
{
    if (!reading)
        throw new NonReadableChannelException();
    if (position < 0)
        throw new IllegalArgumentException("Negative position");
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");

    // check if channel is closed
    if (!isOpen()) {
        Throwable exc = new ClosedChannelException();
        if (handler == null)
            return CompletedFuture.withFailure(exc);
        Invoker.invoke(this, handler, attachment, null, exc);
        return null;
    }

    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    // no space remaining
    if (rem == 0) {
        if (handler == null)
            return CompletedFuture.withResult(0);
        Invoker.invoke(this, handler, attachment, 0, null);
        return null;
    }

    // create Future and task that initiates read
    PendingFuture<Integer,A> result =
        new PendingFuture<Integer,A>(this, handler, attachment);
    ReadTask<A> readTask = new ReadTask<A>(dst, pos, rem, position, result);
    result.setContext(readTask);

    // initiate I/O
    if (Iocp.supportsThreadAgnosticIo()) {
        readTask.run();
    } else {
        Invoker.invokeOnThreadInThreadPool(this, readTask);
    }
    return result;
}
 
Example 17
Project: OpenJSharp   File: LinuxUserDefinedFileAttributeView.java   View Source Code Vote up 4 votes
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 18
Project: OpenJSharp   File: WindowsAsynchronousFileChannelImpl.java   View Source Code Vote up 4 votes
@Override
<A> Future<Integer> implRead(ByteBuffer dst,
                             long position,
                             A attachment,
                             CompletionHandler<Integer,? super A> handler)
{
    if (!reading)
        throw new NonReadableChannelException();
    if (position < 0)
        throw new IllegalArgumentException("Negative position");
    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");

    // check if channel is closed
    if (!isOpen()) {
        Throwable exc = new ClosedChannelException();
        if (handler == null)
            return CompletedFuture.withFailure(exc);
        Invoker.invoke(this, handler, attachment, null, exc);
        return null;
    }

    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    // no space remaining
    if (rem == 0) {
        if (handler == null)
            return CompletedFuture.withResult(0);
        Invoker.invoke(this, handler, attachment, 0, null);
        return null;
    }

    // create Future and task that initiates read
    PendingFuture<Integer,A> result =
        new PendingFuture<Integer,A>(this, handler, attachment);
    ReadTask<A> readTask = new ReadTask<A>(dst, pos, rem, position, result);
    result.setContext(readTask);

    // initiate I/O
    if (Iocp.supportsThreadAgnosticIo()) {
        readTask.run();
    } else {
        Invoker.invokeOnThreadInThreadPool(this, readTask);
    }
    return result;
}
 
Example 19
Project: memory   File: AccessByteBuffer.java   View Source Code Vote up 4 votes
static ResourceState wrap(final ResourceState state) {
  final ByteBuffer byteBuf = state.getByteBuffer();
  state.putCapacity(byteBuf.capacity());
  final boolean readOnlyBB = byteBuf.isReadOnly();

  final boolean direct = byteBuf.isDirect();

  if (readOnlyBB) {
    state.setResourceReadOnly();

    //READ-ONLY DIRECT
    if (direct) {
      //address() is already adjusted for direct slices, so regionOffset = 0
      state.putNativeBaseOffset(((sun.nio.ch.DirectBuffer) byteBuf).address());
      return state;
    }

    //READ-ONLY HEAP
    //The messy acquisition of arrayOffset() and array() created from a RO slice()
    final Object unsafeObj;
    final long regionOffset;
    try {
      Field field = ByteBuffer.class.getDeclaredField("offset");
      field.setAccessible(true);
      //includes the slice() offset for heap.
      regionOffset = ((Integer)field.get(byteBuf)).longValue() * ARRAY_BYTE_INDEX_SCALE;

      field = ByteBuffer.class.getDeclaredField("hb"); //the backing byte[] from HeapByteBuffer
      field.setAccessible(true);
      unsafeObj = field.get(byteBuf);
    }
    catch (final IllegalAccessException | NoSuchFieldException e) {
      throw new RuntimeException(
              "Could not get offset/byteArray from OnHeap ByteBuffer instance: " + e.getClass());
    }
    state.putUnsafeObjectHeader(ARRAY_BYTE_BASE_OFFSET);
    state.putUnsafeObject(unsafeObj);
    state.putRegionOffset(regionOffset);
    return state;
  }

  else { //BB is WRITABLE.

    //WRITABLE-DIRECT  //nativeBaseAddress, byteBuf, capacity
    if (direct) {
      //address() is already adjusted for direct slices, so regionOffset = 0
      state.putNativeBaseOffset(((sun.nio.ch.DirectBuffer) byteBuf).address());
      return state;
    }

    //WRITABLE-HEAP  //unsafeObj, unsafeObjHeader, bytBuf, regionOffset, capacity
    state.putUnsafeObject(byteBuf.array());
    state.putUnsafeObjectHeader(ARRAY_BYTE_BASE_OFFSET);
    state.putRegionOffset(byteBuf.arrayOffset() * ARRAY_BYTE_INDEX_SCALE);
    return state;
  }
}
 
Example 20
Project: jdk8u-jdk   File: Cipher.java   View Source Code Vote up 4 votes
/**
 * 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);
}