Return hexdump of byte-array

@param buf is byte array to dump
@param limit size to dump from 0-offset to limit
@return String representation of hexdump
960248
public final static String byteArrayAsHex(final byte[] buf, final int limit) {
		final StringBuilder sb = new StringBuilder();
		for (int i = 0; i < limit; ++i) {
			if ((i % 16) == 0) { // print offset
				sb.append(nativeAsHex(i, 32)).append("  ");
			} else if (((i) % 8) == 0) { // split on qword
				sb.append(" ");
			}
			sb.append(nativeAsHex((buf[i] & 0xFF), 8)).append(" "); // hex byte
			if (((i % 16) == 15) || (i == (buf.length - 1))) {
				for (int j = (16 - (i % 16)); j > 1; j--) { // padding non exist bytes
					sb.append("   ");
				}
				sb.append(" |"); // byte columns
				final int start = ((i / 16) * 16);
				final int end = ((buf.length < i + 1) ? buf.length : (i + 1));
				for (int j = start; j < end; ++j) {
					if ((buf[j] >= 32) && (buf[j] <= 126)) {
						sb.append((char) buf[j]);
					} else {
						sb.append("."); // non-printable character
					}
				}
				sb.append("|\n"); // end column
			}
		}
		return sb.toString();
	}
1251572
static String byteToBinaryString(byte b, int limit) {
    final StringBuilder sb = new StringBuilder();
    if (limit > 8) {
      limit = 8;
    }
    for (int i = 0; i < limit; i++) {
      sb.append((char)('0' + (b&1)));
      b>>=1;
    }
    return sb.toString();
  }
44659
public static void append(StringBuffer buf,byte b,int base)
    {
        int bi=0xff&b;
        int c='0'+(bi/base)%base;
        if (c>'9')
            c= 'a'+(c-'0'-10);
        buf.append((char)c);
        c='0'+bi%base;
        if (c>'9')
            c= 'a'+(c-'0'-10);
        buf.append((char)c);
    }
650356
private ByteBuf tryCompress(ByteBuf buf) {
        final int uncompressedLength = buf.readableBytes();
        final byte[] compressed = new byte[Snappy.maxCompressedLength(uncompressedLength)];
        final int compressedLength;

        if (buf.hasArray()) {
            // Save a memory copy by reading directly from the backing array.
            compressedLength = Snappy.compress(
                    buf.array(), buf.arrayOffset() + buf.readerIndex(), uncompressedLength, compressed, 0);
        } else {
            final byte[] data = new byte[buf.readableBytes()];
            buf.getBytes(buf.readerIndex(), data);
            compressedLength = Snappy.compress(data, 0, data.length, compressed, 0);
        }

        final double ratio = (double) compressedLength / uncompressedLength;
        if (ratio > minCompressionRatio) {
            // Compression doesn't make enough of a difference.
            return null;
        }

        // Save a memory copy by deferring trimming until we know the result will be used.
        final byte[] trimmedCompressed = Arrays.copyOf(compressed, compressedLength);
        return Unpooled.wrappedBuffer(trimmedCompressed);
    }
249604
function zeroBuffer(buf) {
  for (var i = 0; i < buf.length; i++) {
    buf[i] = 0;
  }
  return buf;
}
1337441
public static ByteBuffer slice(ByteBuffer buf, int off, int len)
	{
		ByteBuffer localBuf=buf.duplicate();	// so we don't mess up the position,etc
		logger.debug("off={} len={}", off, len);
		localBuf.position(off);
		localBuf.limit(off+len);
		logger.debug("pre-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(), localBuf.limit());
		localBuf = localBuf.slice();
		logger.debug("post-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(), localBuf.limit());
		return localBuf;
	}
845652
function byteswap64(buf) {
  var swap = new Buffer(buf.length);
  for (var i = 0; i < buf.length; i++) {
    swap[i] = buf[buf.length -1 - i];
  }
  return swap;
}
651593
public int read(byte []buf, int offset, int length) throws IOException
  {
    int strlen = _length;

    int start = offset;
    int end = offset + length;
    
    int index = _index;
    for (; index < strlen && offset < end; index++) {
      int ch = _string.charAt(index);

      if (ch < 0x80)
        buf[offset++] = (byte) ch;
      else if (ch < 0x800 && offset + 1 < end) {
        buf[offset++] = (byte) (0xc0 | (ch >> 6));
        buf[offset++] = (byte) (0x80 | (ch & 0x3f));
      }
      else if (ch < 0x8000 && offset + 2 < end) {
        buf[offset++] = (byte) (0xe0 | (ch >> 12));
        buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
        buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
      }
      else if (offset == start) {
        throw new IllegalStateException("buffer length is not large enough to decode UTF-8 data");
      }
      else {
        break;
      }
    }

    _index = index;

    return start < offset ? offset - start : -1;
  }
895203
public static String prettyHexDump(ByteBuf buffer) {
        return prettyHexDump(buffer, buffer.readerIndex(), buffer.readableBytes());
    }
676516
@Nonnull
    public static String toHex(@Nonnull final byte[] bytes) {
        StringBuilder buf = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            String hexDigits = Integer.toHexString((int) b & 0x00ff);
            if (hexDigits.length() == 1) {
                buf.append('0');
            }
            buf.append(hexDigits);
        }
        return buf.toString();
    }
998325
@Override
  public void encode(ByteBuf buf) {
    Encoders.Strings.encode(buf, streamId);
    buf.writeLong(byteCount);
  }
909157
function encode(buf) {
	if (buf.length != 8) {
		throw new TypeError("Buffer must be length 8");
	}

	var num = new UInt64(buf.readUInt32BE(4), buf.readUInt32BE(0));
	var str = "";
	do {
		num.div(SIXTY_TWO);
		str = CHARS[num.remainder] + str;
	} while (num.greaterThan(ZERO));

	return str;
}
1035783
public static double[] getDoubleData(DataBuffer buf) {
        if (buf.allocationMode() == DataBuffer.AllocationMode.HEAP)
            return buf.asDouble();
        else {
            double[] ret = new double[(int) buf.length()];
            for (int i = 0; i < buf.length(); i++)
                ret[i] = buf.getDouble(i);
            return ret;

        }
    }
1091280
public static String prettyHexDump(ByteBuf buffer, int offset, int length) {
        return HexUtil.prettyHexDump(buffer, offset, length);
    }
240782
public static ApproximateHistogram fromBytesCompact(ByteBuffer buf)
  {
    short size = (short) (-1 * buf.getShort());
    byte count = buf.get();

    if (count >= 0) {
      // only exact bins
      ApproximateHistogram histogram = new ApproximateHistogram(size);
      for (int i = 0; i < count; ++i) {
        histogram.offer(buf.getFloat());
      }
      return histogram;
    } else {
      byte approxCount = (byte) (-1 * count);

      Map<Float, Long> approx = new HashMap<>();

      for (int i = 0; i < approxCount; ++i) {
        final float value = buf.getFloat();
        if (approx.containsKey(value)) {
          approx.put(value, approx.get(value) + 1);
        } else {
          approx.put(value, 1L);
        }
      }

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

      byte exactCount = buf.get();

      Map<Float, Long> exact = new HashMap<>();

      for (int i = 0; i < exactCount; ++i) {
        final float value = buf.getFloat();
        if (exact.containsKey(value)) {
          exact.put(value, exact.get(value) + 1);
        } else {
          exact.put(value, 1L);
        }
      }

      int binCount = exact.size() + approx.size();

      List<Float> pos = new ArrayList<>();
      pos.addAll(exact.keySet());
      pos.addAll(approx.keySet());
      Collections.sort(pos);

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

      for (int i = 0; i < pos.size(); ++i) {
        positions[i] = pos.get(i);
      }

      for (int i = 0; i < pos.size(); ++i) {
        final float value = pos.get(i);
        if (exact.containsKey(value)) {
          bins[i] = exact.get(value);
        } else {
          bins[i] = approx.get(value) | APPROX_FLAG_BIT;
        }
      }

      return new ApproximateHistogram(binCount, positions, bins, min, max);
    }
  }
139795
public static byte[] fromHexByteArray(final byte[] buffer)
    {
        final byte[] outputBuffer = new byte[buffer.length >> 1];

        for (int i = 0; i < buffer.length; i += 2)
        {
            final int hi = FROM_HEX_DIGIT_TABLE[buffer[i]] << 4;
            final int lo = FROM_HEX_DIGIT_TABLE[buffer[i + 1]]; // lgtm [java/index-out-of-bounds]
            outputBuffer[i >> 1] = (byte)(hi | lo);
        }

        return outputBuffer;
    }
85799
public static String hexDump(byte[] array, int offset, int length) {
            if (length < 0) {
              throw new IllegalArgumentException("length: " + length);
            }
            if (length == 0) {
                return EMPTY_STRING;
            }
            int endIndex = offset + length;
            char[] buf = new char[length << 1];

            int srcIdx = offset;
            int dstIdx = 0;
            for (; srcIdx < endIndex; srcIdx ++, dstIdx += 2) {
                System.arraycopy(
                    HEXDUMP_TABLE, (array[srcIdx] & 0xFF) << 1,
                    buf, dstIdx, 2);
            }
            return new String(buf);
        }