001         package com.croftsoft.core.util.filemap;
002    
003         import java.io.*;
004         import java.util.*;
005    
006         /*********************************************************************
007         * A Map that stores the values in files within a directory.
008         *
009         * Uses filenames as the keys and InputStreams as the values.
010         *
011         * @see
012         *   FilenameKeyGenerator
013         * @see
014         *   java.util.AbstractMap
015         *
016         * @version
017         *   1999-04-03
018         * @author
019         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020         *********************************************************************/
021    
022         public class  FileMap extends AbstractMap
023         //////////////////////////////////////////////////////////////////////
024         //////////////////////////////////////////////////////////////////////
025         {
026    
027         private File  rootDirectory;
028    
029         //////////////////////////////////////////////////////////////////////
030         //////////////////////////////////////////////////////////////////////
031    
032         public static void  main ( String [ ]  args )
033         //////////////////////////////////////////////////////////////////////
034         {
035           System.out.println ( test ( ) );
036         }
037    
038         public static boolean  test ( )
039         //////////////////////////////////////////////////////////////////////
040         {
041           try
042           {
043             String  filename = "com.orbs.util.filemap.FileMap_test.tmp";
044    
045             FileMap  fileMap = new FileMap ( new File ( "." ) );
046    
047             fileMap.put ( filename,
048               new ByteArrayInputStream ( "Hello".getBytes ( ) ) );
049    
050             InputStream  in = ( InputStream ) fileMap.get ( filename );
051    
052             int  i;
053             while ( ( i = in.read ( ) ) > -1 ) System.out.write ( i );
054    
055             in.close ( );
056    
057             System.out.println ( "" );
058    
059             fileMap.remove ( filename );
060           }
061           catch ( Exception  ex )
062           {
063             ex.printStackTrace ( );
064    
065             return false;
066           }
067    
068           return true;
069         }
070    
071         //////////////////////////////////////////////////////////////////////
072         //////////////////////////////////////////////////////////////////////
073    
074         public  FileMap ( File  rootDirectory )
075         //////////////////////////////////////////////////////////////////////
076         {
077           this.rootDirectory = rootDirectory;
078         }
079    
080         //////////////////////////////////////////////////////////////////////
081         //////////////////////////////////////////////////////////////////////
082    
083         public Set  entrySet ( )
084         //////////////////////////////////////////////////////////////////////
085         {
086           return new FileMapEntrySet ( rootDirectory );
087         }
088    
089    // synchronize?
090    
091         public synchronized Object  put ( Object  key, Object  value )
092         //////////////////////////////////////////////////////////////////////
093         {
094           File                  file = null;
095           BufferedOutputStream  out  = null;
096           InputStream           in   = null;
097    
098           try
099           {
100             String  filename = ( String ) key;
101    
102             file = new File ( rootDirectory, filename );
103    
104             out = new BufferedOutputStream ( new FileOutputStream ( file ) );
105    
106             in = ( InputStream ) value;
107    
108             int  i;
109             while ( ( i = in.read ( ) ) > -1 ) out.write ( i );
110           }
111           catch ( IOException  ioex )
112           {
113             try { out.close   ( ); } catch ( Exception  ex ) { }
114             try { file.delete ( ); } catch ( Exception  ex ) { }
115             throw new RuntimeException ( ioex.getMessage ( ) );
116           }
117           finally
118           {
119             try { in.close  ( ); } catch ( Exception  ex ) { }
120             try { out.close ( ); } catch ( Exception  ex ) { }
121           }
122    
123           return null;
124         }
125    
126         public synchronized Object  remove ( Object  key )
127         //////////////////////////////////////////////////////////////////////
128         {
129           String  filename = ( String ) key;
130    
131           File  file = new File ( rootDirectory, filename );
132    
133           file.delete ( );
134    
135           return null;
136         }
137    
138         //////////////////////////////////////////////////////////////////////
139         //////////////////////////////////////////////////////////////////////
140         }