Eclipse-PyUML/pyUml/src/pyUML/backend/JavaHelperMethods.java

85 lines
2.2 KiB
Java
Executable File

package pyUML.backend;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* some helper methods for common java problems, i.e. file copying
*/
public class JavaHelperMethods {
/**
* Fast & simple file copy.
* copies source file to destination file
*/
public static void copy(File source, File dest) throws IOException {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(dest);
final int bufferSize = 4096;
try {
byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = in.read(buffer)) >= 0) {
out.write(buffer, 0, bytesRead);
}
} finally {
out.close();
in.close();
}
}
public static InputStream stringToStream(String s) {
return new ByteArrayInputStream(s.getBytes());
}
/**
* Joins an Array of Strings to one big String separated by
* a separator
* @param array The String array to join
* @param separator The separator
* @return the joined String
*/
public static String join(String[] array, String separator) {
if (array==null) return "";
StringBuffer sb = new StringBuffer(array[0]);
for(int i=1; i<array.length; i++) {
sb.append(separator + array[i]);
}
return sb.toString();
}
/**
* Returns the number of files present in a directory-tree
* matching the given regex.
* This can be used to count the packages (__init__.py)
* for progress monitoring.
* @param parentDir The directory to start with
* @param regex The regex to match
* @return the Number of occurrence of the file in the dir tree
*/
public static int getFileCount(File parentDir, String regex)
{
File[] filesAndDirs = parentDir.listFiles();
int count=0;
for( File file: filesAndDirs )
{
if( file.getName().matches(regex))
{
count ++;
}
if (file.isDirectory())
{
count += getFileCount(file, regex);
}
}
return count;
}
}