title: How to prevent closing InputStream by AutoCloseInputStream
body: <p>What I want to do: open InputStream for big file, read it by 10MB chunks, upload a chunk, read next chunk.</p>
  
  <pre><code>val chunkCount = Math.ceil(totalSize.toDouble() / chunkSize.toDouble()).toInt()
  for (chunkNumber in (0..chunkCount-1)) {
      var chunk = ByteArray(chunkSize)
      val currentChunkSize = inputStream.read(chunk)
      if (chunk.size != currentChunkSize) { //last chunk
          chunk = chunk.copyOf(currentChunkSize)
      }
      // uploading of chunk
  }
  </code></pre>
  
  <p>I wrote good code for this task, but the problem is the ContentResolver returns ParcelFileDescriptor.AutoCloseInputStream as InputStream. This implementation breaks a contract of InputStream, because it closes itself after first .read(), so when I am trying to read next chunk, I am getting IOException - "The stream was closed". </p>
  
  <p>I don't want get the whole array at first .read(), because file can be large, and it will worsen perfomance or can call OutOfMemory error.</p>
  
  <p>How to prevent closing InputStream by AutoCloseInputStream, or make ContentResolver return normal FileInputStream?</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>Problem is not in AutoCloseInputStream, but in Android system, which closes the stream if application memory is low.</p>