bump product version to 4.1.6.2
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / global / GlobalFile.java
blob03390e954690560453f2a0582a6dcee5e4006c41
1 /**
2 * Description: Global package for file operations.
4 * @ Author Create/Modi Note
5 * Xiaofeng Xie Jun 15, 2002
7 * @version 1.0
8 * @Since MAOS1.0
9 */
12 package net.adaptivebox.global;
14 import java.io.*;
15 import java.util.*;
17 public class GlobalFile {
19 // used by the createTempDir to give an index of temp number.
20 private static int counter = -1;
22 /**
23 * Create a temp directory in the given directory.
24 * @param prefix the prefix for the directory.
25 * @param directory the directory that the temp dirctory placed.
26 * @return If a temp directory is created, return a File Object, else
27 * return null.
29 public static File createTempDir(String prefix, String directory)
30 throws IOException {
31 File f = null;
32 String tempDir;
33 boolean isCreated = false;
34 do {
35 if (counter == -1) {
36 counter = new Random().nextInt() & 0xffff;
38 counter++;
39 if (prefix == null)
40 throw new NullPointerException();
41 if (prefix.length() < 3)
42 throw new IllegalArgumentException("Prefix string too short");
43 if (directory == null) {
44 tempDir = prefix + counter;
45 } else {
46 tempDir = getFileLocation(directory, prefix + counter);
48 f = new File(tempDir);
49 isCreated = f.mkdir();
50 } while (!isCreated);
51 return f;
54 /**
55 * Add the given text string to the end of a given file.
56 * @param inStr The string to be added.
57 * @param fileStr the name of the file to be added.
59 public static void addStringToFile(String inStr, String fileStr) throws Exception {
61 RandomAccessFile raFile = new RandomAccessFile(fileStr,"rw");
62 raFile.seek(raFile.length());
63 raFile.writeBytes(inStr);
64 raFile.close();
67 // String oldFileStr = getStringFromFile(fileStr);
68 // String newFileStr = inStr;
69 // if (oldFileStr != null) {
70 // newFileStr = oldFileStr+inStr;
71 // }
72 // saveStringToFile(newFileStr,fileStr);
76 public static Object loadObjectFromFile(String fileName) throws Exception {
77 FileInputStream fis = new FileInputStream(fileName);
78 ObjectInputStream ois = new ObjectInputStream(fis);
79 Object obj = ois.readObject();
80 ois.close();
81 return obj;
84 public static void saveObjectToFile(String fileName, Object obj) throws Exception {
85 FileOutputStream ostream = new FileOutputStream(fileName);
86 ObjectOutputStream p = new ObjectOutputStream(ostream);
87 p.writeObject(obj);
88 p.flush();
89 ostream.close();
92 /**
93 * Save the given text string to a given file.
94 * @param inStr The string to be saved.
95 * @param fileStr the name of the file to be saved.
97 public static void saveStringToFile(String inStr, String fileStr) throws Exception{
98 new File(new File(fileStr).getParent()).mkdirs();
99 FileOutputStream pspOutputStream = new FileOutputStream(new File(fileStr));
100 pspOutputStream.write(inStr.getBytes());
101 pspOutputStream.close();
105 * Load text string from a given file.
106 * @param fileStr the name of the file to be loaded.
107 * @return A text string that is the content of the file. if the given file is
108 * not exist, then return null.
110 public static String getStringFromFile(String fileStr) throws Exception {
111 String getStr = null;
112 FileInputStream pspInputStream = new FileInputStream(fileStr);
113 byte[] pspFileBuffer = new byte[pspInputStream.available()];
114 pspInputStream.read(pspFileBuffer);
115 pspInputStream.close();
116 getStr = new String(pspFileBuffer);
117 return(getStr);
121 * Load curve data from a specified file.
122 * @param fileStr the name of the file to be loaded.
123 * @return An ArrayList that include the curve data.
125 public static ArrayList<ArrayList<Double>> getCurveDataFromFile(String fileName) {
126 File file = new File(fileName);
127 if(!file.exists()){
128 //showMessage();
129 return null;
131 //open data file
132 FileInputStream inStream = null;
133 BufferedReader inReader = null;
134 try{
135 inStream = new FileInputStream(file);
136 inReader = new BufferedReader(new InputStreamReader(inStream));
137 }catch(Exception e){
138 //showMessage();
139 return null;//Data file open error.
141 ArrayList<Double> xaxes = new ArrayList<Double>(1);
142 ArrayList<Double> yaxes = new ArrayList<Double>(1);
143 try{
144 StringTokenizer st;
145 String s;
146 boolean start = false;
147 while(inReader.ready()!=false){
148 st = new StringTokenizer(inReader.readLine());
149 over:{
150 while(!st.hasMoreTokens()){//Justify blank lines.
151 if(inReader.ready()!=false){
152 st = new StringTokenizer(inReader.readLine());
153 }else
154 break over;
156 s = st.nextToken();
157 if((!start)&&(!s.startsWith("@")))
158 break over;
159 if(!start){
160 start = true;
161 break over;
163 if(s.startsWith("#")||s.startsWith("$")||s.startsWith("/")) break over;//Justify comment line.
164 Double xaxis = null;
165 Double yaxis = null;
166 try{
167 xaxis = Double.valueOf(s);
168 xaxes.add(xaxis);
169 }catch(NumberFormatException e){
170 //showMessage();
171 inReader.close();
172 inStream.close();
173 return null;//Data file data format error.
175 s = st.nextToken();
176 try{
177 yaxis = Double.valueOf(s);
178 yaxes.add(yaxis);
179 }catch(NumberFormatException e){
180 //showMessage();
181 inReader.close();
182 inStream.close();
183 return null;//Data file data format error.
187 inReader.close();
188 }catch(Exception e){
189 //showMessage();
190 return null;//Uncertain data file error.
192 ArrayList<ArrayList<Double>> curveData = new ArrayList<ArrayList<Double>>(2);
193 curveData.add(xaxes);
194 curveData.add(yaxes);
195 return curveData;
199 * Get a full path of a given file name and a directory name.
200 * @param fileName the name of the file.
201 * @param dir the name of directory.
202 * @return The full path.
204 public static String getFileLocation(String dir, String fileName) {
205 String realDir = dir;
206 while (realDir.length()>0 && (realDir.endsWith("/")||realDir.endsWith("\\"))) {
207 realDir = dir.substring(0, dir.length()-1);
209 return realDir+BasicTag.FILE_SEP_TAG+fileName;
212 public static String getFileName(String nameBody, String suffix) {
213 if (suffix==null || suffix.trim().length()==0) {
214 return nameBody;
216 String fileName = nameBody;
217 if(nameBody.endsWith(".")) {
218 return fileName+suffix;
219 } else {
220 return nameBody+"."+suffix;
224 public static String getFileLocation(String dir, String fileNameBody, String fileNameSuffix) {
225 String filename = getFileName(fileNameBody, fileNameSuffix);
226 return getFileLocation(dir, filename);
229 public static void clear(String fileStr) throws Exception {
230 File file = new File(fileStr);
231 if(file.isFile()) {
232 file.delete();
233 return;
235 String[] fileNames = file.list();
236 if (fileNames==null) {
237 return;
239 for (int i=0; i<fileNames.length; i++) {
240 String newFileName = GlobalFile.getFileLocation(fileStr,fileNames[i]);
241 clear(newFileName);
243 file.delete();
246 public static String getFilePrefix(String fileStr) {
247 int index = fileStr.lastIndexOf(BasicTag.DOT_TAG);
248 if(index==-1) index = fileStr.length();
249 return fileStr.substring(0, index);
252 public static String getFileSuffix(String fileStr) {
253 String[] subNames = GlobalString.tokenize(fileStr, BasicTag.DOT_TAG);
254 int subNameLen = subNames.length;
255 if(subNameLen==1) return "";
256 else return subNames[subNameLen-1];
259 public static String createTempImageFile(String origFile) throws Exception {
260 return createTempImageFile(origFile, "img", ".inf");
263 public static String createTempImageFile(String origFile, String prefix, String suffix) throws Exception {
264 File outputFile = createTempFile(prefix, suffix);
265 outputFile.deleteOnExit();
266 copyFile(outputFile.getAbsolutePath(), origFile);
267 return outputFile.getAbsolutePath();
270 public static void copyFile(String imgFile, String origFile) throws Exception {
271 String fileContent = GlobalFile.getStringFromFile(origFile);
272 GlobalFile.saveStringToFile(fileContent, imgFile);
275 public static File createTempFile(String prefix, String suffix) throws Exception {
276 String realSuffix = suffix;
277 if (!realSuffix.startsWith(".")) realSuffix = "."+suffix;
278 return File.createTempFile(prefix, realSuffix);