Search code examples
javaarrayskofax

Convert C# Byte array to Java byte array and max array size in Java


I have a double issue concerning my code. I have to make a call to a REST API from KTA (Kofax Total Agility). I send a call to the entry point :

{
    "sessionId" : "EG346A9A4BD94E45C09106DA457596293",
    "reportingData": null,
    "documentId": "c12ebf4e-b756-4c55-a0e6-ab2200f16990"
}

And receive a response :

{
    "d": {
        "__type": "DocumentSourceFile:http://www.kofax.com/agility/services/sdk",
        "MimeType": "application/pdf",
        "SourceFile": [
            37, 80, 68, 70, 226,227,207,211...
        ]
    }
}

According to the documentation it returns a byte array and this byte array should be converted onto a pdf file. So far, so good ! Now i meet some issues :

  • the documentation specifies I should receive a byte containing the file data but it looks like an int array or unsigned bytes (values greater than 127)
  • I tried to retrieve the document and for this I hard coded the byte array in a basic class. Because of length I receive an error message : java: code too large

Here is the code I use in order to try if i can convert it onto a file :

public static int[] sourceFile = new int[]{37,80,68,70,45,49,46,52,10,37,226,227,207,211,

// test variables
private static String FILEPATH = "";
private static File file = new File(FILEPATH);

// convert to byte array
private static void getSourceFile(int[] sourceFile) {
    byte[] returnValue = new byte[sourceFile.length];
    for(int i = 0; i < sourceFile.length; i++) {
        byte b = (byte) Integer.parseInt(String.valueOf(sourceFile[i]));
        returnValue[i] = b;
    }
    // byte to file
    writeByte(returnValue);
}

private static void writeByte(byte[] bytes) {
    try {
        // Initialize a pointer in file using OutputStream
        OutputStream os = new FileOutputStream(file);
        // Starts writing the bytes in it
        os.write(bytes);
        System.out.println("Successfully byte inserted");
        // Close the file
        os.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

public static void main(String[] args) {
    //getSourceFile(sourceFile);
    System.out.println(sourceFile.length);
}

Solution

  • I finally solved my issue concerning the error message and the byte array. This code won't work in a real world situation. I'll use part of it to put it in the application. Instead of hard coding the array in the class, I put the content of the array in a text file. In the application I will receive a json with a byte array.

    // convert a file to string
    private static String fileToString() {
        StringBuilder stringBuilder = new StringBuilder();
        try(Stream<String> stream = Files.lines(Paths.get("source.txt"), StandardCharsets.UTF_8)) {
            stream.forEach(str -> stringBuilder.append(str).append(","));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // System.out.println("Strinbuilder : " + stringBuilder.toString());
        return stringBuilder.toString();
    }
    

    The second and third step are meant to convert the string to a byte array.

    // convert a string to array of string
    private static String[] stringToArray(String s) {
        String[] returnValue;
        String delimiter = ",";
    
        returnValue = s.split(delimiter);
    
        return returnValue;
    }
    
    // convert an array of string to an array of byte
    private static byte[] stringToByte(String[] str) {
        byte[] returnValue = new byte[str.length];
        try {
            for (int i = 0; i < str.length; i++) {
                returnValue[i] = (byte) Integer.parseInt(String.valueOf(str[i]));
                System.out.println("returnValue[i] = " + returnValue[i]);
            }
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    

    Finally I convert the byte array to pdf file.

    private static void writeByte(byte[] bytes) {
        try {
            // Initialize a pointer in file using OutputStream
            OutputStream os = new FileOutputStream("out.pdf");
            // Starts writing the bytes in it
            os.write(bytes);
            System.out.println("Successfully byte inserted");
            // Close the file
            os.close();
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
    

    The main class just aim to check if I generate the right output and it works fine.

    public static void main(String[] args) {
        String s = fileToString();
        String[] arr = stringToArray(s);
        byte[] byteArray = stringToByte(arr);
    
        assert byteArray != null;
        writeByte(byteArray);
    }
    

    Thanks to AntiqTech !