Java Code Examples for java.nio.ByteBuffer.hashCode()

The following are Jave code examples for showing how to use hashCode() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: memory   File: Memory.java   View Source Code Vote up 4 votes
/**
 * Returns a formatted hex string of an area of this Memory.
 * Used primarily for testing.
 * @param preamble a descriptive header
 * @param offsetBytes offset bytes relative to the Memory start
 * @param lengthBytes number of bytes to convert to a hex string
 * @param state the ResourceState
 * @return a formatted hex string in a human readable array
 */
static String toHex(final String preamble, final long offsetBytes, final int lengthBytes,
    final ResourceState state) {
  checkBounds(offsetBytes, lengthBytes, state.getCapacity());
  final StringBuilder sb = new StringBuilder();
  final Object uObj = state.getUnsafeObject();
  final String uObjStr = (uObj == null) ? "null"
          : uObj.getClass().getSimpleName() + ", " + (uObj.hashCode() & 0XFFFFFFFFL);
  final ByteBuffer bb = state.getByteBuffer();
  final String bbStr = (bb == null) ? "null"
          : bb.getClass().getSimpleName() + ", " + (bb.hashCode() & 0XFFFFFFFFL);
  final MemoryRequestServer memReqSvr = state.getMemoryRequestServer();
  final String memReqStr = (memReqSvr == null) ? "null"
          : memReqSvr.getClass().getSimpleName() + ", " + (memReqSvr.hashCode() & 0XFFFFFFFFL);
  final long cumBaseOffset = state.getCumBaseOffset();
  sb.append(preamble).append(LS);
  sb.append("NativeBaseOffset    : ").append(state.getNativeBaseOffset()).append(LS);
  sb.append("UnsafeObj, hashCode : ").append(uObjStr).append(LS);
  sb.append("UnsafeObjHeader     : ").append(state.getUnsafeObjectHeader()).append(LS);
  sb.append("ByteBuf, hashCode   : ").append(bbStr).append(LS);
  sb.append("RegionOffset        : ").append(state.getRegionOffset()).append(LS);
  sb.append("Capacity            : ").append(state.getCapacity()).append(LS);
  sb.append("CumBaseOffset       : ").append(cumBaseOffset).append(LS);
  sb.append("MemReq, hashCode    : ").append(memReqStr).append(LS);
  sb.append("Valid               : ").append(state.isValid()).append(LS);
  sb.append("Resource Read Only  : ").append(state.isResourceReadOnly()).append(LS);
  sb.append("Resource Endianness : ").append(state.order().toString()).append(LS);
  sb.append("JDK Version         : ").append(UnsafeUtil.JDK).append(LS);
  //Data detail
  sb.append("Data, littleEndian  :  0  1  2  3  4  5  6  7");

  for (long i = 0; i < lengthBytes; i++) {
    final int b = unsafe.getByte(uObj, cumBaseOffset + offsetBytes + i) & 0XFF;
    if ((i % 8) == 0) { //row header
      sb.append(String.format("%n%20s: ", offsetBytes + i));
    }
    sb.append(String.format("%02x ", b));
  }
  sb.append(LS);

  return sb.toString();
}