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