summaryrefslogtreecommitdiff
path: root/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/GlobalFile.java
blob: d1ede927c4b6849710d20e044624b988fa25adbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
 * Description: Global package for file operations.
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    Jun 15, 2002
 *
 * @version 1.0
 * @Since MAOS1.0
 */


package net.adaptivebox.global;

import java.io.*;
import java.util.*;

public class GlobalFile {

// used by the createTempDir to give an index of temp number.
    private static int counter = -1;

/**
  * Create a temp directory in the given directory.
  * @param      prefix      the prefix for the directory.
  * @param      directory   the directory that the temp dirctory placed.
  * @return  If a temp directory is created, return a File Object, else
  * return null.
  */
    public static File createTempDir(String prefix, String directory)
    {
        File f = null;
        String tempDir;
        boolean isCreated = false;
        do {
            if (counter == -1) {
                counter = new Random().nextInt() & 0xffff;
            }
            counter++;
            if (prefix == null)
                throw new NullPointerException();
            if (prefix.length() < 3)
                throw new IllegalArgumentException("Prefix string too short");
            if (directory == null) {
                tempDir = prefix + counter;
            } else {
                tempDir = getFileLocation(directory, prefix + counter);
            }
            f = new File(tempDir);
            isCreated = f.mkdir();
        } while (!isCreated);
        return f;
    }

/**
  * Add the given text string to the end of a given file.
  * @param      inStr       The string to be added.
  * @param      fileStr     the name of the file to be added.
  */
  public static void addStringToFile(String inStr, String fileStr) throws Exception {

    RandomAccessFile raFile = new RandomAccessFile(fileStr,"rw");
    raFile.seek(raFile.length());
    raFile.writeBytes(inStr);
    raFile.close();
  }

  public static Object loadObjectFromFile(String fileName) throws Exception {
    FileInputStream fis = new FileInputStream(fileName);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object obj = ois.readObject();
    ois.close();
    return obj;
  }

  public static void saveObjectToFile(String fileName, Object obj) throws Exception {
    FileOutputStream ostream = new FileOutputStream(fileName);
    ObjectOutputStream p = new ObjectOutputStream(ostream);
    p.writeObject(obj);
    p.flush();
    ostream.close();
  }

/**
  * Save the given text string to a given file.
  * @param      inStr       The string to be saved.
  * @param      fileStr     the name of the file to be saved.
  */
  public static void saveStringToFile(String inStr, String fileStr) throws Exception{
    new File(new File(fileStr).getParent()).mkdirs();
    FileOutputStream pspOutputStream = new FileOutputStream(new File(fileStr));
    pspOutputStream.write(inStr.getBytes());
    pspOutputStream.close();
  }

/**
  * Load text string from a given file.
  * @param      fileStr     the name of the file to be loaded.
  * @return  A text string that is the content of the file. if the given file is
  * not exist, then return null.
  */
  public static String getStringFromFile(String fileStr) throws Exception {
    String getStr = null;
    FileInputStream pspInputStream = new FileInputStream(fileStr);
    byte[] pspFileBuffer = new byte[pspInputStream.available()];
    pspInputStream.read(pspFileBuffer);
    pspInputStream.close();
    getStr = new String(pspFileBuffer);
    return(getStr);
  }

/**
  * Load curve data from a specified file.
  * @param      fileName     the name of the file to be loaded.
  * @return  An ArrayList that include the curve data.
  */
  public static ArrayList<ArrayList<Double>> getCurveDataFromFile(String fileName) {
    File file = new File(fileName);
    if(!file.exists()){
      //showMessage();
      return null;
    }
    //open data file
    FileInputStream inStream = null;
    BufferedReader inReader = null;
    try{
      inStream = new FileInputStream(file);
      inReader = new BufferedReader(new InputStreamReader(inStream));
    }catch(Exception e){
      //showMessage();
      return null;//Data file open error.
    }
    ArrayList<Double> xaxes = new ArrayList<Double>(1);
    ArrayList<Double> yaxes = new ArrayList<Double>(1);
    try{
      StringTokenizer st;
      String s;
      boolean start = false;
      while(inReader.ready()){
        st = new StringTokenizer(inReader.readLine());
        over:{
        while(!st.hasMoreTokens()){//Justify blank lines.
          if(inReader.ready()){
            st = new StringTokenizer(inReader.readLine());
          }else
            break over;
          }
          s = st.nextToken();
          if((!start)&&(!s.startsWith("@")))
            break over;
          if(!start){
            start = true;
            break over;
          }
          if(s.startsWith("#")||s.startsWith("$")||s.startsWith("/")) break over;//Justify comment line.
          Double xaxis = null;
          Double yaxis = null;
          try{
            xaxis = Double.valueOf(s);
            xaxes.add(xaxis);
          }catch(NumberFormatException e){
            //showMessage();
            inReader.close();
            inStream.close();
            return null;//Data file data format error.
          }
          s = st.nextToken();
          try{
            yaxis = Double.valueOf(s);
            yaxes.add(yaxis);
          }catch(NumberFormatException e){
          //showMessage();
          inReader.close();
          inStream.close();
          return null;//Data file data format error.
          }
        }
      }
      inReader.close();
    }catch(Exception e){
      //showMessage();
      return null;//Uncertain data file error.
    }
    ArrayList<ArrayList<Double>> curveData = new ArrayList<ArrayList<Double>>(2);
    curveData.add(xaxes);
    curveData.add(yaxes);
    return curveData;
  }

/**
  * Get a full path of a given file name and a directory name.
  * @param      fileName     the name of the file.
  * @param      dir          the name of directory.
  * @return  The full path.
  */
  public static String getFileLocation(String dir, String fileName) {
    String realDir = dir;
    while (realDir.length()>0 && (realDir.endsWith("/")||realDir.endsWith("\\"))) {
      realDir = dir.substring(0, dir.length()-1);
    }
    return realDir+BasicTag.FILE_SEP_TAG+fileName;
  }

  public static String getFileName(String nameBody, String suffix) {
    if (suffix==null || suffix.trim().length()==0) {
      return nameBody;
    }
    String fileName = nameBody;
    if(nameBody.endsWith(".")) {
      return fileName+suffix;
    } else {
      return nameBody+"."+suffix;
    }
  }

  public static String getFileLocation(String dir, String fileNameBody, String fileNameSuffix) {
    String filename = getFileName(fileNameBody, fileNameSuffix);
    return getFileLocation(dir, filename);
  }

 public static void clear(String fileStr) throws Exception {
   File file = new File(fileStr);
   if(file.isFile()) {
     file.delete();
     return;
   }
   String[] fileNames = file.list();
   if (fileNames==null) {
     return;
   }
   for (int i=0; i<fileNames.length; i++) {
     String newFileName = GlobalFile.getFileLocation(fileStr,fileNames[i]);
     clear(newFileName);
   }
   file.delete();
 }

 public static String getFilePrefix(String fileStr) {
   int index = fileStr.lastIndexOf(BasicTag.DOT_TAG);
   if(index==-1) index = fileStr.length();
   return fileStr.substring(0, index);
 }

 public static String getFileSuffix(String fileStr) {
   String[] subNames = GlobalString.tokenize(fileStr, BasicTag.DOT_TAG);
   int subNameLen = subNames.length;
   if(subNameLen==1) return "";
   else return subNames[subNameLen-1];
 }

 public static String createTempImageFile(String origFile) throws Exception {
   return createTempImageFile(origFile, "img", ".inf");
 }

 public static String createTempImageFile(String origFile, String prefix, String suffix) throws Exception {
   File outputFile = createTempFile(prefix, suffix);
   outputFile.deleteOnExit();
   copyFile(outputFile.getAbsolutePath(), origFile);
   return outputFile.getAbsolutePath();
 }

 public static void copyFile(String imgFile, String origFile) throws Exception {
   String fileContent = GlobalFile.getStringFromFile(origFile);
   GlobalFile.saveStringToFile(fileContent, imgFile);
 }

 public static File createTempFile(String prefix, String suffix) throws Exception {
   String realSuffix = suffix;
   if (!realSuffix.startsWith(".")) realSuffix = "."+suffix;
   return File.createTempFile(prefix, realSuffix);
 }
}