All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.jsftemplating.util.CachedURLConnection Maven / Gradle / Ivy

/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://jsftemplating.dev.java.net/cddl1.html or
 * jsftemplating/cddl1.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at jsftemplating/cddl1.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
 */
package com.sun.jsftemplating.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLConnection;


/**
 *  

This class enables a URL connection to a cached object of type * <T>. It is required that a * WeakReference to this object by used, allowing GC to * occur if no other Strong references are present.

* * @author Ken Paulsen ([email protected]) */ public class CachedURLConnection extends URLConnection { /** *

This constructor requires the WeakReference<T> * containing the object to be supplied, in addition to the URL.

*/ public CachedURLConnection(URL url, WeakReference weakRef) { super(url); if ((weakRef == null) || (weakRef.get() == null)) { throw new IllegalArgumentException("The weakRef is required in " + "order to create a CachedURLConnection!"); } this.weakRef = weakRef; } /** *

This method is overriden to provide access to an InputStream based * on the T object.

*/ @Override public InputStream getInputStream() throws IOException { try { // If the value is (null) a NPE will be thrown here... only // should occur if the object has been GC'd. byte bytes[] = null; T obj = weakRef.get(); if (obj instanceof byte[]) { bytes = (byte[]) obj; } else { bytes = obj.toString().getBytes(); } return new ByteArrayInputStream(bytes); } catch (NullPointerException ex) { throw new IOException("Cached object was null!"); } } /** *

This method is required to be overriden, however, no "connection" * is needed, so this method does nothing.

*/ @Override public void connect() throws IOException { // Do nothing. } private WeakReference weakRef = null; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy