741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
55 |
|
1652 |
22 May 07 |
gregory |
56 |
package se.lu.thep.coreftpd.common; |
741 |
10 Oct 06 |
olle |
57 |
|
741 |
10 Oct 06 |
olle |
58 |
import java.io.BufferedInputStream; |
741 |
10 Oct 06 |
olle |
59 |
import java.io.BufferedOutputStream; |
741 |
10 Oct 06 |
olle |
60 |
import java.io.File; |
741 |
10 Oct 06 |
olle |
61 |
import java.io.FileInputStream; |
741 |
10 Oct 06 |
olle |
62 |
import java.io.FileOutputStream; |
741 |
10 Oct 06 |
olle |
63 |
|
741 |
10 Oct 06 |
olle |
64 |
/** |
1652 |
22 May 07 |
gregory |
65 |
* |
1652 |
22 May 07 |
gregory |
* <B>About this class:</B> <BR> |
741 |
10 Oct 06 |
olle |
* This is like <CODE>File</CODE> but with a new method called <CODE>copyFile</CODE>. |
1652 |
22 May 07 |
gregory |
68 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
71 |
*/ |
741 |
10 Oct 06 |
olle |
72 |
|
1223 |
18 Feb 07 |
gregory |
73 |
@SuppressWarnings("serial") |
1652 |
22 May 07 |
gregory |
74 |
final public class FileWithCopy extends File { |
1652 |
22 May 07 |
gregory |
75 |
final static private boolean b_showErrors = false; |
741 |
10 Oct 06 |
olle |
76 |
|
1652 |
22 May 07 |
gregory |
77 |
public FileWithCopy(File parent, String child) { |
1652 |
22 May 07 |
gregory |
78 |
super(parent, child); |
741 |
10 Oct 06 |
olle |
79 |
} |
741 |
10 Oct 06 |
olle |
80 |
|
1652 |
22 May 07 |
gregory |
81 |
public FileWithCopy(String pathname) { |
741 |
10 Oct 06 |
olle |
82 |
super(pathname); |
741 |
10 Oct 06 |
olle |
83 |
} |
741 |
10 Oct 06 |
olle |
84 |
|
1652 |
22 May 07 |
gregory |
85 |
public FileWithCopy(String parent, String child) { |
741 |
10 Oct 06 |
olle |
86 |
super(parent, child); |
741 |
10 Oct 06 |
olle |
87 |
} |
741 |
10 Oct 06 |
olle |
88 |
|
1652 |
22 May 07 |
gregory |
89 |
public boolean copyTo(String filename) { |
741 |
10 Oct 06 |
olle |
90 |
return copyFile(this, new File(filename)); |
741 |
10 Oct 06 |
olle |
91 |
} |
741 |
10 Oct 06 |
olle |
92 |
|
741 |
10 Oct 06 |
olle |
93 |
/** |
1652 |
22 May 07 |
gregory |
* Copy this file to a new location. <BR> |
1652 |
22 May 07 |
gregory |
* If destination file already exists, overwrite it... |
1652 |
22 May 07 |
gregory |
96 |
*/ |
1652 |
22 May 07 |
gregory |
97 |
static public boolean copyFile(File f_src, File f_dest) { |
1652 |
22 May 07 |
gregory |
98 |
BufferedOutputStream bos = null; |
1652 |
22 May 07 |
gregory |
99 |
BufferedInputStream bis = null; |
1652 |
22 May 07 |
gregory |
100 |
try { |
741 |
10 Oct 06 |
olle |
101 |
if (!f_dest.exists()) |
1652 |
22 May 07 |
gregory |
102 |
f_dest.createNewFile(); // We must first create the file before |
1652 |
22 May 07 |
gregory |
// we write to the input stream |
741 |
10 Oct 06 |
olle |
104 |
|
1652 |
22 May 07 |
gregory |
105 |
bis = new BufferedInputStream(new FileInputStream(f_src)); |
1652 |
22 May 07 |
gregory |
106 |
bos = new BufferedOutputStream(new FileOutputStream(f_dest)); |
741 |
10 Oct 06 |
olle |
107 |
|
1652 |
22 May 07 |
gregory |
108 |
StreamIO.writeStreamToStream(bis, bos); |
741 |
10 Oct 06 |
olle |
109 |
|
741 |
10 Oct 06 |
olle |
110 |
bos.flush(); |
741 |
10 Oct 06 |
olle |
111 |
bis.close(); |
741 |
10 Oct 06 |
olle |
112 |
bos.close(); |
1652 |
22 May 07 |
gregory |
113 |
bis = null; |
1652 |
22 May 07 |
gregory |
114 |
bos = null; |
741 |
10 Oct 06 |
olle |
115 |
|
741 |
10 Oct 06 |
olle |
116 |
return true; |
1652 |
22 May 07 |
gregory |
117 |
} catch (Exception e) { |
1652 |
22 May 07 |
gregory |
118 |
try // Make sure to close bis even if we get Exception... |
741 |
10 Oct 06 |
olle |
119 |
{ |
1652 |
22 May 07 |
gregory |
120 |
if (bis != null) |
741 |
10 Oct 06 |
olle |
121 |
bis.close(); |
1652 |
22 May 07 |
gregory |
122 |
} catch (Exception ex) { |
1652 |
22 May 07 |
gregory |
123 |
bis = null; |
741 |
10 Oct 06 |
olle |
124 |
} |
741 |
10 Oct 06 |
olle |
125 |
|
1652 |
22 May 07 |
gregory |
126 |
try // Make sure to close bos even if we get Exception... |
741 |
10 Oct 06 |
olle |
127 |
{ |
1652 |
22 May 07 |
gregory |
128 |
if (bos != null) |
741 |
10 Oct 06 |
olle |
129 |
bos.close(); |
1652 |
22 May 07 |
gregory |
130 |
} catch (Exception ex) { |
1652 |
22 May 07 |
gregory |
131 |
bos = null; |
741 |
10 Oct 06 |
olle |
132 |
} |
741 |
10 Oct 06 |
olle |
133 |
return false; |
741 |
10 Oct 06 |
olle |
134 |
} |
741 |
10 Oct 06 |
olle |
135 |
} |
741 |
10 Oct 06 |
olle |
136 |
} |