{"record":{"id":"b22da31f24bfcbdc","repo":"bumptech/glide","slug":"failed-to-read-all-expected-data-expected-conte","errorCode":null,"errorMessage":"Failed to read all expected data, expected: {contentLength}, but read: {readSoFar}","messagePattern":"Failed to read all expected data, expected: (.+?), but read: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/bumptech/glide/util/ContentLengthInputStream.java","lineNumber":78,"sourceCode":"    checkReadSoFarOrThrow(value >= 0 ? 1 : -1);\n    return value;\n  }\n\n  @Override\n  public int read(byte[] buffer) throws IOException {\n    return read(buffer, 0 /*byteOffset*/, buffer.length /*byteCount*/);\n  }\n\n  @Override\n  public synchronized int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {\n    return checkReadSoFarOrThrow(super.read(buffer, byteOffset, byteCount));\n  }\n\n  private int checkReadSoFarOrThrow(int read) throws IOException {\n    if (read >= 0) {\n      readSoFar += read;\n    } else if (contentLength - readSoFar > 0) {\n      throw new IOException(\n          \"Failed to read all expected data\"\n              + \", expected: \"\n              + contentLength\n              + \", but read: \"\n              + readSoFar);\n    }\n    return read;\n  }\n}\n","sourceCodeStart":60,"sourceCodeEnd":88,"githubUrl":"https://github.com/bumptech/glide/blob/eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a/library/src/main/java/com/bumptech/glide/util/ContentLengthInputStream.java#L60-L88","documentation":"Thrown by ContentLengthInputStream.checkReadSoFarOrThrow when the underlying stream returns end-of-stream (-1) but fewer bytes have been read than the Content-Length header declared. ContentLengthInputStream wraps an InputStream and verifies the byte count to catch truncated/corrupt network responses that would otherwise silently produce partial images.","triggerScenarios":"Any Glide network load (http/https URL model) where the server closes the connection before sending all the bytes advertised in Content-Length: proxy truncation, flaky mobile networks, server-side streaming bugs, or a misconfigured Content-Length header larger than the actual body.","commonSituations":"Spotty cellular/Wi-Fi dropping mid-download; CDN/proxy cache serving a truncated object; backend setting Content-Length incorrectly (e.g. with chunked encoding); server compression (gzip) without updating Content-Length.","solutions":["Add .error(fallbackDrawable) so the load degrades gracefully on truncation","Retry the load with RequestBuilder.listener logging or a built-in retry via .thumbnail()/error(RequestBuilder)","Investigate server/CDN: ensure Content-Length matches the actual body and that compression headers are consistent","If using a custom OkHttpUrlLoader, configure OkHttp retries/timeouts and disable broken intermediate proxies"],"exampleFix":"// before\nGlide.with(ctx).load(url).into(imageView) // crashes on truncated response\n\n// after\nGlide.with(ctx)\n  .load(url)\n  .error(R.drawable.fallback)\n  .listener(object : RequestListener<Drawable> {\n    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {\n        Log.w(TAG, \"Image load failed/truncated\", e)\n        return false\n    }\n    override fun onResourceReady(...) = false\n  })\n  .into(imageView)","handlingStrategy":"try-catch","validationCode":"// Cannot validate network truncation locally; rely on .error(fallback) + a listener.\n// Ensure server returns correct Content-Length and disable broken proxies.","typeGuard":null,"tryCatchPattern":"Glide.with(ctx)\n  .load(url)\n  .error(R.drawable.fallback)\n  .listener(loggingListener)\n  .into(imageView)\n// Where loggingListener.onLoadFailed logs the GlideException root causes\n// (truncation appears as 'Failed to read all expected data...').","preventionTips":["Always set .error(drawable) on network loads so truncation degrades gracefully","Attach a RequestListener that logs root causes to diagnose truncation early","Verify server Content-Length matches the body and is consistent with compression headers","Configure OkHttp retries/timeouts if using a custom OkHttpUrlLoader"],"tags":["android","glide","network","io","truncation","content-length"],"backgroundTag":null,"analyzedSha":"eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a","analyzedAt":"2026-08-14T01:43:54.821Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}