Consume a c# base64 encoded file in java

I want to transfer a file from C# to a java webservice which accepts base64 strings. The problem is that when I encode the file using the c# Convert class, it produces a string based on a little endian unsigned byte[].

In Java byte[] are signed / big endian. When I decode the delivered string, I get a different byte[] and therefor the file is corrupt.

How can I encode a byte[] in C# to a base64, which is equal to the byte[] that is decoded in java using the same string?

C# side:

byte[] attachment = File.ReadAllBytes(@"c:\temp\test.pdf");
String attachmentBase64 = Convert.ToBase64String(attachment, Base64FormattingOptions.None);

Java side:

  @POST
  @Path("/localpdf")
  @Consumes("text/xml")
  @Produces("text/xml")
  public String getExtractedDataFromEncodedPdf(@FormParam("file") String base64String) {

      if(base64String == null) return null;


      byte[] data = Base64.decodeBase64(base64String.getBytes());

      FileOutputStream ms;
    try {
        ms = new FileOutputStream(new File("C:\\Temp\\test1234.pdf"));
        ms.write(data);
        ms.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

File test1234.pdf is corrupt.

Jon Skeet
people
quotationmark

"Signed" and "big-endian" are very different things, and I believe you're confusing yourself.

Yes, bytes are signed in Java and unsigned in C# - but I strongly suspect you're getting the same actual bits in both cases... it's just that a bit pattern of (say) 11111111 represents 255 in C# and -1 in Java. Unless you're viewing the bytes as numbers (which is rarely useful) it won't matter - it certainly doesn't matter if you just use the bytes to write out a file on the Java side.

people

See more on this question at Stackoverflow