001 package com.croftsoft.core.applet;
002
003 import java.applet.*;
004 import java.io.*;
005
006 import com.croftsoft.core.io.SerializableLib;
007 import com.croftsoft.core.lang.NullArgumentException;
008
009 /*********************************************************************
010 * Static library methods for manipulating Applets.
011 *
012 * @version
013 * $Id: AppletLib.java,v 1.3 2008/09/28 21:50:42 croft Exp $
014 * @since
015 * 2002-12-22
016 * @author
017 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
018 *********************************************************************/
019
020 public final class AppletLib
021 //////////////////////////////////////////////////////////////////////
022 //////////////////////////////////////////////////////////////////////
023 {
024
025 /*********************************************************************
026 * Loads GZIP compressed data.
027 *********************************************************************/
028 public static Serializable loadSerializableUsingAppletPersistence (
029 Applet applet,
030 String key )
031 throws ClassNotFoundException, IOException,
032 UnsupportedOperationException
033 //////////////////////////////////////////////////////////////////////
034 {
035 NullArgumentException.check ( applet, "null applet" );
036
037 NullArgumentException.check ( key, "null key" );
038
039 AppletContext appletContext = null;
040
041 try
042 {
043 appletContext = applet.getAppletContext ( );
044 }
045 catch ( NullPointerException ex )
046 {
047 // ignore
048 }
049
050 if ( appletContext == null )
051 {
052 throw new UnsupportedOperationException ( "null AppletContext" );
053 }
054
055 InputStream inputStream = appletContext.getStream ( key );
056
057 if ( inputStream == null )
058 {
059 return null;
060 }
061
062 return SerializableLib.load ( inputStream );
063 }
064
065 /*********************************************************************
066 * Saves data using GZIP compression.
067 *********************************************************************/
068 public static void saveSerializableUsingAppletPersistence (
069 Applet applet,
070 String key,
071 Serializable serializable )
072 throws IOException, UnsupportedOperationException
073 //////////////////////////////////////////////////////////////////////
074 {
075 NullArgumentException.check ( applet, "null applet" );
076
077 NullArgumentException.check ( key, "null key" );
078
079 NullArgumentException.check ( serializable, "null serializable" );
080
081 AppletContext appletContext = null;
082
083 try
084 {
085 appletContext = applet.getAppletContext ( );
086 }
087 catch ( NullPointerException ex )
088 {
089 // ignore
090 }
091
092 if ( appletContext == null )
093 {
094 throw new UnsupportedOperationException ( "null AppletContext" );
095 }
096
097 InputStream inputStream = new ByteArrayInputStream (
098 SerializableLib.compress ( serializable ) );
099
100 appletContext.setStream ( key, inputStream );
101 }
102
103 //////////////////////////////////////////////////////////////////////
104 //////////////////////////////////////////////////////////////////////
105
106 private AppletLib ( ) { /* empty */ }
107
108 //////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////
110 }