Java cleanup, use generics
Change-Id: I164e0f8386558641497258cc0a1d731e81a51c15
This commit is contained in:
@@ -1,277 +1,279 @@
|
|||||||
/**
|
/**
|
||||||
* Description: Global package for file operations.
|
* Description: Global package for file operations.
|
||||||
*
|
*
|
||||||
* @ Author Create/Modi Note
|
* @ Author Create/Modi Note
|
||||||
* Xiaofeng Xie Jun 15, 2002
|
* Xiaofeng Xie Jun 15, 2002
|
||||||
*
|
*
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @Since MAOS1.0
|
* @Since MAOS1.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package net.adaptivebox.global;
|
package net.adaptivebox.global;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class GlobalFile {
|
public class GlobalFile {
|
||||||
|
|
||||||
// used by the createTempDir to give an index of temp number.
|
// used by the createTempDir to give an index of temp number.
|
||||||
private static int counter = -1;
|
private static int counter = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a temp directory in the given directory.
|
* Create a temp directory in the given directory.
|
||||||
* @param prefix the prefix for the directory.
|
* @param prefix the prefix for the directory.
|
||||||
* @param directory the directory that the temp dirctory placed.
|
* @param directory the directory that the temp dirctory placed.
|
||||||
* @return If a temp directory is created, return a File Object, else
|
* @return If a temp directory is created, return a File Object, else
|
||||||
* return null.
|
* return null.
|
||||||
*/
|
*/
|
||||||
public static File createTempDir(String prefix, String directory) throws IOException {
|
public static File createTempDir(String prefix, String directory)
|
||||||
File f = null;
|
throws IOException {
|
||||||
String tempDir;
|
File f = null;
|
||||||
boolean isCreated = false;
|
String tempDir;
|
||||||
do {
|
boolean isCreated = false;
|
||||||
if (counter == -1) {
|
do {
|
||||||
counter = new Random().nextInt() & 0xffff;
|
if (counter == -1) {
|
||||||
}
|
counter = new Random().nextInt() & 0xffff;
|
||||||
counter++;
|
}
|
||||||
if (prefix == null) throw new NullPointerException();
|
counter++;
|
||||||
if (prefix.length() < 3)
|
if (prefix == null)
|
||||||
throw new IllegalArgumentException("Prefix string too short");
|
throw new NullPointerException();
|
||||||
if (directory == null) {
|
if (prefix.length() < 3)
|
||||||
tempDir = prefix+counter;
|
throw new IllegalArgumentException("Prefix string too short");
|
||||||
} else {
|
if (directory == null) {
|
||||||
tempDir = getFileLocation(directory,prefix+counter);
|
tempDir = prefix + counter;
|
||||||
}
|
} else {
|
||||||
f = new File(tempDir);
|
tempDir = getFileLocation(directory, prefix + counter);
|
||||||
isCreated = f.mkdir();
|
}
|
||||||
} while (!isCreated);
|
f = new File(tempDir);
|
||||||
return f;
|
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.
|
* Add the given text string to the end of a given file.
|
||||||
*/
|
* @param inStr The string to be added.
|
||||||
public static void addStringToFile(String inStr, String fileStr) throws Exception {
|
* @param fileStr the name of the file to be added.
|
||||||
|
*/
|
||||||
RandomAccessFile raFile = new RandomAccessFile(fileStr,"rw");
|
public static void addStringToFile(String inStr, String fileStr) throws Exception {
|
||||||
raFile.seek(raFile.length());
|
|
||||||
raFile.writeBytes(inStr);
|
RandomAccessFile raFile = new RandomAccessFile(fileStr,"rw");
|
||||||
raFile.close();
|
raFile.seek(raFile.length());
|
||||||
|
raFile.writeBytes(inStr);
|
||||||
|
raFile.close();
|
||||||
// String oldFileStr = getStringFromFile(fileStr);
|
|
||||||
// String newFileStr = inStr;
|
|
||||||
// if (oldFileStr != null) {
|
// String oldFileStr = getStringFromFile(fileStr);
|
||||||
// newFileStr = oldFileStr+inStr;
|
// String newFileStr = inStr;
|
||||||
// }
|
// if (oldFileStr != null) {
|
||||||
// saveStringToFile(newFileStr,fileStr);
|
// newFileStr = oldFileStr+inStr;
|
||||||
|
// }
|
||||||
}
|
// saveStringToFile(newFileStr,fileStr);
|
||||||
|
|
||||||
public static Object loadObjectFromFile(String fileName) throws Exception {
|
}
|
||||||
FileInputStream fis = new FileInputStream(fileName);
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
public static Object loadObjectFromFile(String fileName) throws Exception {
|
||||||
Object obj = ois.readObject();
|
FileInputStream fis = new FileInputStream(fileName);
|
||||||
ois.close();
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||||
return obj;
|
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);
|
public static void saveObjectToFile(String fileName, Object obj) throws Exception {
|
||||||
p.writeObject(obj);
|
FileOutputStream ostream = new FileOutputStream(fileName);
|
||||||
p.flush();
|
ObjectOutputStream p = new ObjectOutputStream(ostream);
|
||||||
ostream.close();
|
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.
|
* Save the given text string to a given file.
|
||||||
*/
|
* @param inStr The string to be saved.
|
||||||
public static void saveStringToFile(String inStr, String fileStr) throws Exception{
|
* @param fileStr the name of the file to be saved.
|
||||||
new File(new File(fileStr).getParent()).mkdirs();
|
*/
|
||||||
FileOutputStream pspOutputStream = new FileOutputStream(new File(fileStr));
|
public static void saveStringToFile(String inStr, String fileStr) throws Exception{
|
||||||
pspOutputStream.write(inStr.getBytes());
|
new File(new File(fileStr).getParent()).mkdirs();
|
||||||
pspOutputStream.close();
|
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
|
* Load text string from a given file.
|
||||||
* not exist, then return null.
|
* @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
|
||||||
public static String getStringFromFile(String fileStr) throws Exception {
|
* not exist, then return null.
|
||||||
String getStr = null;
|
*/
|
||||||
FileInputStream pspInputStream = new FileInputStream(fileStr);
|
public static String getStringFromFile(String fileStr) throws Exception {
|
||||||
byte[] pspFileBuffer = new byte[pspInputStream.available()];
|
String getStr = null;
|
||||||
pspInputStream.read(pspFileBuffer);
|
FileInputStream pspInputStream = new FileInputStream(fileStr);
|
||||||
pspInputStream.close();
|
byte[] pspFileBuffer = new byte[pspInputStream.available()];
|
||||||
getStr = new String(pspFileBuffer);
|
pspInputStream.read(pspFileBuffer);
|
||||||
return(getStr);
|
pspInputStream.close();
|
||||||
}
|
getStr = new String(pspFileBuffer);
|
||||||
|
return(getStr);
|
||||||
/**
|
}
|
||||||
* Load curve data from a specified file.
|
|
||||||
* @param fileStr the name of the file to be loaded.
|
/**
|
||||||
* @return A Vector that include the curve data.
|
* Load curve data from a specified file.
|
||||||
*/
|
* @param fileStr the name of the file to be loaded.
|
||||||
public static Vector getCurveDataFromFile(String fileName) {
|
* @return An ArrayList that include the curve data.
|
||||||
File file = new File(fileName);
|
*/
|
||||||
if(!file.exists()){
|
public static ArrayList<ArrayList<Double>> getCurveDataFromFile(String fileName) {
|
||||||
//showMessage();
|
File file = new File(fileName);
|
||||||
return null;
|
if(!file.exists()){
|
||||||
}
|
//showMessage();
|
||||||
//open data file
|
return null;
|
||||||
FileInputStream inStream = null;
|
}
|
||||||
BufferedReader inReader = null;
|
//open data file
|
||||||
try{
|
FileInputStream inStream = null;
|
||||||
inStream = new FileInputStream(file);
|
BufferedReader inReader = null;
|
||||||
inReader = new BufferedReader(new InputStreamReader(inStream));
|
try{
|
||||||
}catch(Exception e){
|
inStream = new FileInputStream(file);
|
||||||
//showMessage();
|
inReader = new BufferedReader(new InputStreamReader(inStream));
|
||||||
return null;//Data file open error.
|
}catch(Exception e){
|
||||||
}
|
//showMessage();
|
||||||
Vector xaxes = new Vector(1,1);
|
return null;//Data file open error.
|
||||||
Vector yaxes = new Vector(1,1);
|
}
|
||||||
try{
|
ArrayList<Double> xaxes = new ArrayList<Double>(1);
|
||||||
StringTokenizer st;
|
ArrayList<Double> yaxes = new ArrayList<Double>(1);
|
||||||
String s;
|
try{
|
||||||
boolean start = false;
|
StringTokenizer st;
|
||||||
while(inReader.ready()!=false){
|
String s;
|
||||||
st = new StringTokenizer(inReader.readLine());
|
boolean start = false;
|
||||||
over:{
|
while(inReader.ready()!=false){
|
||||||
while(!st.hasMoreTokens()){//Justify blank lines.
|
st = new StringTokenizer(inReader.readLine());
|
||||||
if(inReader.ready()!=false){
|
over:{
|
||||||
st = new StringTokenizer(inReader.readLine());
|
while(!st.hasMoreTokens()){//Justify blank lines.
|
||||||
}else
|
if(inReader.ready()!=false){
|
||||||
break over;
|
st = new StringTokenizer(inReader.readLine());
|
||||||
}
|
}else
|
||||||
s = st.nextToken();
|
break over;
|
||||||
if((!start)&&(!s.startsWith("@")))
|
}
|
||||||
break over;
|
s = st.nextToken();
|
||||||
if(!start){
|
if((!start)&&(!s.startsWith("@")))
|
||||||
start = true;
|
break over;
|
||||||
break over;
|
if(!start){
|
||||||
}
|
start = true;
|
||||||
if(s.startsWith("#")||s.startsWith("$")||s.startsWith("/")) break over;//Justify comment line.
|
break over;
|
||||||
Double xaxis = null;
|
}
|
||||||
Double yaxis = null;
|
if(s.startsWith("#")||s.startsWith("$")||s.startsWith("/")) break over;//Justify comment line.
|
||||||
try{
|
Double xaxis = null;
|
||||||
xaxis = Double.valueOf(s);
|
Double yaxis = null;
|
||||||
xaxes.addElement(xaxis);
|
try{
|
||||||
}catch(Exception e){
|
xaxis = Double.valueOf(s);
|
||||||
//showMessage();
|
xaxes.add(xaxis);
|
||||||
inReader.close();
|
}catch(NumberFormatException e){
|
||||||
inStream.close();
|
//showMessage();
|
||||||
return null;//Data file data format error.
|
inReader.close();
|
||||||
}
|
inStream.close();
|
||||||
s = st.nextToken();
|
return null;//Data file data format error.
|
||||||
try{
|
}
|
||||||
yaxis = Double.valueOf(s);
|
s = st.nextToken();
|
||||||
yaxes.addElement(yaxis);
|
try{
|
||||||
}catch(Exception e){
|
yaxis = Double.valueOf(s);
|
||||||
//showMessage();
|
yaxes.add(yaxis);
|
||||||
inReader.close();
|
}catch(NumberFormatException e){
|
||||||
inStream.close();
|
//showMessage();
|
||||||
return null;//Data file data format error.
|
inReader.close();
|
||||||
}
|
inStream.close();
|
||||||
}
|
return null;//Data file data format error.
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
}
|
||||||
//showMessage();
|
}
|
||||||
return null;//Uncertain data file error.
|
}catch(Exception e){
|
||||||
}
|
//showMessage();
|
||||||
Vector curveData = new Vector();
|
return null;//Uncertain data file error.
|
||||||
curveData.addElement(xaxes);
|
}
|
||||||
curveData.addElement(yaxes);
|
ArrayList<ArrayList<Double>> curveData = new ArrayList<ArrayList<Double>>(2);
|
||||||
return(curveData);
|
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.
|
* Get a full path of a given file name and a directory name.
|
||||||
* @return The full path.
|
* @param fileName the name of the file.
|
||||||
*/
|
* @param dir the name of directory.
|
||||||
public static String getFileLocation(String dir, String fileName) {
|
* @return The full path.
|
||||||
String realDir = dir;
|
*/
|
||||||
while (realDir.length()>0 && (realDir.endsWith("/")||realDir.endsWith("\\"))) {
|
public static String getFileLocation(String dir, String fileName) {
|
||||||
realDir = dir.substring(0, dir.length()-1);
|
String realDir = dir;
|
||||||
}
|
while (realDir.length()>0 && (realDir.endsWith("/")||realDir.endsWith("\\"))) {
|
||||||
return realDir+BasicTag.FILE_SEP_TAG+fileName;
|
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;
|
public static String getFileName(String nameBody, String suffix) {
|
||||||
}
|
if (suffix==null || suffix.trim().length()==0) {
|
||||||
String fileName = nameBody;
|
return nameBody;
|
||||||
if(nameBody.endsWith(".")) {
|
}
|
||||||
return fileName+suffix;
|
String fileName = nameBody;
|
||||||
} else {
|
if(nameBody.endsWith(".")) {
|
||||||
return nameBody+"."+suffix;
|
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 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()) {
|
public static void clear(String fileStr) throws Exception {
|
||||||
file.delete();
|
File file = new File(fileStr);
|
||||||
return;
|
if(file.isFile()) {
|
||||||
}
|
file.delete();
|
||||||
String[] fileNames = file.list();
|
return;
|
||||||
if (fileNames==null) {
|
}
|
||||||
return;
|
String[] fileNames = file.list();
|
||||||
}
|
if (fileNames==null) {
|
||||||
for (int i=0; i<fileNames.length; i++) {
|
return;
|
||||||
String newFileName = GlobalFile.getFileLocation(fileStr,fileNames[i]);
|
}
|
||||||
clear(newFileName);
|
for (int i=0; i<fileNames.length; i++) {
|
||||||
}
|
String newFileName = GlobalFile.getFileLocation(fileStr,fileNames[i]);
|
||||||
file.delete();
|
clear(newFileName);
|
||||||
}
|
}
|
||||||
|
file.delete();
|
||||||
public static String getFilePrefix(String fileStr) {
|
}
|
||||||
int index = fileStr.lastIndexOf(BasicTag.DOT_TAG);
|
|
||||||
if(index==-1) index = fileStr.length();
|
public static String getFilePrefix(String fileStr) {
|
||||||
return fileStr.substring(0, index);
|
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;
|
public static String getFileSuffix(String fileStr) {
|
||||||
if(subNameLen==1) return "";
|
String[] subNames = GlobalString.tokenize(fileStr, BasicTag.DOT_TAG);
|
||||||
else return subNames[subNameLen-1];
|
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) 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();
|
public static String createTempImageFile(String origFile, String prefix, String suffix) throws Exception {
|
||||||
copyFile(outputFile.getAbsolutePath(), origFile);
|
File outputFile = createTempFile(prefix, suffix);
|
||||||
return outputFile.getAbsolutePath();
|
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 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;
|
public static File createTempFile(String prefix, String suffix) throws Exception {
|
||||||
return File.createTempFile(prefix, realSuffix);
|
String realSuffix = suffix;
|
||||||
}
|
if (!realSuffix.startsWith(".")) realSuffix = "."+suffix;
|
||||||
}
|
return File.createTempFile(prefix, realSuffix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -38,15 +38,11 @@ public class GlobalString {
|
|||||||
* divided by the tokenKey.
|
* divided by the tokenKey.
|
||||||
*/
|
*/
|
||||||
public static String[] tokenize(String input , String tokenKey) {
|
public static String[] tokenize(String input , String tokenKey) {
|
||||||
Vector v = new Vector();
|
ArrayList<String> v = new ArrayList<String>();
|
||||||
StringTokenizer t = new StringTokenizer(input, tokenKey);
|
StringTokenizer t = new StringTokenizer(input, tokenKey);
|
||||||
String cmd[];
|
|
||||||
while (t.hasMoreTokens())
|
while (t.hasMoreTokens())
|
||||||
v.addElement(t.nextToken());
|
v.add(t.nextToken());
|
||||||
cmd = new String[v.size()];
|
return v.toArray(new String[v.size()]);
|
||||||
for (int i = 0; i < cmd.length; i++)
|
|
||||||
cmd[i] = (String) v.elementAt(i);
|
|
||||||
return cmd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] getMeaningfulLines(String srcStr) throws Exception {
|
public static String[] getMeaningfulLines(String srcStr) throws Exception {
|
||||||
|
Reference in New Issue
Block a user