{"record":{"id":"4adf3b4382f222d0","repo":"apache/hadoop","slug":"classname-does-not-support-positioned-read","errorCode":null,"errorMessage":"${className} does not support positioned read.","messagePattern":"(.+?) does not support positioned read\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java","lineNumber":337,"sourceCode":"  @Override\n  public synchronized void close() throws IOException {\n    if (closed) {\n      return;\n    }\n    \n    super.close();\n    freeBuffers();\n    codec.close();\n    closed = true;\n  }\n  \n  /** Positioned read. It is thread-safe */\n  @Override\n  public int read(long position, byte[] buffer, int offset, int length)\n      throws IOException {\n    checkStream();\n    if (!(in instanceof PositionedReadable)) {\n      throw new UnsupportedOperationException(in.getClass().getCanonicalName()\n          + \" does not support positioned read.\");\n    }\n    final int n = ((PositionedReadable) in).read(position, buffer, offset,\n        length);\n    if (n > 0) {\n      // This operation does not change the current offset of the file\n      decrypt(position, buffer, offset, n);\n    }\n\n    return n;\n  }\n\n  /**\n   * Positioned read using {@link ByteBuffer}s. This method is thread-safe.\n   */\n  @Override\n  public int read(long position, final ByteBuffer buf)\n      throws IOException {","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java#L319-L355","documentation":"CryptoInputStream decrypts on the fly by wrapping another InputStream, and can only offer positioned reads if the wrapped stream implements PositionedReadable. read(long, byte[], int, int) checks `in instanceof PositionedReadable` and throws UnsupportedOperationException naming the wrapped class before any I/O when that interface is missing.","triggerScenarios":"Calling read(position, buffer, offset, length) (directly or via FSDataInputStream positional APIs) on a CryptoInputStream whose inner stream is a plain sequential InputStream — raw local streams, some object-store/wrapped connector streams, or mock streams in tests.","commonSituations":"Reading files in HDFS encryption zones with client libraries that rely on positional reads (parquet/orc style split readers); unit tests wrapping arbitrary streams with CryptoInputStream.","solutions":["Use sequential read() plus seek-based navigation instead of positional reads on encrypted files","If you control the source stream, wrap it in a PositionedReadable implementation before constructing the CryptoInputStream","Catch UnsupportedOperationException and fall back to seek + sequential read"],"exampleFix":"// before\nint n = cryptoIn.read(pos, buf, 0, len); // UOE if inner stream lacks PositionedReadable\n\n// after\ntry {\n  int n = cryptoIn.read(pos, buf, 0, len);\n} catch (UnsupportedOperationException e) {\n  cryptoIn.seek(pos);\n  IOUtils.readFully(cryptoIn, buf, 0, len); // sequential fallback\n}","handlingStrategy":"try-catch","validationCode":"// CryptoInputStream implements PositionedReadable itself, so instanceof cannot\n// narrow the inner stream. Pre-check the encryption context instead:\n// EncryptionZone ez = hdfsAdmin.getEncryptionZoneForPath(path);\n// if (ez != null) use sequential reads only for this path.","typeGuard":null,"tryCatchPattern":"try {\n  n = in.read(pos, buf, off, len);\n} catch (UnsupportedOperationException e) {\n  // inner stream lacks PositionedReadable: sequential fallback\n  in.seek(pos);\n  n = in.read(buf, off, len);\n}","preventionTips":["Avoid positional reads on files inside encryption zones unless the underlying client streams are known to implement PositionedReadable","Centralize stream capability fallbacks (positional -> seek+read) in one helper used by all readers","In tests, wrap mock streams with PositionedReadable to match production stream types"],"tags":["crypto","hdfs","stream","positioned-read","hadoop"],"backgroundTag":"unsupported-stream-operation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}