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