Covert short code scala to java

please help me , convert short code scala to java , i’m newbie in scala

protected def readBytes: Array[Byte] = {
val n = read
(0 until n).foldLeft(new Array[Byte](n)) {
    case (a, index) => 
      a(index) = read
      a
  }
}
1 Like

I’m sorry, but I don’t understand what you are asking help doing. Can you clarify?

You didn’t provide a definition of read, I’m assuming it is a method that returns a Byte.

The code looks like an overly convoluted way to create an Array and fill it with values. Unless I’m missing something, it does the same as:

protected def readBytes: Array[Byte] = {

val n = read

val bytes = new Array(n)

var index = 0

while(index < n) {

bytes(index) = read

index += 1

}

bytes

}

My Java is a bit rusty, but I think you want to do something like:

protected byte[] readBytes {

int n = read()

int index = 0

byte[] bytes = new byte[n]

while(index < n) {

bytes[index] = read()

index++

}

return bytes

**}

One way to convert Scala code to Java is to compile it and then use a Java decompiler

Interesting. Have you done that before on real code? I’m curious about the quality of the resulting Java code. Is it quality code, or is it a mess? Thanks.

[…] Is it quality code, or is it a mess? […]

I’d say that mostly depends on the decompiler used. But this method is as well demonstrated in “Progamming in Scala”.

Not sure if literal conversion is needed but here are simple implementations:

Java:

        Scanner source = null; // replace this with proper source
        byte[] array = new byte[source.nextInt()];
        for (int i = 0; i < array.length; i++) {
            array[i] = source.nextByte();
        }

Scala:

    val source: Scanner = null // replace this with proper source
    val array = Array.fill(source.nextInt)(source.nextByte)

Well here’s what I got from IntelliJ…

Original code:

class TestRead {
  def read: Byte = ???
  protected def readBytes: Array[Byte] = {
    val n = read
    (0 until n).foldLeft(new Array[Byte](n)) {
      case (a, index) =>
        a(index) = read
        a
    }
  }
}

For some reason I couldn’t see the class files in the project explorer so I had to open it in the OS file browser and drag it onto the IntelliJ window. Then it showed the usual decompiled scala outline:

package chesednow.people.views
class TestRead() extends scala.AnyRef {
  def read : scala.Byte = { /* compiled code */ }
  protected def readBytes : scala.Array[scala.Byte] = { /* compiled code */ }
}

From there I was able to right click and select Decompile Scala to Java. The final result is this:

//decompiled from TestRead.class
package chesednow.people.views;

import java.lang.invoke.SerializedLambda;
import scala.MatchError;
import scala.Tuple2;
import scala.Predef.;
import scala.reflect.ScalaSignature;
import scala.runtime.BoxesRunTime;

@ScalaSignature(
   bytes = "\u0006\u0001\u00052A\u0001B\u0003\u0001\u0019!)1\u0003\u0001C\u0001)!)q\u0003\u0001C\u00011!)A\u0004\u0001C\t;\tAA+Z:u%\u0016\fGM\u0003\u0002\u0007\u000f\u0005)a/[3xg*\u0011\u0001\"C\u0001\u0007a\u0016|\u0007\u000f\\3\u000b\u0003)\t\u0011b\u00195fg\u0016$gn\\<\u0004\u0001M\u0011\u0001!\u0004\t\u0003\u001dEi\u0011a\u0004\u0006\u0002!\u0005)1oY1mC&\u0011!c\u0004\u0002\u0007\u0003:L(+\u001a4\u0002\rqJg.\u001b;?)\u0005)\u0002C\u0001\f\u0001\u001b\u0005)\u0011\u0001\u0002:fC\u0012,\u0012!\u0007\t\u0003\u001diI!aG\b\u0003\t\tKH/Z\u0001\ne\u0016\fGMQ=uKN,\u0012A\b\t\u0004\u001d}I\u0012B\u0001\u0011\u0010\u0005\u0015\t%O]1z\u0001"
)
public class TestRead {
   public byte read() {
      throw .MODULE$.$qmark$qmark$qmark();
   }

   public byte[] readBytes() {
      byte n = this.read();
      return (byte[])scala.runtime.RichInt..MODULE$.until$extension0(.MODULE$.intWrapper(0), n).foldLeft(new byte[n], (x0$1, x1$1) -> {
         return $anonfun$readBytes$1(this, x0$1, BoxesRunTime.unboxToInt(x1$1));
      });
   }

   // $FF: synthetic method
   public static final byte[] $anonfun$readBytes$1(final TestRead $this, final byte[] x0$1, final int x1$1) {
      Tuple2 var4 = new Tuple2(x0$1, BoxesRunTime.boxToInteger(x1$1));
      if (var4 != null) {
         byte[] a = (byte[])var4._1();
         int index = var4._2$mcI$sp();
         a[index] = $this.read();
         return a;
      } else {
         throw new MatchError(var4);
      }
   }

   // $FF: synthetic method
   private static Object $deserializeLambda$(SerializedLambda var0) {
      return var0.lambdaDeserialize<invokedynamic>(var0);
   }
}

thanks it work for me :smiley: