nit: can you keep the finally outside of the if statement? just makes the code a little more concise and I think easier to read.  i.e.,

val buf = try {
  if (blah)
  else (blah)
} finally {
  channel.close()
}