001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: // RAVE only file!
042: package org.netbeans.modules.visualweb.extension.openide.io;
043:
044: import java.io.BufferedInputStream;
045: import java.io.BufferedOutputStream;
046: import java.io.File;
047: import java.io.FileInputStream;
048: import java.io.FileOutputStream;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.OutputStream;
052: import java.nio.channels.FileChannel;
053:
054: import java.util.Properties;
055: import java.util.regex.Matcher;
056: import java.util.regex.Pattern;
057:
058: // Copied from openide/io/src/org/openide/io where it may not be.
059: /**
060: * Class <code>FileCopy</code> is an utility class to be used
061: * whereever files need to be copied. There are a lot of different
062: * places where this happens and each place did it differently.
063: *
064: * Currently we have a problem with
065: * <code>java.nio.channels.FileChannel.transferTo</code> in the j2se
066: * 1.4.2 and the Linux kernel > 2.4.*. So creating the central class
067: * to do the copies seems like a good way to go.
068: *
069: * @author <a href="mailto:marco.walther@sun.com">Marco Walther</a>
070: * @version 1.0
071: */
072: public class RaveFileCopy {
073: private static boolean use_nio = true;
074:
075: /**
076: * Method <code>fileCopy</code> is the only usable method of this
077: * class.
078: *
079: * @param src a <code>File</code> value
080: * @param dest a <code>File</code> value
081: * @return a <code>boolean</code> value. This is <code>true</code>
082: * if the copy was successful or <code>false</code> if there was
083: * no copy.
084: * @exception IOException if an error occurs
085: */
086: public static boolean fileCopy(File src, File dest)
087: throws IOException {
088: if (!src.exists() || dest.exists()) {
089: return false;
090: } // end of if (!src.exists() || dest.exists())
091:
092: if (!dest.getParentFile().exists()) {
093: dest.getParentFile().mkdirs();
094: }
095:
096: dest.createNewFile();
097:
098: if (use_nio) {
099: FileChannel in = new FileInputStream(src).getChannel();
100: FileChannel out = new FileOutputStream(dest).getChannel();
101: in.transferTo(0, in.size(), out);
102: in.close();
103: out.close();
104: } // end of if (use_nio)
105: else {
106: int bufferSize = 64 * 1024;
107: int bytes;
108: byte[] buffer = new byte[bufferSize];
109:
110: InputStream is = new BufferedInputStream(
111: new FileInputStream(src), bufferSize);
112: OutputStream os = new BufferedOutputStream(
113: new FileOutputStream(dest), bufferSize);
114:
115: while ((bytes = is.read(buffer, 0, bufferSize)) > -1) {
116: os.write(buffer, 0, bytes);
117: } // end of while ((bytes = is.read(buffer, 0,
118: // bufferSize)) > -1)
119:
120: os.flush();
121: os.close();
122: is.close();
123: } // end of if (use_nio) else
124:
125: return true;
126: }
127:
128: static {
129: try {
130: if (System.getProperty("os.name"). // NOI18N
131: equalsIgnoreCase("Linux")) { // NOI18N
132:
133: Pattern p = Pattern.compile("(\\d)\\.(\\d+)\\..*"); // NOI18N
134: Matcher m = p.matcher(System.getProperty("os.version")); // NOI18N
135: if (m.matches()) {
136: if (Integer.parseInt(m.group(1)) == 2
137: && Integer.parseInt(m.group(2)) > 4) {
138: // We know about the problem with the
139: // Linux 2.6.* release kernels. But
140: // the change was probably introduced
141: // somewhen in the 2.5.* development
142: // kernels.
143: use_nio = false;
144: } // end of if (Integer.parseInt(m.group(1)) == 2 &&
145: // Integer.parseInt(m.group(2)) > 4)
146: } // end of if (m.matches())
147: } // end of if (System.getProperty("os.name").
148: // equalsIgnoreCase("Linux"))
149:
150: if (System.getProperty("openide.noniocopy") != null) { // NOI18N
151: use_nio = false;
152: } // end of if (System.getProperty("openide.noniocopy") !=
153: // null)
154:
155: if (System.getProperty("openide.niocopy") != null) { // NOI18N
156: use_nio = true;
157: } // end of if (System.getProperty("openide.niocopy") != null)
158: } catch (Exception e) {
159: use_nio = false;
160: } // end of try-catch
161: }
162: }
|