comparison src/main/java/nl/cwi/monetdb/util/Extract.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children 57978db4ee57 b9b35ca2eec2
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
7 */
8
9 package nl.cwi.monetdb.util;
10
11 import java.io.BufferedReader;
12 import java.io.FileNotFoundException;
13 import java.io.FileWriter;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17
18
19 /**
20 * This file contains a function to extract files from its including Jar
21 * package.
22 *
23 * @author Ying Zhang <Y.Zhang@cwi.nl>
24 * @version 0.1
25 */
26
27 public class Extract {
28 private static final int DEFAULT_BUFSIZE = 16386;
29
30 public Extract() {}
31
32 /**
33 * Extracts a file from the Jar package which includes this class to
34 * the given destination
35 * @param fromFile The file to extract, including it absolute path
36 * inside its including Jar package.
37 * @param toFile Destination for the extracted file
38 * @throws FileNotFoundException If the file to extract can not be
39 * found in its including Jar package.
40 * @throws IOException If any error happens during
41 * creating/reading/writing files.
42 */
43 public static void extractFile(String fromFile, String toFile)
44 throws FileNotFoundException, IOException
45 {
46 char[] cbuf = new char[DEFAULT_BUFSIZE];
47 int ret = 0;
48
49 InputStream is = new Extract().getClass().getResourceAsStream(fromFile);
50
51 if(is == null) {
52 throw new FileNotFoundException("File " + fromFile +
53 " does not exist in the JAR package.");
54 }
55
56 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
57 FileWriter writer = new FileWriter(toFile, false);
58
59 ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE);
60 while(ret > 0){
61 writer.write(cbuf, 0, ret);
62 ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE);
63 }
64 reader.close();
65 writer.close();
66 }
67 }