bug6798062.java revision 2362
0N/A/*
2121N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/* @test %W% %E%
1879N/A @bug 6798062
1879N/A @summary Memory Leak on using getFiles of FileSystemView
1879N/A @author Pavel Porvatov
1879N/A @run applet/manual=done bug6798062.html
1879N/A*/
1879N/A
1879N/Aimport sun.awt.shell.ShellFolder;
1879N/A
1879N/Aimport javax.swing.*;
1879N/Aimport java.awt.event.ActionEvent;
1879N/Aimport java.awt.event.ActionListener;
0N/Aimport java.awt.*;
0N/Aimport java.io.File;
0N/Aimport java.io.FileNotFoundException;
0N/A
0N/Apublic class bug6798062 extends JApplet {
0N/A
0N/A private final JSlider slider = new JSlider(0, 100);
0N/A
0N/A private final JTextField tfLink = new JTextField();
0N/A
0N/A private final JButton btnStart = new JButton("Start");
0N/A
0N/A private final JButton btnStop = new JButton("Stop");
0N/A
0N/A private final JButton btnGC = new JButton("Run System.gc()");
0N/A
0N/A private ShellFolder folder;
77N/A
0N/A private Thread thread;
0N/A
0N/A public static void main(String[] args) {
0N/A JFrame frame = new JFrame("bug6798062");
0N/A
0N/A frame.setSize(400, 300);
77N/A frame.setLocationRelativeTo(null);
0N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65N/A frame.add(new bug6798062().initialize());
65N/A
65N/A frame.setVisible(true);
0N/A }
0N/A
77N/A public void init() {
0N/A add(initialize());
0N/A }
0N/A
0N/A private JPanel initialize() {
0N/A File file = new File("c:/");
0N/A
253N/A try {
0N/A folder = ShellFolder.getShellFolder(file);
253N/A } catch (FileNotFoundException e) {
253N/A fail("Directory " + file.getPath() + " not found");
253N/A }
253N/A
253N/A slider.setMajorTickSpacing(10);
253N/A slider.setPaintTicks(true);
0N/A slider.setPaintLabels(true);
0N/A slider.setSnapToTicks(true);
0N/A slider.setValue(10);
0N/A
0N/A btnStart.addActionListener(new ActionListener() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A setEnabledState(false);
0N/A
0N/A thread = new MyThread(slider.getValue(), tfLink.getText());
0N/A thread.start();
0N/A }
1554N/A });
244N/A
244N/A btnStop.setEnabled(false);
2121N/A
2121N/A btnStop.addActionListener(new ActionListener() {
2121N/A public void actionPerformed(ActionEvent e) {
244N/A thread.interrupt();
1841N/A thread = null;
244N/A
1554N/A setEnabledState(true);
244N/A }
244N/A });
253N/A
253N/A btnGC.addActionListener(new ActionListener() {
253N/A public void actionPerformed(ActionEvent e) {
253N/A System.gc();
253N/A }
253N/A });
2884N/A
253N/A setEnabledState(true);
253N/A
253N/A JPanel pnButtons = new JPanel();
253N/A
253N/A pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS));
2884N/A
253N/A pnButtons.add(btnStart);
2884N/A pnButtons.add(btnStop);
2884N/A pnButtons.add(btnGC);
253N/A
2956N/A tfLink.setMaximumSize(new Dimension(300, 20));
2956N/A
0N/A JPanel pnContent = new JPanel();
0N/A
0N/A pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));
0N/A pnContent.add(new JLabel("Delay between listFiles() invocation (ms):"));
0N/A pnContent.add(slider);
0N/A pnContent.add(new JLabel("Provide link here:"));
0N/A pnContent.add(tfLink);
0N/A pnContent.add(pnButtons);
0N/A
1841N/A return pnContent;
0N/A }
0N/A
0N/A private void setEnabledState(boolean enabled) {
0N/A slider.setEnabled(enabled);
0N/A btnStart.setEnabled(enabled);
0N/A btnStop.setEnabled(!enabled);
0N/A }
0N/A
0N/A private static void fail(String msg) {
0N/A throw new RuntimeException(msg);
0N/A }
0N/A
1841N/A private class MyThread extends Thread {
0N/A private final int delay;
0N/A
65N/A private final ShellFolder link;
65N/A
65N/A private MyThread(int delay, String link) {
65N/A this.delay = delay;
65N/A
65N/A ShellFolder linkFolder;
65N/A
65N/A try {
65N/A linkFolder = ShellFolder.getShellFolder(new File(link));
65N/A } catch (FileNotFoundException e) {
65N/A e.printStackTrace();
65N/A
65N/A linkFolder = null;
65N/A }
0N/A
0N/A this.link = linkFolder;
0N/A }
0N/A
0N/A public void run() {
0N/A while (!isInterrupted()) {
0N/A folder.listFiles();
0N/A if (link != null) {
0N/A try {
0N/A link.getLinkLocation();
0N/A } catch (FileNotFoundException e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
1841N/A
0N/A if (delay > 0) {
0N/A try {
0N/A Thread.sleep(delay);
2884N/A } catch (InterruptedException e1) {
2884N/A // The thread was interrupted
2884N/A return;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
65N/A