Helper method for toBytesSparse

@param buf Destination buffer
@param nonEmptyBuckets Number of non-empty buckets in the histogram
502987
public void writeByteBufferSparse(ByteBuffer buf, int nonEmptyBuckets)
  {
    writeByteBufferSerdeHeader(buf, SPARSE_ENCODING_MODE);
    writeByteBufferCommonFields(buf);

    buf.putInt(nonEmptyBuckets);
    int bucketsWritten = 0;
    for (int i = 0; i < numBuckets; i++) {
      if (histogram[i] > 0) {
        buf.putInt(i);
        buf.putLong(histogram[i]);
        bucketsWritten += 1;
      }
      if (bucketsWritten == nonEmptyBuckets) {
        break;
      }
    }
  }
1127637
public void write(ByteBuffer bb, VBucketCoordinates coords) {
        if (!enabled) {
            return;
        }
        bb.putLong(24, coords.getUuid());
        bb.putLong(32, coords.getSeqno());

    }
53553
public void toBytesDense(ByteBuffer buf)
  {
    buf.putInt(size);
    buf.putInt(binCount);

    buf.asFloatBuffer().put(positions);
    buf.position(buf.position() + Float.BYTES * positions.length);
    buf.asLongBuffer().put(bins);
    buf.position(buf.position() + Long.BYTES * bins.length);

    buf.putFloat(min);
    buf.putFloat(max);
  }
855532
function buffer_empty(buf) {
  for (var i = buf.length - 1; i > 0; i--) {
    if (buf[i]) { return false; }
  }

  return true;
}
204627
function TokenBucket(timespan, limit, { distFactor = 1, factor = 20, spread = 500 / timespan, now = Date.now } = {}) {
  this.now = now;

  this.timespan = timespan;
  // this.givenLimit is the limit given to the constructor, this.limit is the
  // functional limit, accounting for this.distFactor.
  this.givenLimit = limit;
  this.factor = factor;
  this.spread = spread;
  this.timespanIndex = Math.ceil(timespan / factor);
  this.total = 0;
  this.time = -1;
  this.buffer = new Array(this.factor + 1).fill(0);

  this.setDistFactor(distFactor);
}
184140
@Override
    public void asBytes(ByteBuffer buf) {
        buf.putInt(VERBOSE_ENCODING);
        buf.putDouble(min);
        buf.putDouble(max);
        buf.putDouble((float) compression());
        buf.putInt(summary.size());
        for (Centroid centroid : summary) {
            buf.putDouble(centroid.mean());
        }

        for (Centroid centroid : summary) {
            buf.putInt(centroid.count());
        }
    }
805839
public void serialize(final ByteBuffer buf) {

		checkInvariants();

		if (!sizeIsSticky)
			trimToSize();

		buf.putInt(wordsInUse);
		for (int i = 0; i < wordsInUse; i++) {
			buf.putLong(words[i]);
		}
	}
1273383
public void flattenToBuffer(ByteBuffer buf) {
        ByteBuffer dup = m_buffer.duplicate();
        buf.putInt(dup.limit());
        dup.position(0);
        buf.put(dup);
    }
844586
public synchronized long write(final ByteBuffer buf) {
		if (!validState) {
			throw new InvalidStateException();
		}
		final int packet_size = (HEADER_LEN + buf.limit() + FOOTER_LEN); // short + int + data + byte
		final boolean useDirectIO = (packet_size > (1 << bits));
		try {
			if (useDirectIO) {
				log.warn("WARN: usingDirectIO packet size is greater (" + packet_size
						+ ") than file buffer (" + bufOutput.capacity() + ")");
			}
			// Align output
			if (alignBlocks && !useDirectIO) {
				final int diffOffset = nextBlockBoundary(offsetOutputUncommited);
				if (packet_size > diffOffset) {
					// log.warn("WARN: aligning offset=" + offsetOutputUncommited + " to=" +
					// (offsetOutputUncommited+diffOffset) + " needed=" + packet_size + " allowed=" +
					// diffOffset);
					alignBuffer(diffOffset);
					offsetOutputUncommited += diffOffset;
				}
			}
			// Remember current offset
			final long offset = offsetOutputUncommited;
			// Write pending buffered data to disk
			if (bufOutput.remaining() < packet_size) {
				flushBuffer();
			}
			// Write new data to buffer
			bufOutput.put((byte) ((MAGIC >> 8) & 0xFF)); 	// Header - Magic (short, 2 bytes, msb-first)
			bufOutput.put((byte) (MAGIC & 0xFF)); 		// Header - Magic (short, 2 bytes, lsb-last)
			bufOutput.putInt(buf.limit()); 				// Header - Data Size (int, 4 bytes)
			if (useDirectIO) {
				bufOutput.flip();
				fcOutput.write(new ByteBuffer[] {
						bufOutput, buf, ByteBuffer.wrap(new byte[] {
							MAGIC_FOOT
						})
				}); // Write Header + Data + Footer
				bufOutput.clear();
				offsetOutputUncommited = offsetOutputCommited = fcOutput.position();
				if (syncOnFlush) {
					fcOutput.force(false);
					if (callback != null)
						callback.synched(offsetOutputCommited);
				}
			} else {
				bufOutput.put(buf); // Data Body
				bufOutput.put(MAGIC_FOOT); // Footer
				// Increment offset of buffered data (header + user-data)
				offsetOutputUncommited += packet_size;
				if (flushOnWrite) {
					flushBuffer();
				}
			}
			//
			return offset;
		} catch (Exception e) {
			log.error("Exception in write()", e);
		}
		return -1L;
	}
428979
public void deserialize(final ByteBuffer buf) {
		wordsInUse = buf.getInt();
		words = new long[wordsInUse];
		for (int i = 0; i < words.length; i++) {
			words[i] = buf.getLong();
		}

		// Assume maximum length then find real length
		// because recalculateWordsInUse assumes maintenance
		// or reduction in logical size
		wordsInUse = words.length;
		recalculateWordsInUse();
		sizeIsSticky = (words.length > 0 && words[words.length-1] == 0L); // heuristic
		checkInvariants();
	}
896405
private void writeByteBufferFull(ByteBuffer buf, boolean withHeader)
  {
    if (withHeader) {
      writeByteBufferSerdeHeader(buf, FULL_ENCODING_MODE);
    }

    writeByteBufferCommonFields(buf);

    buf.asLongBuffer().put(histogram);
    buf.position(buf.position() + Long.BYTES * histogram.length);
  }
1161788
public static ApproximateHistogram fromBytesDense(ByteBuffer buf)
  {
    int size = buf.getInt();
    int binCount = buf.getInt();

    float[] positions = new float[size];
    long[] bins = new long[size];

    buf.asFloatBuffer().get(positions);
    buf.position(buf.position() + Float.BYTES * positions.length);
    buf.asLongBuffer().get(bins);
    buf.position(buf.position() + Long.BYTES * bins.length);

    float min = buf.getFloat();
    float max = buf.getFloat();

    return new ApproximateHistogram(binCount, positions, bins, min, max);
  }
865353
public static int getSparseStorageSize(int nonEmptyBuckets)
  {
    return COMMON_FIELDS_SIZE +
           Integer.BYTES +
           (Integer.BYTES + Long.BYTES) * nonEmptyBuckets;
  }
890891
private void pp20DoUnpack(RandomAccessInputStream source, byte [] buffer) throws IOException
	{
		BitBuffer bitBuffer = new BitBuffer(source, (int)source.getLength()-4);
		source.seek(source.getLength()-1);
		int skip = source.read();
		bitBuffer.getBits(skip);
		int nBytesLeft = buffer.length;
		while (nBytesLeft > 0)
		{
			if (bitBuffer.getBits(1) == 0)
			{
				int n = 1;
				while (n < nBytesLeft)
				{
					int code = (int)bitBuffer.getBits(2);
					n += code;
					if (code != 3) break;
				}
				for (int i=0; i<n; i++)
				{
					buffer[--nBytesLeft] = (byte)bitBuffer.getBits(8);
				}
				if (nBytesLeft == 0) break;
			}
			
			int n = bitBuffer.getBits(2)+1;
			source.seek(n+3);
			int nbits = source.read();
			int nofs;
			if (n==4)
			{
				nofs = bitBuffer.getBits( (bitBuffer.getBits(1)!=0) ? nbits : 7 );
				while (n < nBytesLeft)
				{
					int code = bitBuffer.getBits(3);
					n += code;
					if (code != 7) break;
				}
			} 
			else
			{
				nofs = bitBuffer.getBits(nbits);
			}
			for (int i=0; i<=n; i++)
			{
				buffer[nBytesLeft-1] = (nBytesLeft+nofs < buffer.length) ? buffer[nBytesLeft+nofs] : 0;
				if ((--nBytesLeft)==0) break;
			}
		}
	}
955379
private static byte[] ensureCapacity(byte[] buf, int needed) {
        if (buf.length <= needed) {
            byte[] old = buf;
            buf = new byte[Integer.highestOneBit(needed) << 1];
            System.arraycopy(old, 0, buf, 0, old.length);
        }
        return buf;
    }
105070
private static void zipSize2KBuffer(
      final Object[] bufA, final int startA, // input
      final Object[] bufC, final int startC, // output
      final int k) {
    final int randomOffset = ItemsSketch.rand.nextBoolean() ? 1 : 0;
    final int limC = startC + k;
    for (int a = startA + randomOffset, c = startC; c < limC; a += 2, c++) {
      bufC[c] = bufA[a];
    }
  }
875805
public void setBuffer(WsByteBuffer buf) {
        if (buf != null) {
            this.buffers = this.defaultBuffers;
            this.buffers[0] = buf;
        } else {
            this.buffers = null;
        }
    }