read .gz files

This commit is contained in:
Fredrik Osterlind 2011-11-01 09:35:09 +01:00
parent ac39443f9c
commit 3154d73e24

View File

@ -30,12 +30,13 @@
package se.sics.cooja.util; package se.sics.cooja.util;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.URL; import java.net.URL;
import java.util.zip.GZIPInputStream;
/** /**
* Some utility methods for generating hex dumps. * Some utility methods for generating hex dumps.
@ -139,13 +140,20 @@ public class StringUtils {
} }
} }
public static String loadFromFile(File scriptFile) { public static String loadFromFile(File file) {
if (scriptFile == null) { if (file == null) {
return null; return null;
} }
StringBuilder sb = new StringBuilder();
InputStreamReader reader = null;
try { try {
FileReader reader = new FileReader(scriptFile); if (file.getName().endsWith(".gz")) {
StringBuilder sb = new StringBuilder(); reader = new InputStreamReader(new GZIPInputStream(new FileInputStream(file)));
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
char[] buf = new char[4096]; char[] buf = new char[4096];
int read; int read;
while ((read = reader.read(buf)) > 0) { while ((read = reader.read(buf)) > 0) {
@ -155,6 +163,18 @@ public class StringUtils {
reader.close(); reader.close();
return sb.toString(); return sb.toString();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
if (sb != null && sb.length() > 0) {
return sb.toString();
}
return null; return null;
} }
} }