/**
 * Jooby https://jooby.io
 * Apache License Version 2.0 https://jooby.io/LICENSE.txt
 * Copyright 2014 Edgar Espina
 */
package io.jooby.rocker;

import com.fizzed.rocker.RockerModel;
import com.fizzed.rocker.runtime.ArrayOfByteArraysOutput;
import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.Route;

import javax.annotation.Nonnull;

class RockerHandler implements Route.Handler {
  private final Route.Handler next;

  RockerHandler(Route.Handler next) {
    this.next = next;
  }

  @Nonnull @Override public Object apply(@Nonnull Context ctx) throws Exception {
    try {
      RockerModel template = (RockerModel) next.apply(ctx);
      ArrayOfByteArraysOutput buff = template.render(ArrayOfByteArraysOutput.FACTORY);
      ctx.setResponseType(MediaType.html);
      ctx.setResponseLength(buff.getByteLength());
      ctx.send(buff.asReadableByteChannel());
      return ctx;
    } catch (Throwable x) {
      ctx.sendError(x);
      return x;
    }
  }
}

@@ -347,16 +347,39 @@
 Nonnull;
+%0Aimport java.util.List;
 %0A%0Aclass 
@@ -760,16 +760,62 @@
 CTORY);%0A
+      List%3Cbyte%5B%5D%3E arrays = buff.getArrays();%0A
       ct
@@ -919,35 +919,48 @@
 end(
-buff.asReadableByteChannel(
+arrays.toArray(new byte%5Barrays.size()%5D%5B%5D
 ));%0A

update rocker renderer to use ctx.send(byte[]...)