title: Memory out of bound while uploading a large file
body: <p>I am getting a memory out of bound exception while uploading a large file to server. </p>
  
  <p>Here is my StackTrace: </p>
  
  <pre><code>09-20 12:21:07.985  7011  7562 E AndroidRuntime: java.lang.OutOfMemoryError: Failed to allocate a 2060 byte allocation with 1856 free bytes and 1856B until OOM
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.android.okio.Segment.&lt;init&gt;(Segment.java:34)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.android.okio.SegmentPool.take(SegmentPool.java:48)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.android.okio.OkBuffer.writableSegment(OkBuffer.java:511)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.android.okio.OkBuffer.write(OkBuffer.java:424)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.android.okio.RealBufferedSink$1.write(RealBufferedSink.java:130)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at java.io.DataOutputStream.write(DataOutputStream.java:98)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.ruf.rufdrive.server.request.HttpClient.addFileBytes(HttpClient.java:296)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.ruf.rufdrive.server.request.service.UploadIntentService.convertFileToByteArray(UploadIntentService.java:327)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.ruf.rufdrive.server.request.service.UploadIntentService.upload(UploadIntentService.java:209)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.ruf.rufdrive.server.request.service.UploadIntentService.uploadFile(UploadIntentService.java:407)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.ruf.rufdrive.server.request.service.UploadIntentService.onHandleIntent(UploadIntentService.java:119)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at com.ruf.rufdrive.server.request.service.UploadIntentService$1.run(UploadIntentService.java:94)
  09-20 12:21:07.985  7011  7562 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:818)
  </code></pre>
  
  <p>Here is my code for the upload:</p>
  
  <pre><code>   InputStream inputStream = new FileInputStream(f);
     bytesAvailable = inputStream.available();
     maxProgress = bytesAvailable;
  
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     int bufferSize = Math.min(bytesAvailable, maxBufferSize);
     if(bufferSize == 0)
      throw  new FileNotFoundException("file not found");
     byte[] b = new byte[bufferSize];
     int bytesRead =0;
     long notificationTriggerAmount = bytesAvailable /100;
     long nextNotificationTrigger = 0;
     while ((bytesRead = inputStream.read(b)) != -1)
     {       
          httpClient.addFileBytes(b, bufferSize);
          bytesAvailable = inputStream.available();
          Log.v("Available",bytesAvailable+"");
          bufferSize = Math.min(bytesAvailable,maxBufferSize);       
     }
  }
  </code></pre>