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.stores; 019 020 import java.io.File; 021 import java.io.FileInputStream; 022 import java.io.FileOutputStream; 023 import java.io.IOException; 024 import java.io.InputStream; 025 import java.io.OutputStream; 026 import java.util.ArrayList; 027 import java.util.List; 028 029 import org.apache.commons.io.IOUtils; 030 031 032 /** 033 * Stores the results on disk 034 * 035 * @author tcurdt 036 */ 037 public final class FileResourceStore implements ResourceStore { 038 039 private final File root; 040 041 public FileResourceStore( final File pFile ) { 042 root = pFile; 043 } 044 045 public byte[] read( final String pResourceName ) { 046 InputStream is = null; 047 try { 048 is = new FileInputStream(getFile(pResourceName)); 049 final byte[] data = IOUtils.toByteArray(is); 050 return data; 051 } catch (Exception e) { 052 return null; 053 } finally { 054 IOUtils.closeQuietly(is); 055 } 056 } 057 058 public void write( final String pResourceName, final byte[] pData ) { 059 OutputStream os = null; 060 try { 061 final File file = getFile(pResourceName); 062 final File parent = file.getParentFile(); 063 if (!parent.mkdirs() && !parent.isDirectory()) { 064 throw new IOException("could not create" + parent); 065 } 066 os = new FileOutputStream(file); 067 os.write(pData); 068 } catch (Exception e) { 069 // FIXME: now what? 070 } finally { 071 IOUtils.closeQuietly(os); 072 } 073 } 074 075 public void remove( final String pResourceName ) { 076 getFile(pResourceName).delete(); 077 } 078 079 private File getFile( final String pResourceName ) { 080 final String fileName = pResourceName.replace('/', File.separatorChar); 081 return new File(root, fileName); 082 } 083 084 /** 085 * @deprecated 086 */ 087 @Deprecated 088 public String[] list() { 089 final List<String> files = new ArrayList<String>(); 090 list(root, files); 091 return files.toArray(new String[files.size()]); 092 } 093 094 /** 095 * @deprecated 096 */ 097 @Deprecated 098 private void list(final File pFile, final List<String> pFiles) { 099 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 @Override 110 public String toString() { 111 return this.getClass().getName() + root.toString(); 112 } 113 114 }