{"record":{"id":"feb2440e43261d00","repo":"apache/beam","slug":"gcsseekablebytechannels-are-read-only-and-cannot-be","errorCode":null,"errorMessage":"GcsSeekableByteChannels are read-only and cannot be truncated.","messagePattern":"GcsSeekableByteChannels are read-only and cannot be truncated\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtilV2.java","lineNumber":539,"sourceCode":"      checkArgument(newPosition >= 0, \"Position must be non-negative: %s\", newPosition);\n      reader.seek(newPosition);\n      this.position = newPosition;\n      return this;\n    }\n\n    @Override\n    public long position() throws IOException {\n      return this.position;\n    }\n\n    @Override\n    public long size() throws IOException {\n      return size;\n    }\n\n    @Override\n    public SeekableByteChannel truncate(long size) throws IOException {\n      throw new UnsupportedOperationException(\n          \"GcsSeekableByteChannels are read-only and cannot be truncated.\");\n    }\n\n    @Override\n    public int write(ByteBuffer src) throws IOException {\n      throw new UnsupportedOperationException(\n          \"GcsSeekableByteChannel are read-only and does not support writing.\");\n    }\n\n    @Override\n    public boolean isOpen() {\n      return reader.isOpen();\n    }\n\n    @Override\n    public void close() throws IOException {\n      if (isOpen()) {\n        reader.close();","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtilV2.java#L521-L557","documentation":"GcsUtilV2's GcsSeekableByteChannel is a read-only channel over a GCS object; truncate(long) unconditionally throws UnsupportedOperationException, as does write(). The channel API contract includes truncate for read-write channels, so the read-only implementation must reject it.","triggerScenarios":"Calling SeekableByteChannel.truncate(size) on a channel obtained from gcsUtil.open (GCS read channel), directly or via file-manipulation utilities (e.g. Files.newByteChannel + truncate, or copy-with-replace helpers that assume writable channels).","commonSituations":"Passing a GCS-backed channel to generic java.nio file code that assumes local-file semantics; attempting in-place size adjustments of remote objects; test code reusing channel helpers written for local filesystems.","solutions":["Don't truncate GCS read channels; to 'shrink' an object, copy the desired byte range to a new object and delete/replace the original.","Check channel writability before truncate: use instanceof/capability checks or try-catch UnsupportedOperationException.","For local filesystem operations, use a local channel, not the GCS one.","If writable GCS channels are needed, use the write path (WriteChannel via create/update) rather than SeekableByteChannel.truncate."],"exampleFix":"// before\nchannel.truncate(newSize); // UnsupportedOperationException on GCS channel\n// after\nif (channel instanceof GcsUtilV2.GcsSeekableByteChannel) {\n  throw new IllegalStateException(\"GCS channels are read-only; copy range to new object instead\");\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isWritableChannel(SeekableByteChannel ch) {\n  return !(ch instanceof GcsUtilV2.GcsSeekableByteChannel);\n}","tryCatchPattern":"try {\n  channel.truncate(size);\n} catch (UnsupportedOperationException e) {\n  // GCS read channel: perform range-copy to a new object instead\n}","preventionTips":["Never pass GCS-backed channels to java.nio code that truncates or writes","Model GCS 'shrink' as range-copy to a new object plus delete","Use GCS write channels (create/update) for any write-like operation"],"tags":["gcs","unsupported-operation","nio","read-only","java","apache-beam"],"backgroundTag":"method-not-implemented","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}