{"record":{"id":"125b765962c8aecf","repo":"apache/beam","slug":"caller-does-not-own-the-underlying-input-stream-and-should","errorCode":null,"errorMessage":"Caller does not own the underlying input stream  and should not call close().","messagePattern":"Caller does not own the underlying input stream  and should not call close\\(\\)\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/util/UnownedInputStream.java","lineNumber":44,"sourceCode":"import org.checkerframework.checker.nullness.qual.Nullable;\n\n/**\n * A {@link OutputStream} wrapper which protects against the user attempting to modify the\n * underlying stream by closing it or using mark.\n */\n@Internal\npublic class UnownedInputStream extends FilterInputStream {\n  public UnownedInputStream(InputStream delegate) {\n    super(delegate);\n  }\n\n  InputStream getWrappedStream() {\n    return in;\n  }\n\n  @Override\n  public void close() throws IOException {\n    throw new UnsupportedOperationException(\n        \"Caller does not own the underlying input stream \" + \" and should not call close().\");\n  }\n\n  @Override\n  public boolean equals(@Nullable Object obj) {\n    return obj instanceof UnownedInputStream && ((UnownedInputStream) obj).in.equals(in);\n  }\n\n  @Override\n  public int hashCode() {\n    return in.hashCode();\n  }\n\n  @SuppressWarnings(\"UnsynchronizedOverridesSynchronized\")\n  @Override\n  public void mark(int readlimit) {\n    throw new UnsupportedOperationException(\n        \"Caller does not own the underlying input stream \" + \" and should not call mark().\");","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/util/UnownedInputStream.java#L26-L62","documentation":"UnownedInputStream wraps an InputStream that the wrapper does not own, so close() deliberately throws UnsupportedOperationException. The library is enforcing resource ownership: closing the wrapper would close a stream the caller does not own, breaking whoever actually manages it.","triggerScenarios":"Calling close() on an UnownedInputStream obtained e.g. from Avro/GCS readable byte channel wrappers, typically via try-with-resources on the wrapper instead of the owned stream, or when a utility auto-closes streams it is handed.","commonSituations":"Using try-with-resources around an UnownedInputStream, passing the wrapper to code that closes its input (e.g. IOUtils.closeQuietly), or refactoring code that previously wrapped an owned FileInputStream.","solutions":["Remove close() from the UnownedInputStream; close the underlying owned stream instead.","Restructure so try-with-resources wraps the original owned InputStream, not the wrapper.","If a consuming API demands a closeable stream, wrap with a CloseableInputStream (e.g. Guava's) or use ByteStreams.limit/Joiner semantics that don't propagate close.","If you truly own the underlying stream and want close to work, pass it directly rather than through UnownedInputStream."],"exampleFix":"// before\ntry (InputStream in = new UnownedInputStream(ownedStream)) {\n  readAll(in);\n} // throws UnsupportedOperationException\n// after\ntry (InputStream owned = ownedStream) {\n  InputStream in = new UnownedInputStream(owned);\n  readAll(in);\n} // owned stream closed by caller-owned resource","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"boolean safeToClose(InputStream in) {\n  return !(in instanceof UnownedInputStream);\n}","tryCatchPattern":"try {\n  in.close();\n} catch (UnsupportedOperationException e) {\n  // expected: caller does not own the stream; close the owned stream elsewhere\n}","preventionTips":["Never put UnownedInputStream in try-with-resources.","Close the stream you created, not wrappers around someone else's.","Check for UnownedInputStream before delegating close in utility code."],"tags":["beam","io","stream","ownership"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}