/*
 * Copyright 2016 Original authors.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */
package io.vertx.ext.web.templ.impl;

import com.mitchellbosecke.pebble.error.LoaderException;
import com.mitchellbosecke.pebble.loader.Loader;
import io.vertx.core.Vertx;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;

/**
 * A PebbleLoader based on Vertx
 *
 * @author Paulo Lopes
 */
class PebbleVertxLoader implements Loader<String> {

  private final Vertx vertx;

  private String charset = Charset.defaultCharset().toString();

  public PebbleVertxLoader(Vertx vertx) {
    this.vertx = vertx;
  }

  @Override
  public Reader getReader(String s) throws LoaderException {
    try {
      final char[] buffer = vertx.fileSystem().readFileBlocking(s).toString(charset).toCharArray();
      final int[] pos = {0};

      return new Reader() {
        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
          if (pos[0] == buffer.length) {
            return -1;
          }
          final int end = Math.min(buffer.length, pos[0] + len);
          System.arraycopy(buffer, pos[0], cbuf, off, end);
          final int read = end - pos[0];
          pos[0] = end;
          return read;
        }

        @Override
        public void close() throws IOException {

        }
      };
    } catch (RuntimeException e) {
      throw new LoaderException(e, e.getMessage());
    }
  }

  @Override
  public void setCharset(String s) {
    this.charset = s;
  }

  @Override
  public void setPrefix(String s) {
  }

  @Override
  public void setSuffix(String s) {
  }

  @Override
  public String resolveRelativePath(String s, String anchorPath) {
    File resolved = new File(new File(anchorPath).getParentFile(), s);

    return resolved.getPath();
  }

  @Override
  public String createCacheKey(String s) {
    return s;
  }
}

@@ -731,27 +731,22 @@
 java.io.
-IOException
+Reader
 ;%0Aimport
@@ -746,32 +746,38 @@
 %0Aimport java.io.
+String
 Reader;%0Aimport j
@@ -1183,22 +1183,22 @@
   final 
-char%5B%5D
+String
  buffer 
@@ -1259,561 +1259,47 @@
 set)
-.toCharArray();%0A      final int%5B%5D pos = %7B0%7D;%0A%0A      return new Reader() %7B%0A        @Override%0A        public int read(char%5B%5D cbuf, int off, int len) throws IOException %7B%0A          if (pos%5B0%5D == buffer.length) %7B%0A            return -1;%0A          %7D%0A          final int end = Math.min(buffer.length, pos%5B0%5D + len);%0A          System.arraycopy(buffer, pos%5B0%5D, cbuf, off, end);%0A          final int read = end - pos%5B0%5D;%0A          pos%5B0%5D = end;%0A          return read;%0A        %7D%0A%0A        @Override%0A        public void close() throws IOException %7B%0A%0A        %7D%0A      %7D
+;%0A      return new StringReader(buffer)
 ;%0A  

Fix Bug on Vertx Loader when handles template with more than 4096 character - Simply Use StringReader