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.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.io.IOUtils;
25  
26  /**
27   * Functions dealing with {@link File}s and Streams.
28   */
29  public final class FileFunction
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    // ****************************** Initializer *******************************
38  
39    // ****************************** Constructors ******************************
40  
41    /**
42     * Utility class.
43     */
44    private FileFunction()
45    {
46    }
47  
48    // ****************************** Inner Classes *****************************
49  
50    // ********************************* Methods ********************************
51  
52    // --- init -----------------------------------------------------------------
53  
54    // --- get&set --------------------------------------------------------------
55  
56    // --- business -------------------------------------------------------------
57  
58    /**
59     * Writes the given stream to the given file.
60     *
61     * @param input the stream to read from.
62     * @param file the file to write to.
63     * @throws IOException on any problem copying the stream contents to the file.
64     */
65    public static void writeToFile(final InputStream input, final File file)
66      throws IOException
67    {
68      final OutputStream output = FileUtils.openOutputStream(file);
69      try
70      {
71        IOUtils.copy(input, output);
72      }
73      finally
74      {
75        IOUtils.closeQuietly(output);
76      }
77    }
78  
79    /**
80     * Checks if a directory with the given name exits and if not, creates it.
81     *
82     * @param directoryName the name of the directory to ensure to exist.
83     * @return a reference to the created file.
84     * @throws IOException if the directory cannot be created.
85     */
86    public static File provideDirectory(final String directoryName)
87      throws IOException
88    {
89      final File directory = new File(directoryName);
90      provideDirectory(directory);
91      return directory;
92    }
93  
94    /**
95     * Checks if the given directory exits and if not, creates it.
96     *
97     * @param directory the directory to ensure to exist.
98     * @throws IOException if the directory cannot be created.
99     */
100   public static void provideDirectory(final File directory) throws IOException
101   {
102     if (!directory.exists())
103     {
104       final boolean created = directory.mkdirs();
105       if (!created)
106       {
107         throw new IOException("Cannot create directory '"
108                               + directory.getAbsolutePath() + "'.");
109       }
110     }
111   }
112 
113   // --- object basics --------------------------------------------------------
114 
115 }