11/*
2- * Copyright (c) 2016, 2025 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2016, 2026 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2929import java .io .EOFException ;
3030import java .io .File ;
3131import java .io .IOException ;
32+ import java .nio .file .Files ;
3233import java .nio .file .NoSuchFileException ;
3334import java .nio .file .Path ;
3435import java .util .ArrayList ;
@@ -221,9 +222,10 @@ public void close() throws IOException {
221222 *
222223 * @param filter filter that determines if an event should be included, not
223224 * {@code null}
224- * @throws IOException if an I/O error occurred, it's not a Flight
225- * Recorder file or a version of a JFR file that can't
226- * be parsed
225+ * @throws IOException if an I/O error occurred, if {@code destination} is the
226+ * same file as the input file for this
227+ * {@code RecordingFile}, if it's not a Flight Recorder file
228+ * or a version of a JFR file that can't be parsed
227229 *
228230 * @since 19
229231 */
@@ -235,6 +237,9 @@ public void write(Path destination, Predicate<RecordedEvent> filter) throws IOEx
235237
236238 // package private
237239 List <RemovedEvents > write (Path destination , Predicate <RecordedEvent > filter , boolean collectResults ) throws IOException {
240+ if (Files .exists (destination ) && Files .isSameFile (destination , file .toPath ())) {
241+ throw new IOException ("Destination file can't be the same as the input file: " + destination .toAbsolutePath ());
242+ }
238243 try (ChunkWriter cw = new ChunkWriter (file .toPath (), destination , filter , collectResults )) {
239244 try (RecordingFile rf = new RecordingFile (cw )) {
240245 while (rf .hasMoreEvents ()) {
0 commit comments