Search code examples
javac#restbytekofax

Converting a byte array onto a C# byte array before Post request


I have to build a JSON object which will be like this :

{
  "sessionId": "DA2F5102377742BE8063ADBC8968A294",
  "documentId": "c2f84034-ea1c-4406-8a8b-ab2c00a1b537",
  "sourceFile": {
    "MimeType": "application/pdf",
    "SourceFile": [
      25,
      68,
      56,...
    ]
}

For this i created 2 POJOs :

@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class UpdateSourceFileRequestModel {

    private String sessionId;
    private ReportingDataRequestModel reportingData;
    private String documentId;
    private DocumentSourceFileRequestModel sourceFile;
}

And :

@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class DocumentSourceFileRequestModel {
    // for GettingSourceFile
    @SerializedName("MimeType")
    private String mimeType;
    @SerializedName("SourceFile")
    private int[] sourceFile;
}

I have to use SerializedName because JSON is case sensitive. So far so good, everything's fine until there.

Now, I'm trying to convert a pdf file onto a byte array. It can be easy be done by reading the file :

byte[] array = Files.readAllBytes(Paths.get(fileToReturn));

My issue is that i have to return a C# byte array (unsigned numbers from 0 to 255). In order to solve this issue I tried this :

int[] result = new int[array.length];
for(int i = 0; i < array.length; i++)   {
     int posInt = array[i] & 0xFF;
     byte bValue = (byte) posInt;
     result[i] = bValue;
 }

I copied a signed byte array (-128 +127) and converted it in a int array (0 to 255) in order to match the requirements of the API. This code was found on stackoverflow. My issue is that the converted file is not readable with acrobat.

Here is the whole method code :

private void updateSourceFile(String fileToReturn, LogOnWithPassword2RestResponseModel login) throws IOException {
    System.out.println("\nSending HTTP POST request - returning file process");
    // retrieve the file
    Path path = Paths.get(fileToReturn);
    File f = path.toFile();
    // convert file to byte array
    byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
    int[] result = new int[array.length];
    for(int i = 0; i < array.length; i++)   {

        int posInt = array[i] & 0xFF;
        byte anotherB = (byte) posInt;
        result[i] =anotherB;
    }

    UpdateSourceFileRequestModel updateSourceFileRequestModel = new UpdateSourceFileRequestModel();
    DocumentSourceFileRequestModel documentSourceFileRequestModel = new DocumentSourceFileRequestModel();
    documentSourceFileRequestModel.setMimeType("application/pdf");
    documentSourceFileRequestModel.setSourceFile(result);

    updateSourceFileRequestModel.setSessionId(login.getD().getSessionId());
    updateSourceFileRequestModel.setDocumentId(docId);
    updateSourceFileRequestModel.setSourceFile(documentSourceFileRequestModel);
    updateSourceFileRequestModel.setReportingData(null);

    String url = "http://localhost/TotalAgility/Services/SDK/CaptureDocumentService.svc/json/UpdateSourceFile";

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);
    String jsonInputString = gson.toJson(updateSourceFileRequestModel);
    StringEntity postingString = new StringEntity(jsonInputString);
    post.setEntity(postingString);
    post.setHeader("Content-type", "application/json");
    post.setHeader("accept", "application/json");
    HttpResponse response = httpClient.execute(post);
}

Solution

  • These lines:

     byte bValue = (byte) posInt;
     result[i] = bValue;
    

    defeat the purpose of converting to an integer because you literally take the integer result (often >127) and cast it back to a byte (which will result in negative values past 127 again). Since the issue is a proper conversion to json with no negative values, absolutely do not convert it to a byte again after getting it into an integer.

    This is just fine:

    int[] result = new int[array.length];
    for(int i = 0; i < array.length; i++)   {
         result[i] = array[i] & 0xFF;
     }