package com.mx.dla.global.bos;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

@Component
public class CustomResourceLoader extends BaseBO implements ResourceLoaderAware {
 
    private ResourceLoader resourceLoader;
 
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    
    public byte[] getFile2ByteArray(String urlFIle) throws IOException, InvalidFormatException{
    	Resource banner = resourceLoader.getResource(urlFIle);
    	   
        InputStream in = banner.getInputStream();    	
    	 
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[1024];
        while ((nRead = in.read(data, 0, data.length)) != -1) 
                buffer.write(data, 0, nRead);
             
        buffer.flush();
        byte[] byteArray = buffer.toByteArray();
        return byteArray;
    }
    
    public InputStream getFile2ItputStream(String urlFIle) throws IOException, InvalidFormatException{
    	Resource banner = resourceLoader.getResource(urlFIle);
        InputStream in = banner.getInputStream();    	
        return in;
    }
}
