001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.jci.readers;
019    
020    import java.util.Map;
021    import java.util.HashMap;
022    
023    /**
024     * A memory based reader to compile from memory
025     * 
026     * @author tcurdt
027     */
028    public class MemoryResourceReader implements ResourceReader {
029        
030        private Map<String, byte[]> resources = null;
031    
032        public boolean isAvailable( final String pResourceName ) {
033            if (resources == null) {
034                return false;
035            }
036    
037            return resources.containsKey(pResourceName);
038        }
039        
040        public void add( final String pResourceName, final byte[] pContent ) {
041            if (resources == null) {
042                resources = new HashMap<String, byte[]>();
043            }
044            
045            resources.put(pResourceName, pContent);
046        }
047        
048        public void remove( final String pResourceName ) {
049            if (resources != null) {
050                resources.remove(pResourceName);
051            }    
052        }    
053        
054    
055        public byte[] getBytes( final String pResourceName ) {
056            return resources.get(pResourceName);
057        }
058    
059        /**
060         * @deprecated
061         */
062        @Deprecated
063        public String[] list() {
064            if (resources == null) {
065                return new String[0];
066            }
067            return resources.keySet().toArray(new String[resources.size()]);
068        }
069    }