From 1336e51ba065f1f5d4d790656b30ec798ec3ce31 Mon Sep 17 00:00:00 2001 From: hns Date: Mon, 19 Dec 2005 22:15:11 +0000 Subject: [PATCH] Committing patch from Juerg Lehni: * Add String getContent(String encoding) method to Resource interface. * Propagate IOExceptions in FileResource.getContent(). * Make FileResource.getContent() more robust by not assuming all data can be read in one single blow. * Do lazy byte->char conversion in ZipResource.getContent() in the String constructor rather than a Reader. --- .../framework/repository/FileResource.java | 29 ++++++++++++------- src/helma/framework/repository/Resource.java | 9 +++++- .../framework/repository/ZipResource.java | 18 ++++++++---- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/helma/framework/repository/FileResource.java b/src/helma/framework/repository/FileResource.java index 01f3c8d0..fe88e613 100644 --- a/src/helma/framework/repository/FileResource.java +++ b/src/helma/framework/repository/FileResource.java @@ -74,18 +74,25 @@ public class FileResource implements Resource { return file.lastModified(); } - public String getContent() { - try { - InputStream in = getInputStream(); - byte[] byteBuffer = new byte[in.available()]; - - in.read(byteBuffer); - in.close(); - - return new String(byteBuffer); - } catch (Exception ignore) { - return ""; + public String getContent(String encoding) throws IOException { + InputStream in = getInputStream(); + int size = (int) file.length(); + byte[] buf = new byte[size]; + int read = 0; + while (read < size) { + int r = in.read(buf, read, size - read); + if (r == -1) + break; + read += r; } + in.close(); + return encoding == null ? + new String(buf) : + new String(buf, encoding); + } + + public String getContent() throws IOException { + return getContent(null); } public long getLength() { diff --git a/src/helma/framework/repository/Resource.java b/src/helma/framework/repository/Resource.java index b9f17ca2..a8973c0a 100644 --- a/src/helma/framework/repository/Resource.java +++ b/src/helma/framework/repository/Resource.java @@ -50,6 +50,13 @@ public interface Resource { */ public InputStream getInputStream() throws IOException; + /** + * Returns the content of the resource in a given encoding + * @param encoding + * @return content + */ + public String getContent(String encoding) throws IOException; + /** * Returns the content of the resource * @return content @@ -73,7 +80,7 @@ public interface Resource { /** * Returns the short name of the resource with the file extension * (everything following the last dot character) cut off. - * @return + * @return the file name without the file extension */ public String getBaseName(); diff --git a/src/helma/framework/repository/ZipResource.java b/src/helma/framework/repository/ZipResource.java index 34b35fb3..aee40afd 100644 --- a/src/helma/framework/repository/ZipResource.java +++ b/src/helma/framework/repository/ZipResource.java @@ -87,7 +87,7 @@ public final class ZipResource implements Resource { } } - public String getContent() throws IOException { + public String getContent(String encoding) throws IOException { ZipFile zipfile = null; try { zipfile = repository.getZipFile(); @@ -95,9 +95,9 @@ public final class ZipResource implements Resource { if (entry == null) { throw new IOException("Zip resource " + this + " does not exist"); } - InputStreamReader in = new InputStreamReader(zipfile.getInputStream(entry)); + InputStream in = zipfile.getInputStream(entry); int size = (int) entry.getSize(); - char[] buf = new char[size]; + byte[] buf = new byte[size]; int read = 0; while (read < size) { int r = in.read(buf, read, size-read); @@ -106,12 +106,20 @@ public final class ZipResource implements Resource { read += r; } in.close(); - return new String(buf); + return encoding == null ? + new String(buf) : + new String(buf, encoding); } finally { - zipfile.close(); + if (zipfile != null) { + zipfile.close(); + } } } + public String getContent() throws IOException { + return getContent(null); + } + public String getName() { return name; }