View Javadoc

1   /*
2    * Copyright 2011-2013 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.Arg;
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      Arg.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    @Override
74    public InputStream openToRead(final String resourceId) throws IOException
75    {
76      final File resource = new File(rootDir, resourceId);
77      final InputStream in = new FileInputStream(resource);
78      final BufferedInputStream bin = new BufferedInputStream(in);
79      return bin;
80    }
81  
82    /**
83     * {@inheritDoc}
84     */
85    @Override
86    public OutputStream openToWrite(final String resourceId) throws IOException
87    {
88      final File resource = new File(rootDir, resourceId);
89      final OutputStream out = new FileOutputStream(resource);
90      final BufferedOutputStream bin = new BufferedOutputStream(out);
91      return bin;
92    }
93  
94    // --- object basics --------------------------------------------------------
95  
96  }