Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - Pad an array to a certain size

Tags:

scala

I have a method:

def loadRom(filename: String) : Array[Int] = {
  val is = new FileInputStream(filename)
  Stream.continually(is.read).takeWhile(-1 !=).toArray
}

It returns an array of 0x8000 (I can always guarantee that). I want to load it into an array of 0x200000 size. Is there an elegant way to do that? What about padding it out to 0x200000?

Edit: Whoops. I JUST noticed the padTo method does what I want. Close?

like image 668
Dominic Bou-Samra Avatar asked Nov 01 '12 04:11

Dominic Bou-Samra


1 Answers

For completeness's sake, there's SeqLike.padTo (arrays implicitly extend SeqLike). Assuming you want to pad with zero bytes, just append .padTo(0x200000, 0) to the result expression and you're good to go.

like image 98
Tomer Gabel Avatar answered Oct 29 '22 11:10

Tomer Gabel