View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.stores;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.commons.io.IOUtils;
30  
31  
32  /**
33   * Stores the results on disk
34   * 
35   * @author tcurdt
36   */
37  public final class FileResourceStore implements ResourceStore {
38  
39      private final File root;
40  
41      public FileResourceStore( final File pFile ) {
42          root = pFile;
43      }
44      
45      public byte[] read( final String pResourceName ) {
46          InputStream is = null;
47          try {
48              is = new FileInputStream(getFile(pResourceName));
49              final byte[] data = IOUtils.toByteArray(is);
50              return data;
51          } catch (Exception e) {
52              return null;
53          } finally {
54              IOUtils.closeQuietly(is);
55          }
56      }
57      
58      public void write( final String pResourceName, final byte[] pData ) {
59          OutputStream os = null;
60          try {
61              final File file = getFile(pResourceName);
62              final File parent = file.getParentFile();
63              if (!parent.exists()) {
64                  if (!parent.mkdirs()) {
65                      throw new IOException("could not create" + parent);
66                  }
67              }
68              os = new FileOutputStream(file);
69              os.write(pData);
70          } catch (Exception e) {
71              // FIXME: now what?
72          } finally {
73              IOUtils.closeQuietly(os);
74          }
75      }
76  
77      public void remove( final String pResourceName ) {
78          getFile(pResourceName).delete();
79      }
80  
81      private File getFile( final String pResourceName ) {
82          final String fileName = pResourceName.replace('/', File.separatorChar);
83          return new File(root, fileName);
84      }
85  
86      /**
87       * @deprecated
88       */
89      public String[] list() {
90          final List files = new ArrayList();
91          list(root, files);
92          return (String[]) files.toArray(new String[files.size()]);
93      }
94  
95      /**
96       * @deprecated
97       */
98      private void list(final File pFile, final List pFiles) {
99          if (pFile.isDirectory()) {
100             final File[] directoryFiles = pFile.listFiles();
101             for (int i=0; i < directoryFiles.length; i++) {
102                 list(directoryFiles[i], pFiles);
103             }
104         } else {
105             pFiles.add(pFile.getAbsolutePath().substring(root.getAbsolutePath().length()+1));
106         }
107     }
108     
109     public String toString() {
110         return this.getClass().getName() + root.toString();
111     }
112 
113 }