// Path: jflask/src/main/java/net/jflask/util/IO.java
// public class IO {
// 
//   /**
//    * Reads specified input stream until it reaches EOF.
//    */
//   public static byte[] readFully(InputStream in) throws IOException {
//     ByteArrayOutputStream bout = new ByteArrayOutputStream();
//     pipe(in, bout, false);
//     return bout.toByteArray();
//   }
// 
//   /**
//    * Reads input stream until EOF and writes all read data into output stream,
//    * then closes it.
//    */
//   public static void pipe(InputStream in, OutputStream out, boolean closeOutput)
//       throws IOException {
//     byte[] buf = new byte[1024];
//     while (true) {
//       int len = in.read(buf);
//       if (len >= 0) {
//         out.write(buf, 0, len);
//         out.flush();
//       }
//       else {
//         if (closeOutput)
//           out.close();
//         break;
//       }
//     }
//   }
// 
// }
// 
// Path: jflask/src/main/java/net/jflask/util/Log.java
// public class Log {
// 
//   public static final boolean DEBUG = Boolean.getBoolean("debug");
// 
//   public static void warn(Object msg) {
//     System.err.println("WARN: " + msg);
//   }
// 
//   public static void error(Object msg) {
//     System.err.println("ERROR: " + msg);
//   }
// 
//   public static void error(Object msg, Throwable t) {
//     System.err.println("ERROR: " + msg);
//     t.printStackTrace();
//   }
// 
//   public static void info(Object msg) {
//     System.err.println("INFO: " + msg);
//   }
// 
//   public static void debug(Object msg) {
//     if (DEBUG)
//       System.err.println("DEBUG: " + msg);
//   }
// 
// }


import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Arrays;
import com.sun.net.httpserver.HttpExchange;
import net.jflask.util.IO;
import net.jflask.util.Log;
    Object res = method.invoke(target, args);

    ctx.app.fireSuccess(method, args, res);

    return processResponse(r, resp, res);
  }

  private boolean processResponse(HttpExchange r,
                                  Response resp,
                                  Object res) throws Exception {
    if (converter != null) {
      converter.convert(res, resp);
    }
    else if (converterName != null) {
      throw new IllegalStateException("Converter '" + converterName +
                                      "' not registered in App.");
    }
    else if (res instanceof CustomResponse) {
      // do nothing: status and headers should already be set
    }
    else if (res instanceof String) {
      r.sendResponseHeaders(200, 0);
      r.getResponseBody().write(((String) res).getBytes("UTF-8"));
    }
    else if (res instanceof byte[]) {
      r.sendResponseHeaders(200, 0);
      r.getResponseBody().write((byte[]) res);
    }
    else if (res instanceof InputStream) {
      r.sendResponseHeaders(200, 0);
// Path: jflask/src/main/java/net/jflask/util/IO.java
// public class IO {
// 
//   /**
//    * Reads specified input stream until it reaches EOF.
//    */
//   public static byte[] readFully(InputStream in) throws IOException {
//     ByteArrayOutputStream bout = new ByteArrayOutputStream();
//     pipe(in, bout, false);
//     return bout.toByteArray();
//   }
// 
//   /**
//    * Reads input stream until EOF and writes all read data into output stream,
//    * then closes it.
//    */
//   public static void pipe(InputStream in, OutputStream out, boolean closeOutput)
//       throws IOException {
//     byte[] buf = new byte[1024];
//     while (true) {
//       int len = in.read(buf);
//       if (len >= 0) {
//         out.write(buf, 0, len);
//         out.flush();
//       }
//       else {
//         if (closeOutput)
//           out.close();
//         break;
//       }
//     }
//   }
// 
// }
// 
// Path: jflask/src/main/java/net/jflask/util/Log.java
// public class Log {
// 
//   public static final boolean DEBUG = Boolean.getBoolean("debug");
// 
//   public static void warn(Object msg) {
//     System.err.println("WARN: " + msg);
//   }
// 
//   public static void error(Object msg) {
//     System.err.println("ERROR: " + msg);
//   }
// 
//   public static void error(Object msg, Throwable t) {
//     System.err.println("ERROR: " + msg);
//     t.printStackTrace();
//   }
// 
//   public static void info(Object msg) {
//     System.err.println("INFO: " + msg);
//   }
// 
//   public static void debug(Object msg) {
//     if (DEBUG)
//       System.err.println("DEBUG: " + msg);
//   }
// 
// }


// Path: jflask/src/main/java/net/jflask/MethodHandler.java
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Arrays;
import com.sun.net.httpserver.HttpExchange;
import net.jflask.util.IO;
import net.jflask.util.Log;

    Object res = method.invoke(target, args);

    ctx.app.fireSuccess(method, args, res);

    return processResponse(r, resp, res);
  }

  private boolean processResponse(HttpExchange r,
                                  Response resp,
                                  Object res) throws Exception {
    if (converter != null) {
      converter.convert(res, resp);
    }
    else if (converterName != null) {
      throw new IllegalStateException("Converter '" + converterName +
                                      "' not registered in App.");
    }
    else if (res instanceof CustomResponse) {
      // do nothing: status and headers should already be set
    }
    else if (res instanceof String) {
      r.sendResponseHeaders(200, 0);
      r.getResponseBody().write(((String) res).getBytes("UTF-8"));
    }
    else if (res instanceof byte[]) {
      r.sendResponseHeaders(200, 0);
      r.getResponseBody().write((byte[]) res);
    }
    else if (res instanceof InputStream) {
      r.sendResponseHeaders(200, 0);
      IO.pipe((InputStream) res, r.getResponseBody(), false);