title: Out of memory while encoding to Base64
body: <p>I am dividing a file into chunks and each chunk is encoded into Base64. But I am getting OutOfMemory error on <code>encodeToString</code>. My code is as follows:</p>
  
  <pre><code>try {
        in = new FileInputStream(zipEntity.zip_file);
        long bytesLeft = in.available();
  
        long chunkOffset = 0;
  
        int i = 0;
        while (bytesLeft &gt; 0) {
        i++;
        // TODO: what happens if int MAX_VALUE exceeded?
        long maxChunkSize = chunkSizeBytes &gt; 0 ? chunkSizeBytes : bytesLeft;
  
        byte[] buffer = new byte[(int) Math.min(bytesLeft, maxChunkSize)];
        int read = in.read(buffer, 0, buffer.length);
  
        bytesLeft -= read;
  
        Log.e("", "===== " + i + ", BYTES LEFT === " + bytesLeft);
  
        boolean isLast = bytesLeft == 0;
        chunks.add(new FileChunkEntity(Base64.encodeToString(buffer, Base64.DEFAULT), chunkOffset + "", isLast));
  
        i++;
        chunkOffset += buffer.length;
      }
     } catch (Exception ex) {
        ex.printStackTrace();
     } finally {
         if (in != null)
             try {
                   in.close();
                 } catch (IOException ex) {
                     ex.printStackTrace();
                 }
     }
  </code></pre>
  
  <p>I am getting error at this line:</p>
  
  <pre><code>chunks.add(new FileChunkEntity(Base64.encodeToString(buffer, Base64.DEFAULT), chunkOffset + "", isLast));
  </code></pre>
  
  <p>I have tried the same with <code>Base64OutputStream</code>, but String obtained from <code>Base64.encodeToString</code> is not matching with <code>outputStream.toString</code>.</p>
  
  <p>When I tried <code>outputStream.toByteArray)</code>, I didn't got out of memory error but <code>new String(outputStream.toByteArray)</code> didn't matched with <code>Base64.encodeToString()</code> </p>