1

I have a piece of JAVA code as follows:

public static byte[] PackFile(String file) throws IOException {
        File f = new File(file);
        if (f.exists()) {
            String fileName = f.getName();
            int fileSize = (int) f.length();
            try {
                byte[] fileBytes = new byte[fileSize];
                byte[] filenameBytes = (fileName + '\0').getBytes();                         

                FileInputStream stream = new FileInputStream(file);
                DataInputStream ds = new DataInputStream(stream);
                for (int i = 0; i < fileSize; i++) {
                    fileBytes[i] = ds.readByte();
                }

                int length = fileBytes.length + filenameBytes.length + 4;
                ByteBuffer buffer = ByteBuffer.allocate(length);
                buffer.put((fileName + '\0').getBytes());

                ByteBuffer bufferLength = ByteBuffer.allocate(4);
                bufferLength.order(ByteOrder.LITTLE_ENDIAN);
                bufferLength.putInt(fileSize);
                
                buffer.put(bufferLength.array());

                buffer.put(fileBytes);
                return buffer.array();

            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }

        }
        return null;
    }

Variable buffer Put [fileName, fileSize, and fileBytes]. Is it possible to implement this in APEX?

Blob Development Documentation

There is no Put method in the documentation, is there any other solution?

This is the APEX code I tried (incorrect)

ContentVersion [] ver = [SELECT Id,VersionData,ContentSize FROM ContentVersion WHERE ContentdocumentId = '069F7000002BlEeIAK'];
String fileBlob = 'image.gif' + String.valueOf(ver[0].ContentSize) + ver[0].VersionData;
System.debug(LoggingLevel.INFO, 'base64 = ' + EncodingUtil.base64Encode(Blob.valueOf(fileBlob)));

1 Answer 1

4

Apex can't operate on binary data directly. That said, it should be possible to do this with some manipulation.

Blob binaryOutput =
  EncodingUtil.convertFromHex(
    EncodingUtil.convertToHex(Blob.valueOf(fileName)) +
    '00' +
    EncodingUtil.convertToHex(Blob.valueOf(
      String.fromCharArray(
        new Intger[] {
          fileSize & 255,
          (fileSize >> 8) & 255,
          (fileSize >> 16) & 255,
          (fileSize >> 24) & 255
        }
      )
    )) +
    EncodingUtil.convertToHex(fileData)
  );

The output from this should include the file name, zero terminated, followed by a 4 byte size prefix, followed by the file's contents.

The output is suitable for a HttpRequest call or storing in a file.

Note that because of the conversion to hex, the maximum file size will be just under half of Limit.getLimitHeapSize().

You could get closer to 4:3 Limit.getLimitHeapSize() by using Base64, but this would make the code a lot more complicated.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .