src/java.desktop/share/classes/sun/applet/AppletImageRef.java
branchJDK-8145252-TLS13-branch
changeset 56755 9e1b125a3f75
parent 56754 d0728b0f98f9
parent 50542 b6ff4cd463e3
child 56757 01f1611dba52
equal deleted inserted replaced
56754:d0728b0f98f9 56755:9e1b125a3f75
     1 /*
       
     2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.applet;
       
    27 
       
    28 import java.awt.Toolkit;
       
    29 import java.awt.Image;
       
    30 import java.lang.ref.SoftReference;
       
    31 import sun.awt.image.URLImageSource;
       
    32 import java.net.URL;
       
    33 
       
    34 @Deprecated(since = "9")
       
    35 class AppletImageRef {
       
    36     private SoftReference<Image> soft = null;
       
    37 
       
    38     URL url;
       
    39 
       
    40     /**
       
    41      * Returns a pointer to the object referenced by this Ref.  If the object
       
    42      * has been thrown away by the garbage collector, it will be
       
    43      * reconstituted. This method does everything necessary to ensure that the garbage
       
    44      * collector throws things away in Least Recently Used(LRU) order.  Applications should
       
    45      * never override this method. The get() method effectively caches calls to
       
    46      * reconstitute().
       
    47      */
       
    48     public synchronized Image get() {
       
    49         Image t = check();
       
    50         if (t == null) {
       
    51             t = reconstitute();
       
    52             setThing(t);
       
    53         }
       
    54         return t;
       
    55     }
       
    56 
       
    57     /**
       
    58      * Create the Ref
       
    59      */
       
    60     AppletImageRef(URL url) {
       
    61         this.url = url;
       
    62     }
       
    63 
       
    64     /**
       
    65      * Flushes the cached object.  Forces the next invocation of get() to
       
    66      * invoke reconstitute().
       
    67      */
       
    68     public synchronized void flush() {
       
    69         SoftReference<Image> s = soft;
       
    70         if (s != null) s.clear();
       
    71         soft = null;
       
    72     }
       
    73 
       
    74     /**
       
    75      * Sets the thing to the specified object.
       
    76      * @param thing the specified object
       
    77      */
       
    78     public synchronized void setThing(Image thing) {
       
    79         flush();
       
    80         soft = new SoftReference<>(thing);
       
    81     }
       
    82 
       
    83     /**
       
    84      * Checks to see what object is being pointed at by this Ref and returns it.
       
    85      */
       
    86     public synchronized Image check() {
       
    87         SoftReference<Image> s = soft;
       
    88         if (s == null) return null;
       
    89         return s.get();
       
    90     }
       
    91 
       
    92     /**
       
    93      * Reconsitute the image.  Only called when the ref has been flushed.
       
    94      */
       
    95     public Image reconstitute() {
       
    96         Image img = Toolkit.getDefaultToolkit().createImage(new URLImageSource(url));
       
    97         return img;
       
    98     }
       
    99 }