Compact the buffer

@param buffer the buffer to compact
@return true if the compact made a full buffer have space
497976
public static boolean compact(ByteBuffer buffer) {
        if (buffer.position() == 0)
            return false;
        boolean full = buffer.limit() == buffer.capacity();
        buffer.compact().flip();
        return full && buffer.limit() < buffer.capacity();
    }
278097
protected ByteBuffer allocateBuffer() {
		if (_buffersToReuse != null && !_buffersToReuse.isEmpty()) {
			ByteBuffer bb = _buffersToReuse.poll();
			bb.rewind();
			bb.limit(bb.capacity());
			return bb;
		}
		ByteBuffer r = StaticBuffers.getInstance().byteBuffer(BUFFER_KEY, _bufferSize);
		r.limit(_bufferSize);
		return r.order(_order);
	}
362086
public boolean containsSymbol(char[] buffer, int offset, int length) {

        // search for identical symbol
        int bucket = hash(buffer, offset, length) % fTableSize;
        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
            if (length == entry.characters.length) {
                for (int i = 0; i < length; i++) {
                    if (buffer[offset + i] != entry.characters[i]) {
                        continue OUTER;
                    }
                }
                return true;
            }
        }

        return false;

    }
824941
public static final boolean hasRemaining(ByteBuffer[] buffers) {
    if (buffers == null) {
      return false;
    }
    for (int i = 0; i < buffers.length; i++) {
      if (buffers[i] != null && buffers[i].hasRemaining()) {
        return true;
      }
    }
    return false;
  }
643652
protected void releasing(ByteBuffer buffer) {
        if (buffer != null && buffer.isDirect()) {
            DirectByteBufferHelper directByteBufferHelper = this.directByteBufferHelper.get();
            if (directByteBufferHelper != null) {
                directByteBufferHelper.releaseDirectByteBuffer(buffer);
            }
        }
    }
969091
@Trivial
    public synchronized boolean isFull() {
        // final String methodName = "isFull()";
        boolean isFull = false;
        int size = this.explicitBuffer.size() + this.scanBuffer.size() + this.garbageCollectorBuffer.size();
        if (size > this.maxInvalidationBufferSize || (System.currentTimeMillis() - this.lastRemoveTime) >= this.maxInvalidationBufferLife) {
            isFull = true;
            setlastRemoveTime();
        }
        // if (isFull) {
        // traceDebug(methodName, "cacheName=" + this.cod.cacheName + " isFull=" + isFull + " explicitBuffer=" +
        // explicitBuffer.size() + " scanBuffer=" + this.scanBuffer.size());
        // }
        return isFull;
    }
25783
private function compactIntegerBuffer(ByteBuffer $buffer)
    {
        $length = $buffer->getLength();
        $raw = $buffer->getBinaryString();
        for ($i = 0; $i < ($length - 1); $i++) {
            if (ord($raw[$i]) !== 0) {
                break;
            }
        }
        if ($i !== 0) {
            return new ByteBuffer(\substr($raw, $i));
        }
        return $buffer;
    }
1042363
private boolean isClientBufferFull(final long now) {
        // check client buffer length when we've already sent some messages
        if (lastMessageTs > 0) {
            // duration the stream is playing / playback duration
            final long delta = now - playbackStart;
            // buffer size as requested by the client
            final long buffer = subscriberStream.getClientBufferDuration();
            // expected amount of data present in client buffer
            final long buffered = lastMessageTs - delta;
            log.trace("isClientBufferFull: timestamp {} delta {} buffered {} buffer duration {}", new Object[] { lastMessageTs, delta, buffered, buffer });
            // fix for SN-122, this sends double the size of the client buffer
            if (buffer > 0 && buffered > (buffer * 2)) {
                // client is likely to have enough data in the buffer
                return true;
            }
        }
        return false;
    }
334664
public final void compress(ByteBuffer src, ByteBuffer dest) {
    final int cpLen = compress(src, src.position(), src.remaining(), dest, dest.position(), dest.remaining());
    src.position(src.limit());
    dest.position(dest.position() + cpLen);
  }
354526
public void write(ByteBuffer buffer) {
    if (buffer.remaining() < 8196) {
      write(buffer.array(), buffer.arrayOffset() + buffer.position(),
          buffer.remaining());
    } else { // append w/o copying bytes
      ByteBuffer dup = buffer.duplicate();
      dup.position(buffer.limit()); // ready for flip
      buffers.add(dup);
    }
  }
737484
private void compactAndRemove(final E[] buffer, final int mask, int removeHashIndex) {
        // remove(9a): [9a,9b,10a,9c,10b,11a,null] -> [9b,9c,10a,10b,null,11a,null]
        removeHashIndex = removeHashIndex & mask;
        int j = removeHashIndex;
        while(true) {
            int k;
            // skip elements which belong where they are
            do {
                // j := (j+1) modulo num_slots
                j = (j + 1) & mask;
                // if slot[j] is unoccupied exit
                if (buffer[j] == null) {
                    // delete last duplicate slot
                    buffer[removeHashIndex] = null;
                    return;
                }

                // k := hash(slot[j].key) modulo num_slots
                k = System.identityHashCode(buffer[j]) & mask;
                // determine if k lies cyclically in [i,j]
                // |    i.k.j |
                // |....j i.k.| or  |.k..j i...|
            }
            while ( (removeHashIndex  <= j) ?
                    ((removeHashIndex < k) && (k <= j)) :
                    ((removeHashIndex < k) || (k <= j)) );
            // slot[removeHashIndex] := slot[j]
            buffer[removeHashIndex] = buffer[j];
            // removeHashIndex := j
            removeHashIndex = j;
        }
    }
89388
public function concat(Buffer $buffer)
    {
        // size-protected through Buffer class
        $this->buffer = $this->buffer . $buffer->getBinary();
        $this->size  += $buffer->getSize();
        return $this;
    }
913448
@Override
    protected boolean checkBuffer() throws IOException {
        if (null != this.buffer) {
            if (this.buffer.hasRemaining()) {
                return true;
            }
            this.buffer.release();
            this.buffer = null;
        }
        return setNextBuffer();
    }
860502
public static long sizeOnHeapOf(ByteBuffer buffer)
    {
        if (buffer.isDirect())
            return BUFFER_EMPTY_SIZE;
        // if we're only referencing a sub-portion of the ByteBuffer, don't count the array overhead (assume it's slab
        // allocated, so amortized over all the allocations the overhead is negligible and better to undercount than over)
        if (buffer.capacity() > buffer.remaining())
            return buffer.remaining();
        return BUFFER_EMPTY_SIZE + sizeOfArray(buffer.capacity(), 1);
    }
1284701
public static DynamicByteBuffer create(byte[] buffer ) {
        DynamicByteBuffer buf = new DynamicByteBuffer( buffer.length );
        buf.buffer = buffer;
        return buf;
    }
1037600
public static Buffer wrapAndClone(final byte[] buffer) {
        if (buffer == null || buffer.length == 0) {
            throw new IllegalArgumentException("the buffer cannot be null or empty");
        }

        final byte[] b = new byte[buffer.length];
        System.arraycopy(buffer, 0, b, 0, buffer.length);
        return new ByteBuffer(b);
    }
1113968
protected List<WsByteBuffer> compress(List<WsByteBuffer> list, WsByteBuffer buffer) {
        if (null == buffer) {
            return null;
        }
        int dataSize = buffer.remaining();
        if (0 == dataSize) {
            return list;
        }
        byte[] input = null;
        int initOffset = 0;
        if (buffer.hasArray()) {
            input = buffer.array();
            initOffset = buffer.arrayOffset() + buffer.position();
            buffer.position(buffer.limit());
        } else {
            input = new byte[dataSize];
            buffer.get(input);
        }
        this.deflater.setInput(input, initOffset, dataSize);

        // keep compressing data until we use up the input amount
        int offset = 0;
        while (!this.deflater.needsInput()) {
            int len = this.deflater.deflate(this.buf, offset, this.buf.length - offset);
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "Compressed amount=" + len + " read=" + this.deflater.getBytesRead() + " written=" + this.deflater.getBytesWritten());
            }
            if (0 == len) {
                break; // out of while
            }
            offset += len;
            if (offset == this.buf.length) {
                list.add(makeBuffer(offset));
                offset = 0;
            }
        }
        // if we generated output, put it into a buffer on the list
        if (0 < offset) {
            list.add(makeBuffer(offset));
        }
        return list;
    }