1

I have a file, then I convert it to base64 and finaly this base64 is stored into a String variable.

And the question is, what is the maximum size of the file that I can convert to base64 and store it in a String variable?

If anyone could help me. Thank you in advance

(With Java of course...)

LetsGoBrandon
  • 498
  • 8
  • 23
  • 1
    See also [How many characters can a Java String have?](https://stackoverflow.com/questions/1179983/how-many-characters-can-a-java-string-have) – Andy Thomas May 26 '17 at 16:28

1 Answers1

2

Strings can hold a lot of data* - I'd be more concerned about your available heap memory if you're putting a whole file into memory that way. Especially as you have to be careful not to have it in memory twice for the base64 conversion...

*String is internally backed by an array and can hold up tp Integer.MAX_VALUE characters (2^31-1)

If you do have enough heap memory for your process you can calculate like this:

  • A char value in Java takes up 2 bytes of memory
  • Every character of your base64 encoded file takes of two bytes internally
  • base64 encoding makes 4 characters out of 3 bytes.

Apply some math and see if it fits or think hard and long why you need that data in memory in the first place.

Jan
  • 13,738
  • 3
  • 30
  • 55