Search code examples
javaopencsv

StatefulBeanToCsv doesn't write complete fields to CSV,


I am using Opencsv v5.3, I am encoding InputStream to OutputStream in CSV format. In the scenario, InputStream contains 24051 records while it writes 24037 correct records with 24038th corrupted/incomplete record. Could you please help?

Below is my code:

private static void doCsvEncoding(Socket clientSocket, InputStream inputStream, OutputStream outputStream) {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            byte[] byteChunk = new byte[131072];
            try {
                int readBytes = inputStream.read(byteChunk);
                int total = 0;
                while (readBytes != -1) {
                    total += readBytes;
                    byteArrayOutputStream.write(byteChunk, 0, readBytes);
                    readBytes = inputStream.read(byteChunk);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                clientSocket.shutdownInput();
            }

            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            int cnt = 0;
            ByteArrayOutputStream mediatorOutputStream = new ByteArrayOutputStream();

            StatefulBeanToCsv<T> statefulBeanToCsv =
                    new StatefulBeanToCsvBuilder<T>(new OutputStreamWriter(mediatorOutputStream))
                            .withIgnoreField(T.class, T.class.getField("tag"))
                            .withIgnoreField(T.class, T.class.getDeclaredField("code")).build();

            try {
                while (true) {
                    T t = (T)objectInputStream.readObject();
                    statefulBeanToCsv.write(t);
                    cnt ++;
                }
            } catch (IOException | NullPointerException e) {
                e.printStackTrace();
            } catch (CsvRequiredFieldEmptyException e) {
                e.printStackTrace();
            } catch (CsvDataTypeMismatchException e) {
                e.printStackTrace();
            }
            outputStream.write(("" + cnt + " ").getBytes());
            System.out.println(cnt);
            mediatorOutputStream.close();
            ByteArrayInputStream mediatorInputStream = new ByteArrayInputStream(mediatorOutputStream.toByteArray());

            try {
                int readBytes = mediatorInputStream.read(byteChunk);
                while (readBytes != -1) {
                    outputStream.write(byteChunk, 0, readBytes);
                    readBytes = mediatorInputStream.read(byteChunk);
                }
                clientSocket.shutdownOutput();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch(IOException | ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

Solution

  • Instead of using StatefulBeanToCsvBuilder, using CSVWiter and adding header through Java Reflection worked.