Prabha Karan
Prabha Karan

Reputation: 1319

java.lang.OutOfMemoryError at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)?

I am facing this issue in uploading the file to the google drive, i am uploading the recorded audio to the google drive at that time this exception is occurring

The code used for writing the content in the file

                      OutputStream outputStream = result.getDriveContents().getOutputStream();
                    FileInputStream fis;
                    try {
                        fis = new FileInputStream(file);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buf = new byte[1024];
                        int n;
                        while (-1 != (n = fis.read(buf)))
                            baos.write(buf, 0, n);
                        byte[] photoBytes = baos.toByteArray();
                        outputStream.write(photoBytes);

                        outputStream.close();
                        outputStream = null;
                        fis.close();
                        fis = null;

                        Log.e("File Size", "Size " + file.length());

                    } catch (FileNotFoundException e) {
                        Log.v("EXCEPTION", "FileNotFoundException: " + e.getMessage());
                    } catch (IOException e1) {
                        Log.v("EXCEPTION", "Unable to write file contents." + e1.getMessage());
                    }

The exception occurs in the line ` baos.write(buf, 0, n);

Please help me how to solve this error.`

Upvotes: 1

Views: 3223

Answers (2)

piet.t
piet.t

Reputation: 11911

Writing to a ByteArrayOutputStream first means that the complete file will end up in the JVM's heap. Depending on the file size and heap size this might not be possible, hence the exception. If you don't need the ByteArrayOutputStream for anything else just write directly to outputStream:

OutputStream outputStream = result.getDriveContents().getOutputStream();
FileInputStream fis;
try {
    fis = new FileInputStream(file);
    byte[] buf = new byte[1024];
    int n;
    while (-1 != (n = fis.read(buf)))
        outputStream.write(buf, 0, n);


} catch (FileNotFoundException e) {
    Log.v("EXCEPTION", "FileNotFoundException: " + e.getMessage());
} catch (IOException e1) {
    Log.v("EXCEPTION", "Unable to write file contents." + e1.getMessage());
} finally {
    outputStream.close();
    fis.close();
    Log.e("File Size", "Size " + file.length());
}

P.S.: nulling the references should not be necessary if they go out of scope soon...

Upvotes: 1

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10871

You are getting OOM because you try to read full file into the memory before writing it to the google drive outputStream. The file may be too large to be stored in the memory. This way you need to write it part by part. It is easy to accomplish using this method:

  private static final int BUFFER_SIZE = 1024;

  public static long copy(InputStream from, OutputStream to)
      throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    long total = 0;
    while (true) {
      int r = from.read(buffer);
      if (r == -1) {
        break;
      }
      to.write(buffer, 0, r);
      total += r;
    }
    return total;
  }

The method will return number of bytes copied.

Upvotes: 1

Related Questions