2016-07-13 15 views
1

Ich habe folgenden Scala-Code zu konvertieren (short, int, long, float, double, bigint) in Byte-Array.Konvertierung von Zahlen (short, int, long, float, double, bigint) in Byte-Array: Scala/Java

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 
    } 
    } 
  1. Ist das alles benötigt? Muss ich am Ende alle Reinigungsmethoden nennen solche clear(), mark(), reset(), um sicherzustellen, gibt es keine ByteBuffer

  2. Lecks Wenn ich allocateDirect Methoden zu verwenden, unter Ausnahme auslöst. Also, bedeutet es allocateDirect Methode hat keine Backing-Array?

Exception in thread "main" java.lang.UnsupportedOperationException bei java.nio.ByteBuffer.array (ByteBuffer.java:994)

Antwort

1
  1. Ja, das ist alles, was erforderlich. Es gibt keine Aufräumarbeiten an Niobuffer.
  2. In der Tat haben direkte Puffer kein Backing-Array.
Verwandte Themen