package com.ssyf.common.web.helper;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.ssyf.common.web.filter.RequestContext;
import com.ssyf.common.web.filter.RequestContextHolder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 *
 * @author huwei
 * @date 2023/8/10 22:36
 */
public class ResponseContextHelper {

    public static RequestContext getContext(){
        return RequestContextHolder.getContext();
    }

    /**
     * 向HttpServletResponse写入数据并关闭流
     * @param bytes
     */
    public static void writeAndClose(byte[] bytes) throws IOException{
        writeAndClose(getContext().getResponse() ,bytes);
    }

    /**
     * 向HttpServletResponse写入数据并关闭流
     * @param response
     * @param bytes
     */
    public static void writeAndClose(HttpServletResponse response, byte[] bytes) throws IOException {
        OutputStream outputStream = response.getOutputStream();
        IoUtil.write(outputStream ,true ,bytes);
    }
}
