1 /*
2 * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.util.io;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
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
27 import de.smartics.util.lang.Arguments;
28
29 /**
30 * Handles resources stored in a directory.
31 */
32 public final class DirectoryStreamHandler implements StreamHandler
33 {
34 // ********************************* Fields *********************************
35
36 // --- constants ------------------------------------------------------------
37
38 // --- members --------------------------------------------------------------
39
40 /**
41 * The root directory within which the resources are stored.
42 */
43 private final File rootDir;
44
45 // ****************************** Initializer *******************************
46
47 // ****************************** Constructors ******************************
48
49 /**
50 * Default constructor.
51 *
52 * @param rootDir the root directory within which the resources are stored.
53 */
54 public DirectoryStreamHandler(final File rootDir)
55 {
56 Arguments.checkNotNull("rootDir", rootDir);
57 this.rootDir = rootDir;
58 }
59
60 // ****************************** Inner Classes *****************************
61
62 // ********************************* Methods ********************************
63
64 // --- init -----------------------------------------------------------------
65
66 // --- get&set --------------------------------------------------------------
67
68 // --- business -------------------------------------------------------------
69
70 /**
71 * {@inheritDoc}
72 *
73 * @see de.smartics.util.io.StreamHandler#open(java.lang.String)
74 */
75 @Override
76 public InputStream openToRead(final String resourceId) throws IOException
77 {
78 final File resource = new File(rootDir, resourceId);
79 final InputStream in = new FileInputStream(resource);
80 final BufferedInputStream bin = new BufferedInputStream(in);
81 return bin;
82 }
83
84 /**
85 * {@inheritDoc}
86 *
87 * @see de.smartics.util.io.StreamHandler#openToWrite(java.lang.String)
88 */
89 @Override
90 public OutputStream openToWrite(final String resourceId) throws IOException
91 {
92 final File resource = new File(rootDir, resourceId);
93 final OutputStream out = new FileOutputStream(resource);
94 final BufferedOutputStream bin = new BufferedOutputStream(out);
95 return bin;
96 }
97
98 // --- object basics --------------------------------------------------------
99
100 }