Exemple #1
1
 /**
  * Put a long value out to the specified BB position in big-endian format.
  *
  * @param buf the byte buffer
  * @param offset position in the buffer
  * @param val long to write out
  * @return incremented offset
  */
 public static int putLong(ByteBuffer buf, int offset, long val) {
   if (littleEndian) {
     val = Long.reverseBytes(val);
   }
   if (buf.isDirect()) {
     theUnsafe.putLong(((DirectBuffer) buf).address() + offset, val);
   } else {
     theUnsafe.putLong(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val);
   }
   return offset + Bytes.SIZEOF_LONG;
 }
Exemple #2
0
  /**
   * Writes all the remaining bytes of the {@code src} byte buffer to this buffer's current
   * position, and increases both buffers' position by the number of bytes copied.
   *
   * @param src the source byte buffer.
   * @return {@code this}
   * @throws BufferOverflowException if {@code src.remaining()} is greater than this buffer's {@code
   *     remaining()}.
   * @throws IllegalArgumentException if {@code src} is this buffer.
   * @throws ReadOnlyBufferException if no changes may be made to the contents of this buffer.
   */
  public ByteBuffer put(ByteBuffer src) {
    if (isReadOnly()) {
      throw new ReadOnlyBufferException();
    }
    if (src == this) {
      throw new IllegalArgumentException("src == this");
    }
    int srcByteCount = src.remaining();
    if (srcByteCount > remaining()) {
      throw new BufferOverflowException();
    }

    Object srcObject = src.isDirect() ? src : NioUtils.unsafeArray(src);
    int srcOffset = src.position();
    if (!src.isDirect()) {
      srcOffset += NioUtils.unsafeArrayOffset(src);
    }

    ByteBuffer dst = this;
    Object dstObject = dst.isDirect() ? dst : NioUtils.unsafeArray(dst);
    int dstOffset = dst.position();
    if (!dst.isDirect()) {
      dstOffset += NioUtils.unsafeArrayOffset(dst);
    }

    Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, srcByteCount);
    src.position(src.limit());
    dst.position(dst.position() + srcByteCount);

    return this;
  }
  /**
   * Creates a new direct buffer by wrapping the specified initial buffer.
   *
   * @param maxCapacity the maximum capacity of the underlying direct buffer
   */
  protected UnpooledDirectByteBuf(
      ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
    super(maxCapacity);
    if (alloc == null) {
      throw new NullPointerException("alloc");
    }
    if (initialBuffer == null) {
      throw new NullPointerException("initialBuffer");
    }
    if (!initialBuffer.isDirect()) {
      throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
    }
    if (initialBuffer.isReadOnly()) {
      throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
    }

    int initialCapacity = initialBuffer.remaining();
    if (initialCapacity > maxCapacity) {
      throw new IllegalArgumentException(
          String.format("initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
    }

    this.alloc = alloc;
    doNotFree = true;
    setByteBuffer(initialBuffer.slice().order(ByteOrder.BIG_ENDIAN));
    writerIndex(initialCapacity);
    leak = leakDetector.open(this);
  }
  /**
   * invokes the clean method on the ByteBuffer's cleaner
   *
   * @param buffer ByteBuffer
   * @return boolean true on success
   */
  private static boolean clean(final java.nio.ByteBuffer buffer) {
    if (buffer == null || !buffer.isDirect()) return false;

    Boolean b =
        (Boolean)
            AccessController.doPrivileged(
                new PrivilegedAction<Boolean>() {
                  public Boolean run() {
                    Boolean success = Boolean.FALSE;
                    try {
                      Method getCleanerMethod =
                          buffer.getClass().getMethod("cleaner", (Class<?>[]) null);
                      getCleanerMethod.setAccessible(true);
                      Object cleaner = getCleanerMethod.invoke(buffer, (Object[]) null);
                      Method clean = cleaner.getClass().getMethod("clean", (Class<?>[]) null);
                      clean.invoke(cleaner, (Object[]) null);
                      success = Boolean.TRUE;
                    } catch (Exception e) {
                      // This really is a show stopper on windows
                      // e.printStackTrace();
                    }
                    return success;
                  }
                });

    return b.booleanValue();
  }
    private void free(ByteBuffer oldBuf) {
      if ((oldBuf == null)
          || ((maxCachedBufferSize != 0) && (oldBuf.capacity() > maxCachedBufferSize))
          || oldBuf.isReadOnly()
          || isDerived()
          || (Thread.currentThread() != ownerThread)) {
        return;
      }

      // Add to the cache.
      Queue<CachedBuffer> pool;

      if (oldBuf.isDirect()) {
        pool = directBuffers.get().get(oldBuf.capacity());
      } else {
        pool = heapBuffers.get().get(oldBuf.capacity());
      }

      if (pool == null) {
        return;
      }

      // Restrict the size of the pool to prevent OOM.
      if ((maxPoolSize == 0) || (pool.size() < maxPoolSize)) {
        pool.offer(new CachedBuffer(oldBuf));
      }
    }
  protected long buildResponseByteBufferBuf(ByteBuffer b, long r, final long preChain) {
    if (b == null) {
      return 0;
    }

    if (!b.hasRemaining()) {
      return -NGX_HTTP_NO_CONTENT;
    }

    long chain = preChain;

    if (b.isDirect()) {
      chain =
          ngx_http_clojure_mem_build_temp_chain(
              r, preChain, null, ((DirectBuffer) b).address() + b.position(), b.remaining());
    } else {
      chain =
          ngx_http_clojure_mem_build_temp_chain(
              r, preChain, b.array(), BYTE_ARRAY_OFFSET, b.remaining());
    }

    b.position(b.limit());

    return chain;
  }
Exemple #7
0
 /**
  * Returns the byte at the given offset
  *
  * @param buf the buffer to read
  * @param offset the offset at which the byte has to be read
  * @return the byte at the given offset
  */
 public static byte toByte(ByteBuffer buf, int offset) {
   if (buf.isDirect()) {
     return theUnsafe.getByte(((DirectBuffer) buf).address() + offset);
   } else {
     return theUnsafe.getByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset);
   }
 }
Exemple #8
0
  /**
   * read from a file channel into a byte buffer, starting at a certain position.
   *
   * @param channel channel to read from
   * @param channelPosition position to read from
   * @param dest destination {@link java.nio.ByteBuffer} to put data in
   * @return total bytes read or -1 if an attempt was made to read past EOF. The method always tries
   *     to read all the bytes that will fit in the destination byte buffer.
   */
  public static int readFromFileChannel(FileChannel channel, long channelPosition, ByteBuffer dest)
      throws IOException {
    if (dest.isDirect() || (dest.remaining() < READ_CHUNK_SIZE)) {
      return readSingleChunk(channel, channelPosition, dest);
    } else {
      int bytesRead = 0;
      int bytesToRead = dest.remaining();

      // duplicate the buffer in order to be able to change the limit
      ByteBuffer tmpBuffer = dest.duplicate();
      try {
        while (dest.hasRemaining()) {
          tmpBuffer.limit(Math.min(dest.limit(), tmpBuffer.position() + READ_CHUNK_SIZE));
          int read = readSingleChunk(channel, channelPosition, tmpBuffer);
          if (read < 0) {
            return read;
          }
          bytesRead += read;
          channelPosition += read;
          dest.position(tmpBuffer.position());
        }
      } finally {
        // make sure we update byteBuffer to indicate how far we came..
        dest.position(tmpBuffer.position());
      }

      assert bytesRead == bytesToRead
          : "failed to read an entire buffer but also didn't get an EOF (read ["
              + bytesRead
              + "] needed ["
              + bytesToRead
              + "]";
      return bytesRead;
    }
  }
  /*
   * MIME type is cached so that Locator doesn't need to talk to the server,
   * it can just get the type and buffer and be done
   */
  public CacheReference registerURICache(URI sourceURI, ByteBuffer data, String mimeType) {
    if (Logger.canLog(Logger.DEBUG)) {
      Logger.logMsg(
          Logger.DEBUG,
          "New cache entry: URI " + sourceURI + ", buffer " + data + ", MIME type " + mimeType);
    }

    // ensure the given ByteBuffer is a direct buffer, required for native access
    if (!data.isDirect()) {
      data.rewind();
      ByteBuffer newData = ByteBuffer.allocateDirect(data.capacity());
      newData.put(data);
      data = newData;
    }

    CacheReference ref = new CacheReference(data, mimeType);
    synchronized (uriCache) {
      uriCache.put(sourceURI, new WeakReference(ref));
    }

    // Now register sourceURI with the disposer so we can remove it
    // from uriCache when our cache entry gets collected
    MediaDisposer.addResourceDisposer(ref, sourceURI, cacheDisposer);
    return ref;
  }
Exemple #10
0
 /**
  * Creates a new buffer which wraps the specified NIO buffer's current slice. A modification on
  * the specified buffer's content will be visible to the returned buffer.
  */
 public static ByteBuf wrappedBuffer(ByteBuffer buffer) {
   if (!buffer.hasRemaining()) {
     return EMPTY_BUFFER;
   }
   if (buffer.hasArray()) {
     return wrappedBuffer(
             buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining())
         .order(buffer.order());
   } else if (PlatformDependent.hasUnsafe()) {
     if (buffer.isReadOnly()) {
       if (buffer.isDirect()) {
         return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer);
       } else {
         return new ReadOnlyByteBufferBuf(ALLOC, buffer);
       }
     } else {
       return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining());
     }
   } else {
     if (buffer.isReadOnly()) {
       return new ReadOnlyByteBufferBuf(ALLOC, buffer);
     } else {
       return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining());
     }
   }
 }
 public static FMOD_RESULT Memory_Initialize(
     ByteBuffer poolmem,
     int poollen,
     FMOD_MEMORY_ALLOCCALLBACK useralloc,
     FMOD_MEMORY_REALLOCCALLBACK userrealloc,
     FMOD_MEMORY_FREECALLBACK userfree,
     int memtypeflags) {
   if (poolmem != null && !poolmem.isDirect()) {
     throw new NonDirectBufferException();
   }
   if (useralloc != null && userrealloc != null && userfree != null) {
     CallbackManager.addCallback(25, useralloc, 0);
     CallbackManager.addCallback(27, userrealloc, 0);
     CallbackManager.addCallback(26, userfree, 0);
   } else {
     useralloc = null;
     userrealloc = null;
     userfree = null;
   }
   int javaResult =
       FmodExJNI.FmodEx_Memory_Initialize(
           poolmem,
           BufferUtils.getPositionInBytes(poolmem),
           poollen,
           useralloc == null ? false : true,
           userrealloc == null ? false : true,
           userfree == null ? false : true,
           memtypeflags);
   return FMOD_RESULT.get(javaResult);
 }
 /**
  * Offset, rowStride, and pixelStride are given in bytes. Height and width are given in pixels.
  */
 private void writeByteBuffer(
     int width,
     int height,
     ByteBuffer pixels,
     OutputStream dngOutput,
     int pixelStride,
     int rowStride,
     long offset)
     throws IOException {
   if (width <= 0 || height <= 0) {
     throw new IllegalArgumentException(
         "Image with invalid width, height: (" + width + "," + height + ") passed to write");
   }
   long capacity = pixels.capacity();
   long totalSize = rowStride * height + offset;
   if (capacity < totalSize) {
     throw new IllegalArgumentException(
         "Image size " + capacity + " is too small (must be larger than " + totalSize + ")");
   }
   int minRowStride = pixelStride * width;
   if (minRowStride > rowStride) {
     throw new IllegalArgumentException(
         "Invalid image pixel stride, row byte width "
             + minRowStride
             + " is too large, expecting "
             + rowStride);
   }
   pixels.clear(); // Reset mark and limit
   nativeWriteImage(
       dngOutput, width, height, pixels, rowStride, pixelStride, offset, pixels.isDirect());
   pixels.clear();
 }
Exemple #13
0
 /**
  * Continues a multiple-part encryption or decryption 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. The result is stored in the output buffer.
  *
  * <p>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, when 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.
  *
  * @param input the input ByteBuffer
  * @param output the output ByteBuffer
  * @return int number of bytes stored in <code>output</code>
  * @throws ShortBufferException if there is insufficient space in the output buffer
  */
 public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
   checkState();
   Preconditions.checkArgument(
       input.isDirect() && output.isDirect(), "Direct buffers are required.");
   int len =
       update(
           context,
           input,
           input.position(),
           input.remaining(),
           output,
           output.position(),
           output.remaining());
   input.position(input.limit());
   output.position(output.position() + len);
   return len;
 }
Exemple #14
0
 /**
  * Put a byte value out to the specified BB position in big-endian format.
  *
  * @param buf the byte buffer
  * @param offset position in the buffer
  * @param b byte to write out
  * @return incremented offset
  */
 public static int putByte(ByteBuffer buf, int offset, byte b) {
   if (buf.isDirect()) {
     theUnsafe.putByte(((DirectBuffer) buf).address() + offset, b);
   } else {
     theUnsafe.putByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, b);
   }
   return offset + 1;
 }
Exemple #15
0
 public static final void checkBuffer(final ByteBuffer buffer) {
   if (buffer == null) {
     throw new IllegalArgumentException("buffer == null");
   }
   if (!buffer.isDirect()) {
     throw new IllegalArgumentException("must use DirectByteBuffer");
   }
 }
Exemple #16
0
 /**
  * Finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this
  * cipher was initialized.
  *
  * <p>The result is stored in the output buffer. Upon return, 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.
  *
  * <p>Upon finishing, this method resets this cipher object to the state it was in when previously
  * initialized. That is, the object is available to encrypt or decrypt more data.
  *
  * <p>If any exception is thrown, this cipher object need to be reset before it can be used again.
  *
  * @param output the output ByteBuffer
  * @return int number of bytes stored in <code>output</code>
  * @throws ShortBufferException
  * @throws IllegalBlockSizeException
  * @throws BadPaddingException
  */
 public int doFinal(ByteBuffer output)
     throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
   checkState();
   Preconditions.checkArgument(output.isDirect(), "Direct buffer is required.");
   int len = doFinal(context, output, output.position(), output.remaining());
   output.position(output.position() + len);
   return len;
 }
Exemple #17
0
 private void readDirect(ByteBuffer buf) throws IOException {
   if (!buf.isDirect()) {
     throw new IllegalArgumentException("need direct buffer");
   }
   final int pos = buf.position();
   final int sz = read(kfsAccess.getCPtr(), kfsFd, buf, pos, buf.limit());
   kfsAccess.kfs_retToIOException(sz);
   buf.position(pos + sz);
 }
Exemple #18
0
 /**
  * Force to unmap direct buffer if the buffer is no longer used. It is unsafe operation and
  * currently a walk-around to avoid huge memory occupation caused by memory map.
  *
  * @param buffer the byte buffer to be unmapped
  */
 public static void cleanDirectBuffer(ByteBuffer buffer) {
   if (buffer == null) {
     return;
   }
   if (buffer.isDirect()) {
     Cleaner cleaner = ((DirectBuffer) buffer).cleaner();
     cleaner.clean();
   }
 }
Exemple #19
0
 /**
  * Copies specified number of bytes from given offset of {@code src} buffer into the {@code dest}
  * buffer.
  *
  * @param src
  * @param srcOffset
  * @param dest
  * @param destOffset
  * @param length
  */
 public static void copy(
     ByteBuffer src, int srcOffset, ByteBuffer dest, int destOffset, int length) {
   long srcAddress, destAddress;
   Object srcBase = null, destBase = null;
   if (src.isDirect()) {
     srcAddress = srcOffset + ((DirectBuffer) src).address();
   } else {
     srcAddress = srcOffset + src.arrayOffset() + BYTE_ARRAY_BASE_OFFSET;
     srcBase = src.array();
   }
   if (dest.isDirect()) {
     destAddress = destOffset + ((DirectBuffer) dest).address();
   } else {
     destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset();
     destBase = dest.array();
   }
   unsafeCopy(srcBase, srcAddress, destBase, destAddress, length);
 }
 public static void clean(ByteBuffer buffer) {
   if (buffer == null) return;
   if ((CLEAN_SUPPORTED) && (buffer.isDirect()))
     try {
       Object cleaner = directBufferCleaner.invoke(buffer, new Object[0]);
       directBufferCleanerClean.invoke(cleaner, new Object[0]);
     } catch (Exception localException) {
     }
 }
 public void putBuffer(ByteBuffer buf) {
   if (buf.isDirect()) {
     buf.clear(); // 초기화 시켜준다.
     synchronized (queue) {
       queue.add(buf);
       queue.notifyAll();
     }
   }
 }
  @Override
  @ForceInline
  public void write(long offsetInRDO, @NotNull ByteBuffer bytes, int offset, int length) {
    if (bytes.isDirect()) {
      memory.copyMemory(((DirectBuffer) bytes).address(), address + translate(offsetInRDO), length);

    } else {
      memory.copyMemory(bytes.array(), offset, address + translate(offsetInRDO), length);
    }
  }
 public ProgressHandler(
     SWIGTYPE_p_direct_buffer pointer, ByteBuffer buffer, int stepsPerCallback) {
   myStepsPerCallback = stepsPerCallback;
   assert buffer.isDirect();
   assert buffer.capacity() == 16 : buffer.capacity();
   myPointer = pointer;
   myBuffer = buffer;
   myLongs = buffer.order(ByteOrder.nativeOrder()).asLongBuffer();
   assert myLongs.capacity() == 2;
 }
Exemple #24
0
 private void expand(int expandSize) {
   snapshot();
   ByteBuffer newBuff =
       (buffer.isDirect()
           ? ByteBuffer.allocateDirect(buffer.capacity() + expandSize)
           : ByteBuffer.allocate(buffer.capacity() + expandSize));
   buffer.flip();
   newBuff.put(buffer);
   buffer = newBuff;
   reset();
 }
  public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
    if (!nioBuffer.isReadOnly() && nioBuffer.isDirect()) {
      return ChannelBuffers.wrappedBuffer(nioBuffer);
    }

    ChannelBuffer buf = getBuffer(nioBuffer.order(), nioBuffer.remaining());
    int pos = nioBuffer.position();
    buf.writeBytes(nioBuffer);
    nioBuffer.position(pos);
    return buf;
  }
Exemple #26
0
 public void send(ByteBuffer buf) throws Exception {
   if (buf == null) return;
   int offset = buf.hasArray() ? buf.arrayOffset() + buf.position() : buf.position(),
       len = buf.remaining();
   if (!buf.isDirect()) send(buf.array(), offset, len);
   else { // by default use a copy; but of course implementers of Receiver can override this
     byte[] tmp = new byte[len];
     buf.get(tmp, 0, len);
     send(tmp, 0, len); // will get copied again if send-queues are enabled
   }
 }
  /** {@inheritDoc} */
  @Override
  public void setBuffer(ByteBuffer buf) {
    assert buf != null;

    if (this.buf != buf) {
      this.buf = buf;

      heapArr = buf.isDirect() ? null : buf.array();
      baseOff = buf.isDirect() ? ((DirectBuffer) buf).address() : BYTE_ARR_OFF;
    }
  }
Exemple #28
0
 public AsyncWrite(ByteBuffer buffer, boolean complete) {
   _buffer = buffer;
   _len = buffer.remaining();
   // Use a slice buffer for large indirect to avoid JVM pooling large direct buffers
   if (_buffer.isDirect() || _len < getBufferSize()) _slice = null;
   else {
     _slice = _buffer.duplicate();
     _buffer.position(_buffer.limit());
   }
   _complete = complete;
 }
Exemple #29
0
 /**
  * Put an int value out to the specified ByteBuffer offset in big-endian format.
  *
  * @param buf the ByteBuffer to write to
  * @param offset offset in the ByteBuffer
  * @param val int to write out
  * @return incremented offset
  */
 public static int putInt(ByteBuffer buf, int offset, int val) {
   if (littleEndian) {
     val = Integer.reverseBytes(val);
   }
   if (buf.isDirect()) {
     theUnsafe.putInt(((DirectBuffer) buf).address() + offset, val);
   } else {
     theUnsafe.putInt(buf.array(), offset + buf.arrayOffset() + BYTE_ARRAY_BASE_OFFSET, val);
   }
   return offset + Bytes.SIZEOF_INT;
 }
Exemple #30
0
 /**
  * Put a short value out to the specified BB position in big-endian format.
  *
  * @param buf the byte buffer
  * @param offset position in the buffer
  * @param val short to write out
  * @return incremented offset
  */
 public static int putShort(ByteBuffer buf, int offset, short val) {
   if (littleEndian) {
     val = Short.reverseBytes(val);
   }
   if (buf.isDirect()) {
     theUnsafe.putShort(((DirectBuffer) buf).address() + offset, val);
   } else {
     theUnsafe.putShort(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val);
   }
   return offset + Bytes.SIZEOF_SHORT;
 }