1 17 18 package org.apache.james.mailrepository.filepair; 19 20 import java.io.BufferedOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import org.apache.avalon.cornerstone.services.store.StreamRepository; 27 import org.apache.avalon.excalibur.io.IOUtil; 28 29 35 public class File_Persistent_Stream_Repository 36 extends AbstractFileRepository 37 implements StreamRepository 38 { 39 protected final HashMap m_inputs = new HashMap (); 40 protected final HashMap m_outputs = new HashMap (); 41 42 protected String getExtensionDecorator() 43 { 44 return ".FileStreamStore"; 45 } 46 47 50 public synchronized InputStream get( final String key ) 51 { 52 try 53 { 54 final ResettableFileInputStream stream = 55 new ResettableFileInputStream( getFile( key ) ); 56 57 final Object o = m_inputs.get( key ); 58 if( null == o ) 59 { 60 m_inputs.put( key, stream ); 61 } 62 else if( o instanceof ArrayList ) 63 { 64 ( (ArrayList )o ).add( stream ); 65 } 66 else 67 { 68 final ArrayList list = new ArrayList (); 69 list.add( o ); 70 list.add( stream ); 71 m_inputs.put( key, list ); 72 } 73 74 return stream; 75 } 76 catch( final IOException ioe ) 77 { 78 final String message = "Exception caught while retrieving a stream "; 79 getLogger().warn( message, ioe ); 80 throw new RuntimeException ( message + ": " + ioe ); 81 } 82 } 83 84 87 public synchronized OutputStream put( final String key ) 88 { 89 try 90 { 91 final OutputStream outputStream = getOutputStream( key ); 92 final BufferedOutputStream stream = new BufferedOutputStream ( outputStream ); 93 94 final Object o = m_outputs.get( key ); 95 if( null == o ) 96 { 97 m_outputs.put( key, stream ); 98 } 99 else if( o instanceof ArrayList ) 100 { 101 ( (ArrayList )o ).add( stream ); 102 } 103 else 104 { 105 final ArrayList list = new ArrayList (); 106 list.add( o ); 107 list.add( stream ); 108 m_outputs.put( key, list ); 109 } 110 111 return stream; 112 } 113 catch( final IOException ioe ) 114 { 115 final String message = "Exception caught while storing a stream "; 116 getLogger().warn( message, ioe ); 117 throw new RuntimeException ( message + ": " + ioe ); 118 } 119 } 120 121 public synchronized void remove( final String key ) 122 { 123 Object o = m_inputs.remove( key ); 124 if( null != o ) 125 { 126 if( o instanceof InputStream ) 127 { 128 IOUtil.shutdownStream( (InputStream )o ); 129 } 130 else 131 { 132 final ArrayList list = (ArrayList )o; 133 final int size = list.size(); 134 135 for( int i = 0; i < size; i++ ) 136 { 137 IOUtil.shutdownStream( (InputStream )list.get( i ) ); 138 } 139 } 140 } 141 142 o = m_outputs.remove( key ); 143 if( null != o ) 144 { 145 if( o instanceof OutputStream ) 146 { 147 IOUtil.shutdownStream( (OutputStream )o ); 148 } 149 else 150 { 151 final ArrayList list = (ArrayList )o; 152 final int size = list.size(); 153 154 for( int i = 0; i < size; i++ ) 155 { 156 IOUtil.shutdownStream( (OutputStream )list.get( 0 ) ); 157 } 158 } 159 } 160 161 super.remove( key ); 162 } 163 164 public long getSize(final String key) { 165 try { 166 return getFile(key).length(); 167 } 168 catch(IOException e) { 169 return 0; 170 } 171 } 172 } 173 | Popular Tags |