From 3154d73e245ef5ca382360635578bda56e3b5906 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Tue, 1 Nov 2011 09:35:09 +0100 Subject: [PATCH] read .gz files --- .../java/se/sics/cooja/util/StringUtils.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/util/StringUtils.java b/tools/cooja/java/se/sics/cooja/util/StringUtils.java index 86c1e726b..8500bf225 100644 --- a/tools/cooja/java/se/sics/cooja/util/StringUtils.java +++ b/tools/cooja/java/se/sics/cooja/util/StringUtils.java @@ -30,12 +30,13 @@ package se.sics.cooja.util; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; +import java.util.zip.GZIPInputStream; /** * Some utility methods for generating hex dumps. @@ -139,13 +140,20 @@ public class StringUtils { } } - public static String loadFromFile(File scriptFile) { - if (scriptFile == null) { + public static String loadFromFile(File file) { + if (file == null) { return null; } + StringBuilder sb = new StringBuilder(); + InputStreamReader reader = null; + try { - FileReader reader = new FileReader(scriptFile); - StringBuilder sb = new StringBuilder(); + if (file.getName().endsWith(".gz")) { + reader = new InputStreamReader(new GZIPInputStream(new FileInputStream(file))); + } else { + reader = new InputStreamReader(new FileInputStream(file)); + } + char[] buf = new char[4096]; int read; while ((read = reader.read(buf)) > 0) { @@ -155,6 +163,18 @@ public class StringUtils { reader.close(); return sb.toString(); } 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; } }