001 package com.croftsoft.core.util.cache; 002 003 import java.io.*; 004 005 import com.croftsoft.core.util.id.Id; 006 import com.croftsoft.core.util.id.IntId; 007 008 /********************************************************************* 009 * Static methods to support Cache implementations. 010 * 011 * @see 012 * Cache 013 * 014 * @version 015 * 1999-04-20 016 * @author 017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 018 *********************************************************************/ 019 020 public class CacheLib 021 ////////////////////////////////////////////////////////////////////// 022 ////////////////////////////////////////////////////////////////////// 023 { 024 025 private CacheLib ( ) { } 026 027 ////////////////////////////////////////////////////////////////////// 028 // Test methods 029 ////////////////////////////////////////////////////////////////////// 030 031 /********************************************************************* 032 * Tests Cache implementations. 033 * 034 * <PRE> 035 * 036 * if ( args.length < 1 ) 037 * { 038 * System.out.println ( test ( ) ); 039 * } 040 * else 041 * { 042 * System.out.println ( test ( args ) ); 043 * } 044 * 045 * </PRE> 046 * 047 * @param args 048 * Names of Cache classes. 049 *********************************************************************/ 050 /* 051 public static void main ( String [ ] args ) 052 ////////////////////////////////////////////////////////////////////// 053 { 054 if ( args.length < 1 ) 055 { 056 System.out.println ( test ( ) ); 057 } 058 else 059 { 060 System.out.println ( test ( args ) ); 061 } 062 } 063 */ 064 065 /********************************************************************* 066 * Tests the Cache implementations provided in this package. 067 *********************************************************************/ 068 /* 069 public static boolean test ( ) 070 ////////////////////////////////////////////////////////////////////// 071 { 072 try 073 { 074 return test ( new SoftCache ( ) ) 075 && test ( new WeakCache ( ) ); 076 } 077 catch ( Throwable t ) 078 { 079 t.printStackTrace ( ); 080 return false; 081 } 082 } 083 */ 084 085 /********************************************************************* 086 * Tests Cache implementations. 087 * 088 * @param cacheClassNames 089 * Names of Cache classes. 090 *********************************************************************/ 091 /* 092 public static boolean test ( String [ ] cacheClassNames ) 093 ////////////////////////////////////////////////////////////////////// 094 { 095 try 096 { 097 boolean result = true; 098 099 for ( int i = 0; i < cacheClassNames.length; i++ ) 100 { 101 Class c = Class.forName ( cacheClassNames [ i ] ); 102 Cache cache = ( Cache ) c.newInstance ( ); 103 result = test ( cache ) && result; 104 if ( !result ) break; 105 } 106 107 return result; 108 } 109 catch ( Throwable t ) 110 { 111 t.printStackTrace ( ); 112 return false; 113 } 114 } 115 */ 116 117 /********************************************************************* 118 * Tests a Cache implementation. 119 * 120 * @param cache 121 * An instance of a Cache to be tested. 122 *********************************************************************/ 123 /* 124 public static boolean test ( Cache cache ) 125 ////////////////////////////////////////////////////////////////////// 126 { 127 try 128 { 129 Runtime runtime = Runtime.getRuntime ( ); 130 131 Id id0 = null; 132 133 for ( int index = 0; index < 1000; index++ ) 134 { 135 // System.gc ( ); 136 137 if ( index % 10 == 0 ) 138 { 139 System.out.println ( 140 index + "-: " 141 + runtime.freeMemory ( ) 142 + " / " 143 + runtime.totalMemory ( ) ); 144 } 145 146 String s = Integer.toString ( index ); 147 Id id = new IntegerId ( index ); 148 id = ( Id ) storeString ( cache, s, id ).clone ( ); 149 150 if ( index == 0 ) 151 { 152 id0 = id; 153 } 154 155 if ( index % 10 == 0 ) 156 { 157 System.out.println ( 158 index + "+: " 159 + runtime.freeMemory ( ) 160 + " / " 161 + runtime.totalMemory ( ) 162 + " " 163 + retrieveString ( cache, id0 ) ); 164 } 165 166 if ( !s.equals ( retrieveString ( cache, id ) ) ) 167 { 168 return false; 169 } 170 } 171 172 return true; 173 } 174 catch ( Throwable t ) 175 { 176 t.printStackTrace ( ); 177 return false; 178 } 179 } 180 */ 181 182 ////////////////////////////////////////////////////////////////////// 183 ////////////////////////////////////////////////////////////////////// 184 185 public static Id storeString ( Cache cache, String s ) 186 throws IOException 187 ////////////////////////////////////////////////////////////////////// 188 { 189 return cache.store ( toInputStream ( s ) ); 190 } 191 192 public static String retrieveString ( Cache cache, Id id ) 193 throws IOException 194 ////////////////////////////////////////////////////////////////////// 195 { 196 return toString ( cache.retrieve ( id ) ); 197 } 198 199 ////////////////////////////////////////////////////////////////////// 200 ////////////////////////////////////////////////////////////////////// 201 202 public static byte [ ] toByteArray ( InputStream in ) 203 throws IOException 204 ////////////////////////////////////////////////////////////////////// 205 { 206 ByteArrayOutputStream out = null; 207 208 try 209 { 210 out = new ByteArrayOutputStream ( ); 211 212 int i; 213 while ( ( i = in.read ( ) ) > -1 ) out.write ( i ); 214 215 return out.toByteArray ( ); 216 } 217 finally 218 { 219 try { in.close ( ); } catch ( Exception ex ) { } 220 try { out.close ( ); } catch ( Exception ex ) { } 221 } 222 } 223 224 public static InputStream toInputStream ( String s ) 225 ////////////////////////////////////////////////////////////////////// 226 { 227 return toInputStream ( s.getBytes ( ) ); 228 } 229 230 public static InputStream toInputStream ( byte [ ] byteArray ) 231 ////////////////////////////////////////////////////////////////////// 232 { 233 return new ByteArrayInputStream ( byteArray ); 234 } 235 236 public static String toString ( InputStream in ) throws IOException 237 ////////////////////////////////////////////////////////////////////// 238 { 239 if ( in == null ) return null; 240 241 ByteArrayOutputStream out = new ByteArrayOutputStream ( ); 242 int i; 243 while ( ( i = in.read ( ) ) > -1 ) out.write ( i ); 244 245 return new String ( out.toByteArray ( ) ); 246 } 247 248 ////////////////////////////////////////////////////////////////////// 249 ////////////////////////////////////////////////////////////////////// 250 }