Base64 encode file by chunks

Adrian Veliz

I want to split a file into multiple chunks (in this case, trying lengths of 300) and base64 encode it, since loading the entire file to memory gives a negative array exception when base64 encoding it. I tried using the following code:

int offset = 0;
bis = new BufferedInputStream(new FileInputStream(f));
while(offset + 300 <= f.length()){
    byte[] temp = new byte[300];
    bis.skip(offset);
    bis.read(temp, 0, 300);
    offset += 300;
    System.out.println(Base64.encode(temp));
}
if(offset < f.length()){
    byte[] temp = new byte[(int) f.length() - offset];
    bis.skip(offset);
    bis.read(temp, 0, temp.length);
    System.out.println(Base64.encode(temp));
}

At first it appears to be working, however, at one point it switches to just printing out "AAAAAAAAA" and fills up the entire console with it, and the new file is corrupted when decoded. What could be causing this error?

Andreas

skip() "Skips over and discards n bytes of data from the input stream", and read() returns "the number of bytes read".

So, you read some bytes, skip some bytes, read some more, skip, .... eventually reaching EOF at which point read() returns -1, but you ignore that and use the content of temp which contains all 0's, that are then encoded to all A's.

Your code should be:

try (InputStream in = new BufferedInputStream(new FileInputStream(f))) {
    int len;
    byte[] temp = new byte[300];
    while ((len = in.read(temp)) > 0)
        System.out.println(Base64.encode(temp, 0, len));
}

This code reuses the single buffer allocated before the loop, so it will also cause much less garbage collection than your code.

If Base64.encode doesn't have a 3 parameter version, do this:

try (InputStream in = new BufferedInputStream(new FileInputStream(f))) {
    int len;
    byte[] temp = new byte[300];
    while ((len = in.read(temp)) > 0) {
        byte[] data;
        if (len == temp.length)
            data = temp;
        else {
            data = new byte[len];
            System.arraycopy(temp, 0, data, 0, len);
        }
        System.out.println(Base64.encode(data));
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

base64 encode, decode to, from files in chunks with python 2.7

From Dev

Encode Base64 String in Chunks in C#

From Dev

Base64 encode file and OutOfMemory error

From Dev

Encode zip file to Base64

From Dev

PHP base64 encode a pdf file

From Dev

Carrierwave encode file to base64 as process

From Dev

Encode file as base64 and send to API

From Dev

base64 encode image host url or server file path

From Dev

Base64 Encode Image and Save it later to a file

From Dev

Base64 Encode Image and Save it later to a file

From Dev

Encode MP3 file to base64 string in Ionic

From Dev

Encode image file to base 64

From Dev

base64 encode HttpPostedFileBase

From Dev

Javascript base64 encode

From Dev

Python - generate csv file in memory and then encode its data into base64?

From Dev

JS: how can I base64 encode a local file without XMLHttpRequest?

From Dev

Given a URL, how to encode a file's contents as base64 with Python / Django?

From Dev

How encode in base64 a file generated with jspdf and html2canvas?

From Dev

What's the right way to base64 encode a binary file on CentOS 7?

From Dev

Vba Excel. Resize image when loaded from file and then encode it base64

From Dev

Encode Carrierwave attachment to base64 in Rails

From Dev

How to encode an image resource to base64?

From Dev

Encode a ImageMagick image with Base64

From Java

Base64 Java encode and decode a string

From Dev

Angular 2 encode image to base64

From Dev

Encode a base64 request body into a binary

From Dev

How to (base64) encode a protobuf as a String

From Dev

Encode a FileStream to base64 with c#

From Dev

How to encode text to base64 in python

Related Related

HotTag

Archive