scala-hashing icon indicating copy to clipboard operation
scala-hashing copied to clipboard

Support for java 9+

Open saksmt opened this issue 2 years ago • 0 comments

Currently due to usage of sun.nio.ch.DirectBuffer it is impossible to use this library with jdk9+ since it leads to lass com.desmondyeung.hashing.StreamingHash64 cannot access class sun.nio.ch.DirectBuffer (in module java.base) because module java.base does not export sun.nio.ch

How about checking for jdk version and based on that either using DirectBuffer directly (pun not intended :) ) or through var handles?

I can't actually into unsafe, so just high-level example to clarify what I mean:

trait UnsafeBytes {
  def addressOf(buffer: ByteBuffer): Int
}
object UnsafeBytes {
  private val vmSpec: Int = sys.props("java.vm.specification.version").split('.').last.toInt

  def apply: UnsafeBytes = if (vmSpec < 9) { // or we could check if there is Var/MethodHandle class present in classpath
    directDirectBuffer
  } else {
    varHandleDirectBuffer
  }

  private object directDirectBuffer extends UnsafeBytes {
    //...
  }

  private object varHandleDirectBuffer extends UnsafeBytes {
    //...
  }
}

saksmt avatar Dec 15 '21 11:12 saksmt