当前位置: 首页>>代码示例>>Java>>正文


Java ByteBuffer.isReadOnly方法代码示例

本文整理汇总了Java中java.nio.ByteBuffer.isReadOnly方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.isReadOnly方法的具体用法?Java ByteBuffer.isReadOnly怎么用?Java ByteBuffer.isReadOnly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.ByteBuffer的用法示例。


在下文中一共展示了ByteBuffer.isReadOnly方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: read

import java.nio.ByteBuffer; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:IOUtil.java

示例2: getBuffer

import java.nio.ByteBuffer; //导入方法依赖的package包/类
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;
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:12,代码来源:DirectChannelBufferFactory.java

示例3: wrap

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:DataSketches,项目名称:memory,代码行数:19,代码来源:WritableMemory.java

示例4: wrap

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:DataSketches,项目名称:memory,代码行数:20,代码来源:WritableBuffer.java

示例5: getSafeArray

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static SafeArray getSafeArray(ByteBuffer byteBuffer) {
  if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) {
    return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
  }
  return null;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:7,代码来源:ByteBufferUtil.java

示例6: update

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:57,代码来源:Cipher.java

示例7: read

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:AsynchronousSocketChannelImpl.java

示例8: implRead

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@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;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:WindowsAsynchronousFileChannelImpl.java

示例9: doFinal

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:82,代码来源:Cipher.java

示例10: doFinal

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:82,代码来源:Cipher.java

示例11: implRead

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:50,代码来源:WindowsAsynchronousFileChannelImpl.java

示例12: read

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@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();
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:56,代码来源:LinuxUserDefinedFileAttributeView.java

示例13: wrap

import java.nio.ByteBuffer; //导入方法依赖的package包/类
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;
  }
}
 
开发者ID:DataSketches,项目名称:memory,代码行数:58,代码来源:AccessByteBuffer.java

示例14: update

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:57,代码来源:Cipher.java


注:本文中的java.nio.ByteBuffer.isReadOnly方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。