title: Socket programming in android - Reading bytes from input stream is very slow over Wifi
body: <p>I am using Sockets for transferring data over Wifi in my android app, I have set the Buffer size to around 10MB and here is my code for sending data.</p>
  <pre class="lang-kotlin prettyprint-override"><code>// Sending data from a file in chunks, PREFERRED_CHUNK_SIZE is [1024 * 1024 * 10]
  
  var fileSize = fileList[index].totalSize
  val buffer = ByteArray(SocketConstants.PREFERRED_CHUNK_SIZE)
  var length: Int
  do {
      length = stream.read(buffer, 0, min(buffer.size, fileSize))
      bos.write(buffer, 0, length)
      fileSize -= length
  } while (fileSize != 0)
   stream.close()
  </code></pre>
  <p>I am reusing the same code for multiple file transfer, the condition filesize != 0 makes sure I read only that much bytes for a single file and thus I have used that min function lets say I want to send 36 MB it is sent as 10(10485760 bytes), 10(10485760 bytes), 10(10,485,760 bytes), 6.</p>
  <p>Below is my code for receiver:</p>
  <pre class="lang-kotlin prettyprint-override"><code>        var fileSize = file.totalSize
          var current: Int
          var offset = 0
          val byteArray = ByteArray(SocketConstants.PREFERRED_CHUNK_SIZE)
          val bufferedInputStream = BufferedInputStream(inputStream)
          do {
              current = bufferedInputStream.read(byteArray, 0, min(byteArray.size, fileSize))
  //            outputStream.write(byteArray, 0, current)  To ignore file write for now
              fileSize -= current
              offset += current
              println(&quot;Length: $current&quot;)
              file.bytesDownloaded = offset
              updateList(offset)
          } while (fileSize != 0)
          outputStream.flush()
          outputStream.close()
  </code></pre>
  <p>When reading I am getting really small chunks of bytes, 1358, 1358, 1358, 1358, 1358.</p>
  <p>This is really slow, I don't understand what is causing inputStream.read() such small reads.</p>
  <p>I have already set the sender and receiver buffer size for SocketServer and Socket instance. But there was no difference in the results</p>