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();
|
return file.lastModified();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContent() {
|
public String getContent(String encoding) throws IOException {
|
||||||
try {
|
|
||||||
InputStream in = getInputStream();
|
InputStream in = getInputStream();
|
||||||
byte[] byteBuffer = new byte[in.available()];
|
int size = (int) file.length();
|
||||||
|
byte[] buf = new byte[size];
|
||||||
in.read(byteBuffer);
|
int read = 0;
|
||||||
in.close();
|
while (read < size) {
|
||||||
|
int r = in.read(buf, read, size - read);
|
||||||
return new String(byteBuffer);
|
if (r == -1)
|
||||||
} catch (Exception ignore) {
|
break;
|
||||||
return "";
|
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() {
|
public long getLength() {
|
||||||
|
|
|
@ -50,6 +50,13 @@ public interface Resource {
|
||||||
*/
|
*/
|
||||||
public InputStream getInputStream() throws IOException;
|
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
|
* Returns the content of the resource
|
||||||
* @return content
|
* @return content
|
||||||
|
@ -73,7 +80,7 @@ public interface Resource {
|
||||||
/**
|
/**
|
||||||
* Returns the short name of the resource with the file extension
|
* Returns the short name of the resource with the file extension
|
||||||
* (everything following the last dot character) cut off.
|
* (everything following the last dot character) cut off.
|
||||||
* @return
|
* @return the file name without the file extension
|
||||||
*/
|
*/
|
||||||
public String getBaseName();
|
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;
|
ZipFile zipfile = null;
|
||||||
try {
|
try {
|
||||||
zipfile = repository.getZipFile();
|
zipfile = repository.getZipFile();
|
||||||
|
@ -95,9 +95,9 @@ public final class ZipResource implements Resource {
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
throw new IOException("Zip resource " + this + " does not exist");
|
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();
|
int size = (int) entry.getSize();
|
||||||
char[] buf = new char[size];
|
byte[] buf = new byte[size];
|
||||||
int read = 0;
|
int read = 0;
|
||||||
while (read < size) {
|
while (read < size) {
|
||||||
int r = in.read(buf, read, size-read);
|
int r = in.read(buf, read, size-read);
|
||||||
|
@ -106,11 +106,19 @@ public final class ZipResource implements Resource {
|
||||||
read += r;
|
read += r;
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
return new String(buf);
|
return encoding == null ?
|
||||||
|
new String(buf) :
|
||||||
|
new String(buf, encoding);
|
||||||
} finally {
|
} finally {
|
||||||
|
if (zipfile != null) {
|
||||||
zipfile.close();
|
zipfile.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() throws IOException {
|
||||||
|
return getContent(null);
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
|
|
Loading…
Add table
Reference in a new issue