package au.gov.dva.sopapi.sopref.data;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.*;
import java.nio.charset.Charset;

public class Conversions {



    public static String pdfToPlainText(byte[] pdf) throws IOException {

        RandomAccessBuffer randomAccessBuffer = new RandomAccessBuffer(pdf);
        PDFParser pdfParser = new PDFParser(randomAccessBuffer);
        pdfParser.parse();
        COSDocument cosDocument = pdfParser.getDocument();
        PDDocument pdDocument = new PDDocument(cosDocument);
        PDFTextStripper pdfTextStripper = new PDFTextStripper();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        Writer outWriter = new OutputStreamWriter(outStream, Charsets.UTF_8);
        pdfTextStripper.writeText(pdDocument,outWriter);

        String text = outStream.toString("UTF-8");

        pdDocument.close();
        randomAccessBuffer.close();
        cosDocument.close();
        outStream.close();
        return text;
    }

    public static String toString(JsonNode jsonNode) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
    }


}


@@ -196,48 +196,8 @@
 er;%0A
-import com.google.common.base.Charsets;%0A
 impo
@@ -226,32 +226,32 @@
 os.COSDocument;%0A
+
 import org.apach
@@ -440,42 +440,19 @@
 .io.
-*;%0Aimport java.nio.charset.Charset
+IOException
 ;%0A%0Ap
@@ -920,156 +920,21 @@
     
-ByteArrayOutputStream outStream = new ByteArrayOutputStream();%0A        Writer outWriter = new OutputStreamWriter(outStream, Charsets.UTF_8);%0A       
+String text =
  pdf
@@ -946,21 +946,19 @@
 tripper.
-write
+get
 Text(pdD
@@ -968,73 +968,10 @@
 ment
-,outWriter);%0A%0A        String text = outStream.toString(%22UTF-8%22);%0A
+);
 %0A   
@@ -1064,34 +1064,8 @@
 ();%0A
-        outStream.close();
 %0A   

Revert "text conversion changes attempt to fix encoding issue"