aboutsummaryrefslogtreecommitdiff
path: root/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java')
-rw-r--r--moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java160
1 files changed, 90 insertions, 70 deletions
diff --git a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java
index 309e01a..d64cda0 100644
--- a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java
+++ b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/FileUtils.java
@@ -21,7 +21,6 @@
* that you distribute must include a readable copy of the "NOTICE" text file.
*/
-
package at.gv.egovernment.moaspss.util;
import java.io.BufferedInputStream;
@@ -34,146 +33,167 @@ import java.io.OutputStream;
import java.net.URL;
/**
- * Utility for accessing files on the file system, and for reading from input streams.
+ * Utility for accessing files on the file system, and for reading from input
+ * streams.
+ *
* @author Paul Ivancsics
* @version $Id$
*/
public class FileUtils {
-
+
/**
* Reads a file, given by URL, into a byte array.
+ *
* @param urlString file URL
* @return file content
* @throws IOException on any exception thrown
*/
- public static byte[] readURL(String urlString) throws IOException {
- URL url = new URL(urlString);
- InputStream in = new BufferedInputStream(url.openStream());
- byte[] content = StreamUtils.readStream(in);
+ public static byte[] readURL(String urlString) throws IOException {
+ final URL url = new URL(urlString);
+ final InputStream in = new BufferedInputStream(url.openStream());
+ final byte[] content = StreamUtils.readStream(in);
in.close();
return content;
}
+
/**
* Reads a file, given by URL, into a String.
+ *
* @param urlString file URL
- * @param encoding character encoding
+ * @param encoding character encoding
* @return file content
* @throws IOException on any exception thrown
*/
public static String readURL(String urlString, String encoding) throws IOException {
- byte[] content = readURL(urlString);
+ final byte[] content = readURL(urlString);
return new String(content, encoding);
}
+
/**
* Reads a file, given by filename, into a byte array.
+ *
* @param filename filename
* @return file content
* @throws IOException on any exception thrown
*/
public static byte[] readFile(String filename) throws IOException {
- BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
- byte[] content = StreamUtils.readStream(in);
+ final BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
+ final byte[] content = StreamUtils.readStream(in);
in.close();
return content;
}
+
/**
* Reads a file, given by filename, into a String.
+ *
* @param filename filename
* @param encoding character encoding
* @return file content
* @throws IOException on any exception thrown
*/
public static String readFile(String filename, String encoding) throws IOException {
- byte[] content = readFile(filename);
+ final byte[] content = readFile(filename);
return new String(content, encoding);
}
+
/**
* Reads a file from a resource.
+ *
* @param name resource name
* @return file content as a byte array
* @throws IOException on any exception thrown
*/
public static byte[] readResource(String name) throws IOException {
- ClassLoader cl = FileUtils.class.getClassLoader();
- BufferedInputStream in = new BufferedInputStream(cl.getResourceAsStream(name));
- byte[] content = StreamUtils.readStream(in);
+ final ClassLoader cl = FileUtils.class.getClassLoader();
+ final BufferedInputStream in = new BufferedInputStream(cl.getResourceAsStream(name));
+ final byte[] content = StreamUtils.readStream(in);
in.close();
return content;
}
+
/**
* Reads a file from a resource.
- * @param name filename
+ *
+ * @param name filename
* @param encoding character encoding
* @return file content
* @throws IOException on any exception thrown
*/
public static String readResource(String name, String encoding) throws IOException {
- byte[] content = readResource(name);
+ final byte[] content = readResource(name);
return new String(content, encoding);
}
-
- /**
- * Returns the absolute URL of a given url which is relative to the parameter root
- * @param url
- * @param root
- * @return String
- */
- public static String makeAbsoluteURL(String url, String root) {
- //if url is relative to rootConfigFileDirName make it absolute
-
+
+ /**
+ * Returns the absolute URL of a given url which is relative to the parameter
+ * root
+ *
+ * @param url
+ * @param root
+ * @return String
+ */
+ public static String makeAbsoluteURL(String url, String root) {
+ // if url is relative to rootConfigFileDirName make it absolute
+
File keyFile;
String newURL = url;
- if(null == url) return null;
-
- if (url.startsWith("http:/") || url.startsWith("https:/") || url.startsWith("file:/") || url.startsWith("ftp:/")) {
- return url;
+ if (null == url) {
+ return null;
+ }
+
+ if (url.startsWith("http:/") || url.startsWith("https:/") || url.startsWith("file:/") || url.startsWith(
+ "ftp:/")) {
+ return url;
} else {
// check if absolute - if not make it absolute
keyFile = new File(url);
if (!keyFile.isAbsolute()) {
keyFile = new File(root, url);
- if (keyFile.toString().startsWith("file:"))
- newURL = keyFile.toString();
-
- else
- newURL = keyFile.toURI().toString();
-
+ if (keyFile.toString().startsWith("file:")) {
+ newURL = keyFile.toString();
+ } else {
+ newURL = keyFile.toURI().toString();
+ }
+
}
return newURL;
}
- }
-
-
- private static void copy( InputStream fis, OutputStream fos )
- {
- try
- {
- byte[] buffer = new byte[ 0xFFFF ];
- for ( int len; (len = fis.read(buffer)) != -1; )
- fos.write( buffer, 0, len );
- }
- catch( IOException e ) {
- System.err.println( e );
- }
- finally {
- if ( fis != null )
- try { fis.close(); } catch ( IOException e ) { e.printStackTrace(); }
- if ( fos != null )
- try { fos.close(); } catch ( IOException e ) { e.printStackTrace(); }
- }
- }
-
- public static void copyFile(File src, File dest)
- {
- try
- {
- copy( new FileInputStream( src ), new FileOutputStream( dest ) );
- }
- catch( IOException e ) {
- e.printStackTrace();
- }
- }
-
+ }
+
+ private static void copy(InputStream fis, OutputStream fos) {
+ try {
+ final byte[] buffer = new byte[0xFFFF];
+ for (int len; (len = fis.read(buffer)) != -1;) {
+ fos.write(buffer, 0, len);
+ }
+ } catch (final IOException e) {
+ System.err.println(e);
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ public static void copyFile(File src, File dest) {
+ try {
+ copy(new FileInputStream(src), new FileOutputStream(dest));
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
+ }
+
}