convert Scala Option () match in java

Viewed 34

I need to convert this code into java, how can I do this?

Option(cipher.getIV) match{
      {
        case Some(iv) =>
          s"$version-${Base64.getEncoder.encodeToString(iv ++ encryptedValue)}"
        case None     =>
          throw new CryptoException(UnderlyingIVBug)
      }
}
1 Answers

It would be something like this. ++ is the list concat operator. And Option basically is like Optional in java

var iv = cipher.getIV;
if (iv == null) {
    throw new CryptoException(UnderlyingIVBug);
}
byte[] encode = Arrays.copyOf(iv , iv.length + encryptedValue.length);
System.arraycopy(encryptedValue, 0, encode, iv.length, encryptedValue.length);
return version + "-" + Base64.getEncoder.encodeToString(encode);
Related