From 5bac67209997ee9e08a990780def06c78879c822 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Tue, 9 Jun 2009 09:42:01 +0000 Subject: [PATCH] added help methods for reading text files from disk --- .../java/se/sics/cooja/util/StringUtils.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tools/cooja/java/se/sics/cooja/util/StringUtils.java b/tools/cooja/java/se/sics/cooja/util/StringUtils.java index 44afa88de..41688c1b6 100644 --- a/tools/cooja/java/se/sics/cooja/util/StringUtils.java +++ b/tools/cooja/java/se/sics/cooja/util/StringUtils.java @@ -24,11 +24,17 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: StringUtils.java,v 1.1 2009/04/20 17:14:19 nifi Exp $ + * $Id: StringUtils.java,v 1.2 2009/06/09 09:42:01 fros4943 Exp $ */ package se.sics.cooja.util; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; + /** * Some utility methods for generating hex dumps. * @@ -107,4 +113,43 @@ public class StringUtils { return sb.toString(); } + public static String loadFromURL(URL url) { + if (url == null) { + return null; + } + try { + InputStreamReader reader = new InputStreamReader(url.openStream()); + StringBuilder sb = new StringBuilder(); + char[] buf = new char[4096]; + int read; + while ((read = reader.read(buf)) > 0) { + sb.append(buf, 0, read); + }; + + reader.close(); + return sb.toString(); + } catch (IOException e) { + return null; + } + } + + public static String loadFromFile(File scriptFile) { + if (scriptFile == null) { + return null; + } + try { + FileReader reader = new FileReader(scriptFile); + StringBuilder sb = new StringBuilder(); + char[] buf = new char[4096]; + int read; + while ((read = reader.read(buf)) > 0) { + sb.append(buf, 0, read); + }; + + reader.close(); + return sb.toString(); + } catch (IOException e) { + return null; + } + } }