    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.io.File;
    021    import java.util.ArrayList;
    022    import java.util.List;
    023
    024    import org.apache.commons.io.FileUtils;
    025
    026    /**
    027     * A simple file system based Reader implementation
    028     *
    029     * @author tcurdt
    030     */
    031    public final class FileResourceReader implements ResourceReader {
    032
    033        private final File root;
    034
    035        public FileResourceReader( final File pRoot ) {
    036            root = pRoot;
    037        }
    038
    039        public boolean isAvailable( final String pResourceName ) {
    040            return new File(root, pResourceName).exists();
    041        }
    042
    043        public byte[] getBytes( final String pResourceName ) {
    044            try {
    045                return FileUtils.readFileToString(new File(root, pResourceName), "UTF-8").getBytes();
    046            } catch(Exception e) {
    047                return null;
    048            }
    049        }
    050
    051        /**
    052         * @deprecated
    053         */
    054        @Deprecated
    055        public String[] list() {
    056            final List<String> files = new ArrayList<String>();
    057            list(root, files);
    058            return files.toArray(new String[files.size()]);
    059        }
    060
    061        /**
    062         * @deprecated
    063         */
    064        @Deprecated
    065        private void list( final File pFile, final List<String> pFiles ) {
    066            if (pFile.isDirectory()) {
    067                final File[] directoryFiles = pFile.listFiles();
    068                for (int i = 0; i < directoryFiles.length; i++) {
    069                    list(directoryFiles[i], pFiles);
    070                }
    071            } else {
    072                pFiles.add(pFile.getAbsolutePath().substring(root.getAbsolutePath().length()+1));
    073            }
    074        }
    075    }