From 58906bf8fbbf4417149e796325bcbaec189d538b Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Thu, 3 Aug 2023 07:23:19 +0000 Subject: [PATCH] 4893524: Swing drop targets should call close() on transferred readers and streams Reviewed-by: serb, tr, aivanov --- .../javax/swing/plaf/basic/BasicTextUI.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java index c105a74cfcb..ec0fbaf9928 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -2598,18 +2598,21 @@ public abstract class BasicTextUI extends TextUI implements ViewFactory { if (ic != null) { ic.endComposition(); } - Reader r = importFlavor.getReaderForText(t); - if (modeBetween) { - Caret caret = c.getCaret(); - if (caret instanceof DefaultCaret) { - ((DefaultCaret)caret).setDot(pos, dropBias); - } else { - c.setCaretPosition(pos); + // Use try-with-resource logic to close stream after use + try (Reader r = importFlavor.getReaderForText(t)) { + + if (modeBetween) { + Caret caret = c.getCaret(); + if (caret instanceof DefaultCaret) { + ((DefaultCaret) caret).setDot(pos, dropBias); + } else { + c.setCaretPosition(pos); + } } - } - handleReaderImport(r, c, useRead); + handleReaderImport(r, c, useRead); + } if (isDrop) { c.requestFocus();