View Javadoc
1   /*
2    * This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
3    * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 John Currier
4    *
5    * SchemaSpy is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * SchemaSpy is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sourceforge.schemaspy.util;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  public class ResourceWriter {
28      private static ResourceWriter instance = new ResourceWriter();
29  
30      protected ResourceWriter() {
31      }
32  
33      public static ResourceWriter getInstance() {
34          return instance;
35      }
36  
37      /**
38       * Write the specified resource to the specified filename
39       *
40       * @param resourceName
41       * @param writeTo
42       * @throws IOException
43       */
44      public void writeResource(String resourceName, File writeTo) throws IOException {
45          writeTo.getParentFile().mkdirs();
46          InputStream in = getClass().getResourceAsStream(resourceName);
47          if (in == null)
48              throw new IOException("Resource \"" + resourceName + "\" not found");
49  
50          byte[] buf = new byte[4096];
51  
52          OutputStream out = new FileOutputStream(writeTo);
53          int numBytes = 0;
54          while ((numBytes = in.read(buf)) != -1) {
55              out.write(buf, 0, numBytes);
56          }
57          in.close();
58          out.close();
59      }
60  }