|
From: <de...@us...> - 2010-09-05 22:02:29
|
Revision: 5912
http://fudaa.svn.sourceforge.net/fudaa/?rev=5912&view=rev
Author: deniger
Date: 2010-09-05 22:02:19 +0000 (Sun, 05 Sep 2010)
Log Message:
-----------
Modified Paths:
--------------
trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibArray.java
trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibFile.java
Modified: trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibArray.java
===================================================================
--- trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibArray.java 2010-09-05 22:01:16 UTC (rev 5911)
+++ trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibArray.java 2010-09-05 22:02:19 UTC (rev 5912)
@@ -371,6 +371,14 @@
}
return r;
}
+ public static double getSum(final double[] _d) {
+ if (_d == null || _d.length == 0) { return 0; }
+ double r = 0;
+ for (int i = _d.length - 1; i >= 0; i--) {
+ r += _d[i];
+ }
+ return r;
+ }
public static long getSum(final long[] _d) {
if (_d == null || _d.length == 0) { return 0; }
Modified: trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibFile.java
===================================================================
--- trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibFile.java 2010-09-05 22:01:16 UTC (rev 5911)
+++ trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluLibFile.java 2010-09-05 22:02:19 UTC (rev 5912)
@@ -1,8 +1,12 @@
/*
* @creation 2 ao\xFBt 2004
+ *
* @modification $Date: 2007-06-20 12:20:45 $
+ *
* @license GNU General Public License 2
+ *
* @copyright (c)1998-2001 CETMEF 2 bd Gambetta F-60231 Compiegne
+ *
* @mail de...@fu...
*/
package org.fudaa.ctulu;
@@ -19,7 +23,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
-import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
@@ -30,6 +33,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
+import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
@@ -66,20 +70,15 @@
* @return le fichier avec la bonne extension
*/
public static File changeExtension(final File _init, final String _newExt) {
- if (_init == null || _newExt == null) {
- return null;
- }
- if (_init.getParentFile() == null) {
- return new File(CtuluLibFile.getFileName(CtuluLibFile.getSansExtension(_init.getName()), _newExt));
- }
+ if (_init == null || _newExt == null) { return null; }
+ if (_init.getParentFile() == null) { return new File(CtuluLibFile.getFileName(CtuluLibFile.getSansExtension(_init
+ .getName()), _newExt)); }
return new File(_init.getParentFile(), CtuluLibFile.getFileName(CtuluLibFile.getSansExtension(_init.getName()),
_newExt));
}
public static String changeExtension(final String _fileName, final String _newExt) {
- if (_fileName == null || _newExt == null) {
- return null;
- }
+ if (_fileName == null || _newExt == null) { return null; }
return getFileName(getSansExtension(_fileName), _newExt);
}
@@ -124,9 +123,7 @@
* @return true si succes.
*/
public static boolean zip(final File[] _filesToZip, final File _zipFile) {
- if (_filesToZip == null || _zipFile == null || _filesToZip.length == 0 || canWrite(_zipFile) != null) {
- return false;
- }
+ if (_filesToZip == null || _zipFile == null || _filesToZip.length == 0 || canWrite(_zipFile) != null) { return false; }
// Create a buffer for reading the files
ZipOutputStream out = null;
boolean ok = true;
@@ -140,9 +137,7 @@
in = new FileInputStream(_filesToZip[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(_filesToZip[i].getName()));
- if (!copyStream(in, out, false, false)) {
- return false;
- }
+ if (!copyStream(in, out, false, false)) { return false; }
// Complete the entry
out.closeEntry();
} catch (final IOException e) {
@@ -180,9 +175,7 @@
}
public static boolean deleteDir(final File _f) {
- if (_f == null) {
- return false;
- }
+ if (_f == null) { return false; }
final File[] files = _f.listFiles();
if (files != null) {
for (int i = files.length - 1; i >= 0; i--) {
@@ -208,13 +201,9 @@
public static File createTempDir(final String _prefix, final File _dir) throws IOException {
final File tempFile = File.createTempFile(_prefix, CtuluLibString.EMPTY_STRING, _dir);
- if (!tempFile.delete()) {
- throw new IOException();
- }
+ if (!tempFile.delete()) { throw new IOException(); }
- if (!tempFile.mkdir()) {
- throw new IOException();
- }
+ if (!tempFile.mkdir()) { throw new IOException(); }
return tempFile;
}
@@ -224,9 +213,7 @@
* @return l'exception levee lors de la fermeture. ou null si aucune.
*/
public static IOException close(final Reader _r) {
- if (_r == null) {
- return null;
- }
+ if (_r == null) { return null; }
try {
_r.close();
} catch (final IOException e) {
@@ -236,9 +223,7 @@
}
public static IOException close(final OutputStream _r) {
- if (_r == null) {
- return null;
- }
+ if (_r == null) { return null; }
try {
_r.close();
} catch (final IOException e) {
@@ -248,9 +233,7 @@
}
public static IOException close(final InputStream _r) {
- if (_r == null) {
- return null;
- }
+ if (_r == null) { return null; }
try {
_r.close();
} catch (final IOException e) {
@@ -279,9 +262,7 @@
}
}
}
- if (exceptionLauch) {
- return envoie;
- }
+ if (exceptionLauch) { return envoie; }
return null;
}
@@ -290,9 +271,7 @@
* @return l'exception levee lors de la fermeture (ou null si aucune).
*/
public static IOException close(final Writer _r) {
- if (_r == null) {
- return null;
- }
+ if (_r == null) { return null; }
try {
_r.close();
} catch (final IOException e) {
@@ -513,9 +492,7 @@
* @return true si ok
*/
public boolean move(final File _from, final File _to) {
- if (_from == null || _to == null || !_from.exists() || canWrite(_to) != null) {
- return false;
- }
+ if (_from == null || _to == null || !_from.exists() || canWrite(_to) != null) { return false; }
final boolean ok = _from.renameTo(_to);
if (!ok) {
final boolean r = copyFile(_from, _to);
@@ -527,8 +504,6 @@
}
-
-
public static Charset getUTF8Charset() {
return Charset.forName("UTF-8");
}
@@ -541,13 +516,9 @@
* @param _path le path a convertir
*/
public static File getAbsolutePath(final File _baseDir, final String _path) {
- if ((_path == null) || (_baseDir == null)) {
- return null;
- }
+ if ((_path == null) || (_baseDir == null)) { return null; }
final File f = new File(_path);
- if (f.isAbsolute()) {
- return f;
- }
+ if (f.isAbsolute()) { return f; }
return CtuluLibFile.getConanicalPathFile(new File(_baseDir, _path));
}
@@ -560,9 +531,7 @@
*/
public static File getAbsolutePath(final String _baseDir, final String _path) {
final File f = new File(_path);
- if (f.isAbsolute()) {
- return f;
- }
+ if (f.isAbsolute()) { return f; }
return new File(_baseDir, _path);
}
@@ -642,16 +611,12 @@
return null;
} else if (_destFile.getParentFile().equals(_baseDir)) {
return _destFile.getName();
- } else if ((_baseDir == null) || (_destFile.getParentFile() == null)) {
- return _destFile.getAbsolutePath();
- }
+ } else if ((_baseDir == null) || (_destFile.getParentFile() == null)) { return _destFile.getAbsolutePath(); }
final String sFile = _destFile.getAbsolutePath();
String parentPath = _baseDir.getAbsolutePath();
if (sFile.startsWith(parentPath)) {
final String r = sFile.substring(parentPath.length());
- if (r.startsWith(File.separator)) {
- return r.substring(1);
- }
+ if (r.startsWith(File.separator)) { return r.substring(1); }
return r;
}
File parent = _baseDir.getParentFile();
@@ -747,13 +712,9 @@
* chaine intiale.
*/
public static String getSansExtension(final String _fName) {
- if (_fName == null) {
- return null;
- }
+ if (_fName == null) { return null; }
final int index = _fName.lastIndexOf('.');
- if (index < 0) {
- return _fName;
- }
+ if (index < 0) { return _fName; }
return _fName.substring(0, index);
}
@@ -766,13 +727,9 @@
* @return null si pas d'extension sinon l'extension sans point
*/
public static String getExtension(final String _fName) {
- if (_fName == null) {
- return null;
- }
+ if (_fName == null) { return null; }
final int index = _fName.lastIndexOf('.');
- if (index < 0 || index == _fName.length() - 1) {
- return null;
- }
+ if (index < 0 || index == _fName.length() - 1) { return null; }
return _fName.substring(index + 1);
}
@@ -800,12 +757,8 @@
* @return _f ou _f+'.ext'
*/
public static File appendExtensionIfNeeded(final File _f, final String _ext) {
- if (_f == null || _ext == null) {
- return null;
- }
- if (containsExtension(_f.getName())) {
- return _f;
- }
+ if (_f == null || _ext == null) { return null; }
+ if (containsExtension(_f.getName())) { return _f; }
return new File(_f.getAbsolutePath() + getDot() + _ext);
}
@@ -815,13 +768,9 @@
* @return null si _f ou _ext est null. Le fichier finissant par .ext
*/
public static File appendStrictExtensionIfNeeded(final File _f, final String _ext) {
- if (_f == null || _ext == null) {
- return null;
- }
+ if (_f == null || _ext == null) { return null; }
final String ext = getDot() + _ext;
- if (_f.getName().endsWith(ext)) {
- return _f;
- }
+ if (_f.getName().endsWith(ext)) { return _f; }
return new File(_f.getAbsolutePath() + ext);
}
@@ -830,9 +779,7 @@
* @return _ext sans le point au debut
*/
public static String getCorrectExtension(final String _ext) {
- if (_ext == null) {
- return null;
- }
+ if (_ext == null) { return null; }
String res = _ext.trim();
if (res.startsWith(getDot())) {
res = res.substring(1);
@@ -841,9 +788,7 @@
}
public static String[] getCorrectExtension(final String[] _ext) {
- if (_ext == null) {
- return null;
- }
+ if (_ext == null) { return null; }
final String[] res = new String[_ext.length];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = getCorrectExtension(_ext[i]);
@@ -969,6 +914,63 @@
}
/**
+ * A utiliser pour de petits fichiers: le fichier est copie dans un buffer. Commence par remplacer _oldToken par
+ * _newToken puis fait de meme pour 1.
+ *
+ * @param _fileFrom le fichier source
+ * @param _fileTo le fichier de dest
+ * @param _oldToken le motif a remplacer
+ * @param _newToken le motif qui le remplacera
+ * @param _oldToken1 le motif 1 a remplacer
+ * @param _newToken1 le motif 1 qui le remplacera
+ * @param _encoding l'encodage du fichier
+ * @return true si operation reussie
+ */
+ public static boolean replaceAndCopyFile(final Reader _fileFrom, final File _fileTo, Map<String, String> oldNew,
+ final String _encoding) {
+ boolean r = true;
+ BufferedReader reader = null;
+ Writer writer = null;
+ try {
+ reader = new BufferedReader(_fileFrom);
+ writer = new BufferedWriter(_encoding == null ? new FileWriter(_fileTo) : new OutputStreamWriter(
+ new FileOutputStream(_fileTo), _encoding));
+ final StringBuffer b = new StringBuffer(8192);
+ int lu;
+ while ((lu = reader.read()) != -1) {
+ b.append((char) lu);
+ }
+ String n=b.toString();
+ for (Map.Entry<String, String> entry : oldNew.entrySet()) {
+ n=FuLib.replace(n, entry.getKey(), entry.getValue());
+
+ }
+
+ writer.write(n);
+ writer.flush();
+ } catch (final FileNotFoundException io) {
+ FuLog.error("file not found " + io.getMessage());
+ r = false;
+ } catch (final UnsupportedEncodingException io) {
+ FuLog.error("encode not supported " + io.getMessage());
+ r = false;
+ } catch (final IOException io) {
+ FuLog.error("i/o error " + io.getMessage());
+ r = false;
+ } finally {
+ try {
+ if (reader != null) {
+ reader.close();
+ }
+ if (writer != null) {
+ writer.close();
+ }
+ } catch (final IOException _io) {}
+ }
+ return r;
+ }
+
+ /**
* prive car utilitaire.
*/
private CtuluLibFile() {
@@ -983,9 +985,7 @@
* @return null si le fichier peut etre ecrit. Explication sinon _f est null.
*/
public static String canWrite(final File _f) {
- if (_f == null) {
- return CtuluLib.getS("Le fichier n'est pas correct (null)");
- }
+ if (_f == null) { return CtuluLib.getS("Le fichier n'est pas correct (null)"); }
if (_f.exists()) {
if (!_f.canWrite()) return CtuluLib.getS("Le fichier '{0}' existe d\xE9j\xE0 et est prot\xE9g\xE9", _f.getName());
@@ -1007,9 +1007,7 @@
}
public static boolean isWritable(final File _f) {
- if (_f == null) {
- return false;
- }
+ if (_f == null) { return false; }
if (_f.exists() && !_f.canWrite()) {
return false;
} else if (_f.getParentFile() == null) {
@@ -1064,9 +1062,7 @@
public static String litFichierTexte(final File _fichier, final double _tailleMax, final boolean _lectureFin,
final boolean _affiche) throws IOException {
final byte[] res = litFichier(_fichier, _tailleMax, _lectureFin, _affiche);
- if (res == null) {
- return null;
- }
+ if (res == null) { return null; }
return new String(res);
}
@@ -1082,9 +1078,7 @@
public static String litFichierTexte(final File _fichier, final double _tailleMax, final boolean _lectureFin)
throws IOException {
final byte[] res = litFichier(_fichier, _tailleMax, _lectureFin);
- if (res == null) {
- return null;
- }
+ if (res == null) { return null; }
return new String(res);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|