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.
This commit is contained in:
parent
1aa4fe75c9
commit
1336e51ba0
3 changed files with 39 additions and 17 deletions
|
@ -74,18 +74,25 @@ public class FileResource implements Resource {
|
|||
return file.lastModified();
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
try {
|
||||
public String getContent(String encoding) throws IOException {
|
||||
InputStream in = getInputStream();
|
||||
byte[] byteBuffer = new byte[in.available()];
|
||||
|
||||
in.read(byteBuffer);
|
||||
in.close();
|
||||
|
||||
return new String(byteBuffer);
|
||||
} catch (Exception ignore) {
|
||||
return "";
|
||||
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() {
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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,11 +106,19 @@ 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 {
|
||||
if (zipfile != null) {
|
||||
zipfile.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getContent() throws IOException {
|
||||
return getContent(null);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
|
Loading…
Add table
Reference in a new issue