I've following scala code to convert (short, int, long, float, double,bigint) to byte array.
def getByteArray(value: Any, of_type: String) = {
of_type match {
case "short" => ByteBuffer.allocate(2).putShort(value.asInstanceOf[Short]).array()
case "int" => ByteBuffer.allocate(4).putInt(value.asInstanceOf[Int]).array()
case "long" => ByteBuffer.allocate(8).putLong(value.asInstanceOf[Long]).array()
case "float" => ByteBuffer.allocate(4).putFloat(value.asInstanceOf[Float]).array()
case "double" => ByteBuffer.allocate(8).putDouble(value.asInstanceOf[Double]).array()
case "bigint" => BigInt(value.toString).toByteArray
}
}
Is this all is needed ? Do I need to call any cleanup methods at the end such clear(), mark(), reset() to ensure there are no ByteBuffer leaks
When I use allocateDirect methods, it throws below exception. So, does it mean allocateDirect method has no backing array ?
Exception in thread "main" java.lang.UnsupportedOperationException at java.nio.ByteBuffer.array(ByteBuffer.java:994)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With