package com.rockson.servletplus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.alibaba.fastjson.JSON;

public class Response extends HttpServletResponseWrapper {
	protected HttpServlet servlet;
	public Response(HttpServletResponse response , HttpServlet servlet) {
		super(response);
		this.servlet = servlet;
	}

	public void json(Object object) throws IOException {
		getWriter().write(JSON.toJSONString(object));
	}

	protected void copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
		byte[] buff = new byte[bufferSize <= 0 ? 4 * 1024 : bufferSize];
		int len;
		while (-1 != (len = in.read(buff))) {
			out.write(buff, 0, len);
		}
	}

	public void send(String str) throws IOException {
		if (null == str) {
			return;
		}
		getWriter().write(str);
	}

	public void sendFile(String str) throws IOException {
		String path = this.servlet.getServletContext().getRealPath(str);
		System.out.println(path);
		File file = new File(path);
		addHeader("Content-Length", "" + file.length());
		addHeader("Content-Type", Files.probeContentType(file.toPath()));
		InputStream inputStream= new FileInputStream(file);
		sendFile(inputStream);
		inputStream.close();
	}

	public void sendFile(InputStream in) throws IOException {
		copy(in, getOutputStream(), 0);
	}

	public void sendFile(Path path) throws IOException {
		reset();
		File file = path.toFile();
		addHeader("Content-Length", "" + file.length());
		addHeader("Content-Type", Files.probeContentType(path));
		Files.copy(path, getOutputStream());
	}

	public void download(Path path) throws IOException {
		download(path, null);
	}

	public void download(Path path, String name) throws IOException {
		reset();
		File file = path.toFile();
		addHeader("Content-Disposition", "attachment; filename=" + (null == name ? file.getName() : name));
		addHeader("Content-Length", "" + file.length());
		addHeader("Content-Type", Files.probeContentType(path));
		
		Files.copy(path, getOutputStream());
	}
	
}

@@ -1316,37 +1316,8 @@
 );%0D%0A
-%09%09System.out.println(path);%0D%0A
 %09%09Fi

fix sendfile bug