title: Google Protobuf format error: input ended unexpectedly in the middle of a field
body: <p>I am parsing a <a href="https://developers.google.com/transit/gtfs-realtime/" rel="noreferrer">GftsRealtime</a> feed using <a href="https://github.com/google/gtfs-realtime-bindings/tree/master/java" rel="noreferrer">google protobuf parser</a>. Here is the exact error I am encountering: 
  <code>com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either than the input has been truncated or that an embedded message misreported its own length.</code>
  First thought that comes to mind is that the integrity of the file was compromised while downloading but I check the file in the Android emulator against the one I download on the desktop and it's the same. Has anyone encountered this issue before? I am using version '<a href="https://github.com/google/gtfs-realtime-bindings/blob/master/java/README.md" rel="noreferrer">0.0.4'</a> of the gfts-realtime-parser. The feed is <a href="http://developer.mbta.com/lib/GTRTFS/Alerts/TripUpdates.pb" rel="noreferrer">here</a>. </p>
  
  <p>EDIT ----</p>
  
  <p>Here is the code I use to download the file:</p>
  
  <pre><code>override fun doInBackground(vararg urls: URL): Boolean? {
          val httpClient = OkHttpClient()
          val call = httpClient.newCall(Request.Builder().url(urls[0]).get().build())
          val urlStr = urls[0].toString()
          val fileName = urlStr.substring(urlStr.lastIndexOf('/') + 1, urlStr.length)
          val file = File(context.filesDir, fileName)
          try {
              val response = call.execute()
              if (response.code() === 200) {
                  var inputStream: InputStream? = null
                  val outputStream = FileOutputStream(file)
                  try {
                      inputStream = response.body().byteStream()
                      val buff = ByteArray(1024 * 4)
                      var downloaded: Long = 0
                      val target = response.body().contentLength()
                      publishProgress(0L, target)
                      while (true) {
                          val readed = inputStream!!.read(buff)
                          if (readed == -1) {
                              break
                          }
                          outputStream.write(buff, 0, readed)
                          //write buff
                          downloaded += readed.toLong()
                          publishProgress(downloaded, target)
                          if (isCancelled()) {
                              fileListener(null)
                              return false
                          }
                      }
                      fileListener(file)
                      return downloaded == target
                  } catch (ignore: IOException) {
                      fileListener(null)
                      return false
                  } finally {
                      if (inputStream != null) {
                          inputStream!!.close()
                      }
                      outputStream.flush()
                      outputStream.close()
                  }
              } else {
                  fileListener(null)
                  return false
              }
          } catch (e: Exception) {
              e.printStackTrace()
              fileListener(null)
              return false
          }
      }
  </code></pre>
  
  <p>EDIT 2 ---- 
  Here is the complete code path</p>
  
  <pre><code>override fun onHandleIntent(intent: Intent?) {
          Realm.init(applicationContext)
          val config = RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build() // TODO remove before deploy
          RealmInstace = Realm.getInstance(config)
  
          Timber.i("Realm file path: ", RealmInstace?.path)
          val action = intent?.action
          when (action) {
              ACTION_UPDATE_GFTS -&gt; updateGfts()
              ACTION_UPDATE_GFTS_REALTIME -&gt; updateGftsReal() // this is called
              REALM_GET_ALL_TRAIN_MAP -&gt; getTrainMapData(intent)
          }
          exportDatabase()
  //        RealmInstace?.close()
          RealmInstace = null
      }
  
  private fun updateGftsReal() {
          var file:File?=null
          AsyncDownloader(this, null, { f -&gt;
              file = f
              if(file!=null &amp;&amp; file!!.exists())
                  GftsRealRealmParser(applicationContext, file!!, RealmInstace!!)
              else
                  Timber.e("Failed to download trip updates", GFTS_REALTIME_TRIP_URL)
          }).execute(URL(GFTS_REALTIME_TRIP_URL)).get(2, TimeUnit.MINUTES)
      }
  
  class GftsRealRealmParser(val context: Context, val file: File, val realm: Realm) {
      init {
          var feedr: FeedMessageR? = null
          var inputStream = BufferedInputStream(FileInputStream(file))
          try {
              val url = URL("http://developer.mbta.com/lib/GTRTFS/Alerts/TripUpdates.pb")
              val feed = GtfsRealtime.FeedMessage.parseDelimitedFrom(url.openStream())
              feedr = FeedMessageR(feed)
              realm.executeTransaction {
                  Timber.i("Committing ${feedr!!.entityList.size} to the realtime db.")
                  realm?.copyToRealm(feedr)
  //                val train = realm.where(TripDescriptorR::class.java).contains("route_id", "31760754").findFirst()
              }
          } catch (e: IOException) {
              Timber.e(e.message, e)
              e.printStackTrace()
          } catch(e: Exception) {
              Timber.e(e.message, e)
              e.printStackTrace()
          }
          if (feedr == null)
              throw ParseException("Failed to parse file", 0)
      }
  }
  </code></pre>
  
  <p>EDIT 4 ----</p>
  
  <p>see answer below</p>