Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:23:59+00:00 2026-05-30T21:23:59+00:00

Using Base64 from Apache commons public byte[] encode(File file) throws FileNotFoundException, IOException { byte[]

  • 0

Using Base64 from Apache commons

public byte[] encode(File file) throws FileNotFoundException, IOException {
        byte[] encoded;
        try (FileInputStream fin = new FileInputStream(file)) {
            byte fileContent[] = new byte[(int) file.length()];
            fin.read(fileContent);
            encoded = Base64.encodeBase64(fileContent);
        }
        return encoded;   
}


Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at org.apache.commons.codec.binary.BaseNCodec.encode(BaseNCodec.java:342)
    at org.apache.commons.codec.binary.Base64.encodeBase64(Base64.java:657)
    at org.apache.commons.codec.binary.Base64.encodeBase64(Base64.java:622)
    at org.apache.commons.codec.binary.Base64.encodeBase64(Base64.java:604)

I’m making small app for mobile device.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:24:00+00:00Added an answer on May 30, 2026 at 9:24 pm

    You cannot just load the whole file into memory, like here:

    byte fileContent[] = new byte[(int) file.length()];
    fin.read(fileContent);
    

    Instead load the file chunk by chunk and encode it in parts. Base64 is a simple encoding, it is enough to load 3 bytes and encode them at a time (this will produce 4 bytes after encoding). For performance reasons consider loading multiples of 3 bytes, e.g. 3000 bytes – should be just fine. Also consider buffering input file.

    An example:

    byte fileContent[] = new byte[3000];
    try (FileInputStream fin = new FileInputStream(file)) {
        while(fin.read(fileContent) >= 0) {
             Base64.encodeBase64(fileContent);
        }
    }
    

    Note that you cannot simply append results of Base64.encodeBase64() to encoded bbyte array. Actually, it is not loading the file but encoding it to Base64 causing the out-of-memory problem. This is understandable because Base64 version is bigger (and you already have a file occupying a lot of memory).

    Consider changing your method to:

    public void encode(File file, OutputStream base64OutputStream)
    

    and sending Base64-encoded data directly to the base64OutputStream rather than returning it.

    UPDATE: Thanks to @StephenC I developed much easier version:

    public void encode(File file, OutputStream base64OutputStream) {
      InputStream is = new FileInputStream(file);
      OutputStream out = new Base64OutputStream(base64OutputStream)
      IOUtils.copy(is, out);
      is.close();
      out.close();
    }
    

    It uses Base64OutputStream that translates input to Base64 on-the-fly and IOUtils class from Apache Commons IO.

    Note: you must close the FileInputStream and Base64OutputStream explicitly to print = if required but buffering is handled by IOUtils.copy().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want get the image from an xml file which is encoded using base64.
I'm using HTTPService with a POST operation to submit a Base64 encoded file (taken
I am currently getting a base64 encoded image from a mobile device's camera using
I occasionally get an IOException from a Servlet I have that writes a byte
I'm using the window.atob('string') function to decode a string from base64 to a string.
I am trying to take a Base64 encoded image from the database and save
I am using standard input and output to pass 2 base64 strings from one
I want to convert some base64 encoded png images to jpg using python. I
I have been able to do base64 binary encode using iterators like base64_from_binary<transform_width<const char
I am trying to use the Base64 encoding functionality from Apache Common. But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.