From: <de...@us...> - 2012-12-20 14:02:41
|
Revision: 8159 http://fudaa.svn.sourceforge.net/fudaa/?rev=8159&view=rev Author: deniger Date: 2012-12-20 14:02:32 +0000 (Thu, 20 Dec 2012) Log Message: ----------- Modified Paths: -------------- trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/fileformat/FileOperationAbstract.java trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoDoubleArrayBinaryFileSaver.java trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java trunk/framework/ebli-common/src/main/java/org/fudaa/ebli/commun/EbliUIProperties.java Added Paths: ----------- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoArraysBinaryFileSaver.java Removed Paths: ------------- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java Modified: trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/fileformat/FileOperationAbstract.java =================================================================== --- trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/fileformat/FileOperationAbstract.java 2012-12-20 10:14:50 UTC (rev 8158) +++ trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/fileformat/FileOperationAbstract.java 2012-12-20 14:02:32 UTC (rev 8159) @@ -7,6 +7,7 @@ * @mail de...@fu... */ package org.fudaa.ctulu.fileformat; + import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; @@ -18,20 +19,24 @@ import org.fudaa.ctulu.CtuluAnalyze; import org.fudaa.ctulu.CtuluIOOperationSynthese; import org.fudaa.ctulu.ProgressionInterface; + /** * @deprecated use {@link FileReadWriteAbstract} * @author deniger * @version $Id: FileOperationAbstract.java,v 1.5 2006-07-13 13:34:41 deniger Exp $ */ public abstract class FileOperationAbstract { + protected ProgressionInterface progress_; protected CtuluAnalyze analyze_; + /** * @param _progressReceiver l'ecouter pour la progression */ public void setProgressReceiver(final ProgressionInterface _progressReceiver) { - progress_= _progressReceiver; + progress_ = _progressReceiver; } + protected abstract FortranInterface getFortranInterface(); /** @@ -40,35 +45,52 @@ public abstract void setFile(File _f); protected final CtuluIOOperationSynthese closeOperation(final Object _o) { - final CtuluIOOperationSynthese r= new CtuluIOOperationSynthese(); - final FortranInterface out= getFortranInterface(); - r.setClosingOperation(FortranLib.closeState(out)); + final CtuluIOOperationSynthese r = new CtuluIOOperationSynthese(); + final FortranInterface out = getFortranInterface(); + if (closeStreamAtEnd) { + r.setClosingOperation(FortranLib.closeState(out)); + } r.setAnalyze(analyze_); r.setSource(_o); - progress_= null; - analyze_= null; + progress_ = null; + analyze_ = null; return r; } + private boolean closeStreamAtEnd = true; + /** + * + * @param closeStreamAtEnd si false, le flux ne sera pas ferm\xE9 \xE0 la fin + */ + public void setCloseOutputStreamAtEnd(boolean closeStreamAtEnd) { + this.closeStreamAtEnd = closeStreamAtEnd; + } + + /** * @param _f le fichier a lire * @return le FileReader correspondant */ public static final FileReader initFileReader(final File _f) { - FileReader r= null; + FileReader r = null; try { - r= new FileReader(_f); - } catch (final FileNotFoundException _e) {FuLog.error(_e);} + r = new FileReader(_f); + } catch (final FileNotFoundException _e) { + FuLog.error(_e); + } return r; } + /** * @param _f le fichier a ecrire * @return le FileWriter correspondant */ public static final FileWriter initFileWriter(final File _f) { - FileWriter r= null; + FileWriter r = null; try { - r= new FileWriter(_f); - } catch (final IOException _e) {FuLog.error(_e);} + r = new FileWriter(_f); + } catch (final IOException _e) { + FuLog.error(_e); + } return r; } } Copied: trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoArraysBinaryFileSaver.java (from rev 8158, trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java) =================================================================== --- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoArraysBinaryFileSaver.java (rev 0) +++ trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoArraysBinaryFileSaver.java 2012-12-20 14:02:32 UTC (rev 8159) @@ -0,0 +1,281 @@ +package org.fudaa.dodico.fortran; + +import com.memoire.fu.FuEmptyArrays; +import java.io.File; +import java.io.FileOutputStream; + +import org.fudaa.ctulu.fileformat.FortranLib; +import org.fudaa.dodico.fichiers.NativeBinarySystem; + +import com.memoire.fu.FuLog; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import org.fudaa.ctulu.CtuluLibArray; +import org.fudaa.ctulu.CtuluLibFile; + +/** + * Permet d'\xE9crire facilement des tableaux d'entier, bool\xE9ens dans un fichier binaire. + * + * @author Frederic Deniger + */ +public class DodicoArraysBinaryFileSaver { + + public DodicoArraysBinaryFileSaver() { + } + + public boolean saveSimpleArray(File file, int[] in) { + int[][] toSave = new int[1][]; + toSave[0] = in; + return save(file, toSave); + } + + public boolean saveSimpleArray(OutputStream file, int[] in) { + int[][] toSave = new int[1][]; + toSave[0] = in; + return save(file, toSave); + } + + public boolean saveDouble(File file, double[] in) { + boolean ok = false; + FileOutputStream out = null; + try { + out = new FileOutputStream(file); + ok = saveDouble(out, in); + } catch (Exception exception) { + ok = false; + FuLog.error(exception); + } finally { + CtuluLibFile.close(out); + } + return ok; + + } + + public boolean saveDouble(OutputStream file, double[] in) { + FortranBinaryOutputStream output = null; + boolean ok = true; + try { + output = new FortranBinaryOutputStream(file, true, NativeBinarySystem.X86); + int nbValues = in.length; + output.writeInteger(nbValues); + output.writeRecord(); + for (int i = 0; i < nbValues; i++) { + output.writeDoublePrecision(in[i]); + } + output.writeRecord(); + } catch (Exception e) { + ok = false; + FuLog.error(e); + } finally { + } + return ok; + } + + public int[] loadSimpleArray(File file) { + int[][] res = load(file); + return CtuluLibArray.isEmpty(res) ? FuEmptyArrays.INT0 : res[0]; + } + + public int[] loadSimpleArray(InputStream file) { + int[][] res = load(file); + return CtuluLibArray.isEmpty(res) ? FuEmptyArrays.INT0 : res[0]; + } + + private static int[] toInt(boolean[] in) { + if (in == null) { + return FuEmptyArrays.INT0; + } + int[] out = new int[in.length]; + for (int i = 0; i < out.length; i++) { + out[i] = in[i] ? 1 : 0; + } + return out; + } + + private static boolean[] toBoolean(int[][] in) { + if (in == null || in.length == 0 || in[0].length == 0) { + return FuEmptyArrays.BOOLEAN0; + } + final int[] values = in[0]; + boolean[] out = new boolean[values.length]; + for (int i = 0; i < out.length; i++) { + out[i] = values[i] == 1 ? true : false; + } + return out; + } + + public boolean saveBoolean(File file, boolean[] in) { + int[][] toSave = new int[1][]; + toSave[0] = toInt(in); + return save(file, toSave); + } + + public boolean saveBoolean(OutputStream file, boolean[] in) { + int[][] toSave = new int[1][]; + toSave[0] = toInt(in); + return save(file, toSave); + } + + public boolean[] loadBoolean(File file) { + int[][] res = load(file); + return toBoolean(res); + } + + public boolean[] loadBoolean(InputStream file) { + int[][] res = load(file); + return toBoolean(res); + } + + public int[][] load(File file) { + FileInputStream input = null; + int[][] res = null; + try { + input = new FileInputStream(file); + res = load(input); + } catch (Exception e) { + FuLog.error(e); + } finally { + CtuluLibFile.close(input); + } + return res == null ? new int[0][0] : res; + } + + public double[] loadDouble(File file) { + FileInputStream input = null; + double[] res = null; + try { + input = new FileInputStream(file); + res = loadDouble(input); + } catch (Exception e) { + FuLog.error(e); + } finally { + CtuluLibFile.close(input); + } + return res == null ? FuEmptyArrays.DOUBLE0 : res; + } + + public int[][] load(InputStream in) { + FortranBinaryInputStream input = null; + if (in == null) { + return new int[0][0]; + } + boolean ok = true; + int[][] mapOfValues = null; + try { + try { + input = new FortranBinaryInputStream(in, true, NativeBinarySystem.X86); + input.readRecord(); + int nbValues = input.readInteger(); + if (nbValues >= 0) { + mapOfValues = new int[nbValues][]; + for (int i = 0; i < nbValues; i++) { + input.readRecord(); + int nbItem = input.readInteger(); + if (nbItem < 0) { + ok = false; + break; + } + mapOfValues[i] = new int[nbItem]; + for (int j = 0; j < nbItem; j++) { + mapOfValues[i][j] = input.readInteger(); + } + } + } else { + ok = false; + } + } catch (Exception e) { + FuLog.error(e); + } + + } catch (Exception e) { + FuLog.error(e); + } + if (ok) { + return mapOfValues; + } + return new int[0][0]; + } + + public double[] loadDouble(InputStream in) { + if (in == null) { + return FuEmptyArrays.DOUBLE0; + } + boolean ok = true; + double[] mapOfValues = null; + FortranBinaryInputStream input = null; + try { + try { + input = new FortranBinaryInputStream(in, true, NativeBinarySystem.X86); + input.readRecord(); + int nbValues = input.readInteger(); + input.readRecord(); + if (nbValues >= 0) { + mapOfValues = new double[nbValues]; + for (int i = 0; i < nbValues; i++) { + mapOfValues[i] = input.readDoublePrecision(); + } + } else { + ok = false; + } + } catch (Exception e) { + FuLog.error(e); + } + + } catch (Exception e) { + FuLog.error(e); + } + if (ok) { + return mapOfValues; + } + return FuEmptyArrays.DOUBLE0; + } + + public boolean save(File file, int[][] mapOfValues) { + boolean ok = false; + FileOutputStream out = null; + try { + out = new FileOutputStream(file); + ok = save(out, mapOfValues); + } catch (Exception exception) { + ok = false; + FuLog.error(exception); + } finally { + CtuluLibFile.close(out); + } + return ok; + } + + public boolean save(OutputStream out, int[][] mapOfValues) { + return save(out, mapOfValues, false); + + } + + public boolean save(OutputStream out, int[][] mapOfValues, boolean close) { + FortranBinaryOutputStream output = null; + boolean ok = true; + try { + output = new FortranBinaryOutputStream(out, true, NativeBinarySystem.X86); + int nbValues = mapOfValues.length; + output.writeInteger(nbValues); + output.writeRecord(); + for (int i = 0; i < nbValues; i++) { + int[] values = mapOfValues[i]; + output.writeInteger(values.length); + for (int j = 0; j < values.length; j++) { + output.writeInteger(values[j]); + } + output.writeRecord(); + } + } catch (Exception e) { + ok = false; + FuLog.error(e); + } finally { + if (close) { + FortranLib.close(output); + } + } + return ok; + + } +} Modified: trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoDoubleArrayBinaryFileSaver.java =================================================================== --- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoDoubleArrayBinaryFileSaver.java 2012-12-20 10:14:50 UTC (rev 8158) +++ trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoDoubleArrayBinaryFileSaver.java 2012-12-20 14:02:32 UTC (rev 8159) @@ -23,11 +23,11 @@ } public Map<String, double[]> load() { - FortranBinaryInputStream input = null; Map<String, double[]> mapOfValues = new HashMap<String, double[]>(); if (destFile.length() == 0) { return mapOfValues; } + FortranBinaryInputStream input = null; try { try { input = new FortranBinaryInputStream(new FileInputStream(destFile), true, NativeBinarySystem.X86); @@ -46,7 +46,7 @@ mapOfValues.put(name, values); } } catch (EOFException e) { - e.printStackTrace(); + FuLog.error(e); } } catch (Exception e) { Deleted: trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java =================================================================== --- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java 2012-12-20 10:14:50 UTC (rev 8158) +++ trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java 2012-12-20 14:02:32 UTC (rev 8159) @@ -1,189 +0,0 @@ -package org.fudaa.dodico.fortran; - -import com.memoire.fu.FuEmptyArrays; -import java.io.File; -import java.io.FileOutputStream; - -import org.fudaa.ctulu.fileformat.FortranLib; -import org.fudaa.dodico.fichiers.NativeBinarySystem; - -import com.memoire.fu.FuLog; -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import org.fudaa.ctulu.CtuluLibArray; -import org.fudaa.ctulu.CtuluLibFile; - -/** - * Permet d'\xE9crire facilement des tableaux d'entier, bool\xE9ens dans un fichier binaire. - * @author Frederic Deniger - */ -public class DodicoIntegerArrayBinaryFileSaver { - - public DodicoIntegerArrayBinaryFileSaver() { - } - - public boolean saveSimpleArray(File file, int[] in) { - int[][] toSave = new int[1][]; - toSave[0] = in; - return save(file, toSave); - } - - public boolean saveSimpleArray(OutputStream file, int[] in) { - int[][] toSave = new int[1][]; - toSave[0] = in; - return save(file, toSave); - } - - public int[] loadSimpleArray(File file) { - int[][] res = load(file); - return CtuluLibArray.isEmpty(res) ? FuEmptyArrays.INT0 : res[0]; - } - - public int[] loadSimpleArray(InputStream file) { - int[][] res = load(file); - return CtuluLibArray.isEmpty(res) ? FuEmptyArrays.INT0 : res[0]; - } - - private static int[] toInt(boolean[] in) { - if (in == null) { - return FuEmptyArrays.INT0; - } - int[] out = new int[in.length]; - for (int i = 0; i < out.length; i++) { - out[i] = in[i] ? 1 : 0; - } - return out; - } - - private static boolean[] toBoolean(int[][] in) { - if (in == null || in.length == 0 || in[0].length == 0) { - return FuEmptyArrays.BOOLEAN0; - } - final int[] values = in[0]; - boolean[] out = new boolean[values.length]; - for (int i = 0; i < out.length; i++) { - out[i] = values[i] == 1 ? true : false; - } - return out; - } - - public boolean saveBoolean(File file, boolean[] in) { - int[][] toSave = new int[1][]; - toSave[0] = toInt(in); - return save(file, toSave); - } - - public boolean saveBoolean(OutputStream file, boolean[] in) { - int[][] toSave = new int[1][]; - toSave[0] = toInt(in); - return save(file, toSave); - } - - public boolean[] loadBoolean(File file) { - int[][] res = load(file); - return toBoolean(res); - } - - public boolean[] loadBoolean(InputStream file) { - int[][] res = load(file); - return toBoolean(res); - } - - public int[][] load(File file) { - FileInputStream input = null; - int[][] res = null; - try { - input = new FileInputStream(file); - res = load(input); - } catch (Exception e) { - FuLog.error(e); - } finally { - CtuluLibFile.close(input); - } - return res == null ? new int[0][0] : res; - } - - public int[][] load(InputStream in) { - FortranBinaryInputStream input = null; - if (in == null) { - return new int[0][0]; - } - boolean ok = true; - int[][] mapOfValues = null; - try { - try { - input = new FortranBinaryInputStream(in, true, NativeBinarySystem.X86); - input.readRecord(); - int nbValues = input.readInteger(); - if (nbValues >= 0) { - mapOfValues = new int[nbValues][]; - for (int i = 0; i < nbValues; i++) { - input.readRecord(); - int nbItem = input.readInteger(); - if (nbItem < 0) { - ok = false; - break; - } - mapOfValues[i] = new int[nbItem]; - for (int j = 0; j < nbItem; j++) { - mapOfValues[i][j] = input.readInteger(); - } - } - } else { - ok = false; - } - } catch (Exception e) { - FuLog.error(e); - } - - } catch (Exception e) { - FuLog.error(e); - } - if (ok) { - return mapOfValues; - } - return new int[0][0]; - } - - public boolean save(File file, int[][] mapOfValues) { - boolean ok = false; - FileOutputStream out = null; - try { - out = new FileOutputStream(file); - ok = save(out, mapOfValues); - } catch (Exception exception) { - ok = false; - FuLog.error(exception); - } finally { - CtuluLibFile.close(out); - } - return ok; - } - - public boolean save(OutputStream out, int[][] mapOfValues) { - FortranBinaryOutputStream output = null; - boolean ok = true; - try { - output = new FortranBinaryOutputStream(out, true, NativeBinarySystem.X86); - int nbValues = mapOfValues.length; - output.writeInteger(nbValues); - output.writeRecord(); - for (int i = 0; i < nbValues; i++) { - int[] values = mapOfValues[i]; - output.writeInteger(values.length); - for (int j = 0; j < values.length; j++) { - output.writeInteger(values[j]); - } - output.writeRecord(); - } - } catch (Exception e) { - ok = false; - FuLog.error(e); - } finally { - FortranLib.close(output); - } - return ok; - - } -} Modified: trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java =================================================================== --- trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java 2012-12-20 10:14:50 UTC (rev 8158) +++ trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java 2012-12-20 14:02:32 UTC (rev 8159) @@ -26,7 +26,7 @@ } } File tempFile = createTempFile(); - DodicoIntegerArrayBinaryFileSaver saver = new DodicoIntegerArrayBinaryFileSaver(); + DodicoArraysBinaryFileSaver saver = new DodicoArraysBinaryFileSaver(); saver.save(tempFile, values); int[][] load = saver.load(tempFile); assertEquals(values.length, load.length); @@ -44,7 +44,7 @@ values[i] = i * 2; } File tempFile = createTempFile(); - DodicoIntegerArrayBinaryFileSaver saver = new DodicoIntegerArrayBinaryFileSaver(); + DodicoArraysBinaryFileSaver saver = new DodicoArraysBinaryFileSaver(); saver.saveSimpleArray(tempFile, values); int[] load = saver.loadSimpleArray(tempFile); assertEquals(values.length, load.length); @@ -59,7 +59,7 @@ values[i] = i % 2 == 0; } File tempFile = createTempFile(); - DodicoIntegerArrayBinaryFileSaver saver = new DodicoIntegerArrayBinaryFileSaver(); + DodicoArraysBinaryFileSaver saver = new DodicoArraysBinaryFileSaver(); saver.saveBoolean(tempFile, values); boolean[] load = saver.loadBoolean(tempFile); assertEquals(values.length, load.length); @@ -67,4 +67,19 @@ assertEquals(values[i], load[i]); } } + + public void testReadWriteDoubleArray() { + double[] values = new double[11]; + for (int i = 0; i < values.length; i++) { + values[i] = Math.random() * 100; + } + File tempFile = createTempFile(); + DodicoArraysBinaryFileSaver saver = new DodicoArraysBinaryFileSaver(); + saver.saveDouble(tempFile, values); + double[] load = saver.loadDouble(tempFile); + assertEquals(values.length, load.length); + for (int i = 0; i < values.length; i++) { + assertEquals(values[i], load[i], 1e-5); + } + } } Modified: trunk/framework/ebli-common/src/main/java/org/fudaa/ebli/commun/EbliUIProperties.java =================================================================== --- trunk/framework/ebli-common/src/main/java/org/fudaa/ebli/commun/EbliUIProperties.java 2012-12-20 10:14:50 UTC (rev 8158) +++ trunk/framework/ebli-common/src/main/java/org/fudaa/ebli/commun/EbliUIProperties.java 2012-12-20 14:02:32 UTC (rev 8159) @@ -14,14 +14,14 @@ import com.thoughtworks.xstream.annotations.XStreamAlias; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang.ObjectUtils; import org.fudaa.ctulu.CtuluLib; import org.fudaa.ctulu.CtuluLibString; /** - * Classe decrivant les propri\xE9t\xE9s d'un calque. NE MODIFIER QUE SI VRAIMENT NECESSAIRE: utiliser pour serialiser l'etat - * d'un calque - * + * Classe decrivant les propri\xE9t\xE9s d'un calque. NE MODIFIER QUE SI VRAIMENT NECESSAIRE: utiliser pour serialiser l'etat d'un calque + * * @author Fred Deniger * @version $Id: EbliUIProperties.java,v 1.7 2007-04-16 16:35:09 deniger Exp $ */ @@ -36,6 +36,50 @@ values_ = new HashMap(); } + public void addAll(EbliUIProperties other) { + if (other == null) { + return; + } + for (Object object : other.values_.entrySet()) { + java.lang.Object key = ((Map.Entry) object).getKey(); + java.lang.Object value = ((Map.Entry) object).getValue(); + values_.put(key, value); + } + } + + /** + * + * @param prefix le prefix \xE0 ajouter \xE0 toutes les propri\xE9t\xE9s + * @return une nouvelles instance de EbliUIProperties + */ + public EbliUIProperties cloneWithPrefix(String prefix) { + EbliUIProperties res = new EbliUIProperties(name_); + for (Object object : values_.entrySet()) { + java.lang.Object key = ((Map.Entry) object).getKey(); + java.lang.Object value = ((Map.Entry) object).getValue(); + res.values_.put(prefix + key, value); + } + return res; + } + + /** + * + * @param prefix + * @return une map de propri\xE9t\xE9 contenant uniquement celles commencant par le prefix. + */ + public EbliUIProperties extractSubset(String prefix) { + EbliUIProperties res = new EbliUIProperties(name_); + int prefixSize = prefix.length(); + for (Object object : values_.entrySet()) { + String key = ObjectUtils.toString(((Map.Entry) object).getKey()); + java.lang.Object value = ((Map.Entry) object).getValue(); + if (key != null && key.startsWith(prefix)) { + res.values_.put(key.substring(prefixSize), value); + } + } + return res; + } + public EbliUIProperties(final String _name) { this(); name_ = _name; @@ -116,5 +160,4 @@ public void setLayerName(final String _name) { name_ = _name; } - } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |