Forum Discussion

roryLIT's avatar
roryLIT
Contributor
8 years ago

Memory Issue with encoding a file to base64

Hi,

 

I am able to encode a file to base64 using groovy script however it gives memory issues. Is there a way to do this so memory is not affected? I would like to achieve this using groovy, not using the attachments functionality in the request

 

Below is the code I am currently using that works but with memory issues. Example file location: C:\Users\Public\Pictures\Sample Pictures\Desert.jpg

 

import org.apache.commons.codec.binary.Base64;

String filePath = testRunner.testCase.testSuite.project.getPropertyValue("filePath")
File fileName = new File(filePath);
String encodedBase64 = null;
FileInputStream fileInputStreamReader = new FileInputStream(fileName);
byte[] bytes = new byte[(int) fileName.length()];
fileInputStreamReader.read(bytes);
encodedBase64 = new String(Base64.encodeBase64(bytes));
fileInputStreamReader.close();
testRunner.testCase.testSuite.project.setPropertyValue("encodedFilePath", encodedBase64)

 

Thanks,

 

Rory

  • nmrao's avatar
    nmrao
    Champion Level 3
    Do you want elaborate "Memory issues"? How do come to know?
  • So it encodes the file ok and sends it in the request but when I run the automated test through Jenkins I get an 'out of memory' error on the step that is doing the encoding. I researched online and saw a potential solution where you use an outputstream for base64 instead and read in the bytes of the file in parts if that makes sense. I just can't get my head around coding it. Any ideas?

    Thanks