Search code examples
javalistinputstream

Converting List into InputStream without creating an intermediate file


I'm using a function which will get the input as List and className in which I convert the List as a delimiter file(Intermediate) then convert the file as InputStream as a return value.

Here creating an intermediate file and deleting is a tedious process as this will increase the time and performance.

So is there is another way in which we can directly convert the data from List<Class> into a delimiter with the headers (maybe we can Store it in List<Object>) then without creating an intermediate file we can directly convert it into InputStream.

<T> InputStream createInputStream(String csvFileName, List<T> listObject, Class className, char csvPreference) {

        ICsvBeanWriter beanWriter = null;
        File file=new File(csvFileName);
        InputStream inputStream=null;
        try {
            CsvPreference DELIMITER = new CsvPreference.Builder('"', csvPreference, "\n").build();

            beanWriter = new CsvBeanWriter(new FileWriter(file), DELIMITER);
            List<String> fieldList = Arrays.stream(className.getDeclaredFields()).map(Field::getName).collect(Collectors.toList());
            CellProcessor[] processors = new CellProcessor[fieldList.size()];
            for (int i = 0; i < fieldList.size(); i++) {
                processors[i] = new Optional();
            }
            String[] header = new String[fieldList.size()];
            fieldList.toArray(header);
            beanWriter.writeHeader(header);

            for (T obj : listObject) {
                beanWriter.write(obj, header, processors);
            }

        } catch (IOException ex) {
            System.err.println("Error writing the CSV file: " + ex);
        } finally {
            if (beanWriter != null) {
                try {
                    beanWriter.close();
                    inputStream=new FileInputStream(file);
                    file.delete();
                } catch (IOException ex) {
                    System.err.println("Error closing the writer: " + ex);
                }
            }
        }
        return inputStream;
    }

Update 1:

Users Class

public class Users {

    String id;

    String name;

    String gender;

}

Adding elements in List

List<Users> userList=new ArrayList<Users>();
userList.add(new Users("1","AA","Male"));
userList.add(new Users("2","BB","Male"));
userList.add(new Users("3","CC","Female"));

Here how I'm currently calling

InputStream inputStream=createInputStream("demo.csv",userList,Users.Class,'|');

Here the Intermediate file(demo.csv) will be like,

id|name|gender
1|AA|Male
2|BB|Male
3|CC|Female

From this file, I'll convert into inputstream

Thanks in Advance,
Jay


Solution

  • Have you considered something like

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    beanWriter = new CsvBeanWriter(new PrintWriter(baos), DELIMITER);
    
    //write some stuff
    
    byte[] byteArray = baos.toByteArray();
    
    return new ByteArrayInputStream(byteArray);