Patch to close InputStreams contributed by Jürg Lehni.

This commit is contained in:
hns 2004-09-14 13:29:08 +00:00
parent f90f174719
commit 426a0d0c9d
2 changed files with 66 additions and 65 deletions

View file

@ -127,67 +127,61 @@ public class ImageObject {
Object arg = args[0]; Object arg = args[0];
InputStream in = null; InputStream in = null;
ImageInfo info = new ImageInfo(); ImageInfo info = new ImageInfo();
Object ret = null;
if (arg instanceof Wrapper) {
arg = ((Wrapper) arg).unwrap(); try {
} if (arg instanceof Wrapper) {
arg = ((Wrapper) arg).unwrap();
if (arg instanceof InputStream) {
in = (InputStream) arg;
} else if (arg instanceof byte[]) {
in = new ByteArrayInputStream((byte[]) arg);
} else if (arg instanceof File) {
try {
in = new FileInputStream((File) arg);
} catch (FileNotFoundException fnf) {
return null;
} }
} else if (arg instanceof File) {
try { if (arg instanceof InputStream) {
in = (InputStream) arg;
} else if (arg instanceof byte[]) {
in = new ByteArrayInputStream((byte[]) arg);
} else if (arg instanceof File) {
in = new FileInputStream((File) arg); in = new FileInputStream((File) arg);
} catch (FileNotFoundException fnf) { } else if (arg instanceof File) {
return null; in = new FileInputStream((File) arg);
} } else if (arg instanceof FileObject) {
} else if (arg instanceof FileObject) {
try {
in = new FileInputStream(((FileObject)arg).getFile()); in = new FileInputStream(((FileObject)arg).getFile());
} catch (FileNotFoundException fnf) { } else if (arg instanceof String) {
return null; String str = (String) arg;
} // try to interpret argument as URL if it contains a colon,
} else if (arg instanceof String) { // otherwise or if URL is malformed interpret as file name.
String str = (String) arg;
// try to interpret argument as URL if it contains a colon,
// otherwise or if URL is malformed interpret as file name.
try {
if (str.indexOf(":") > -1) { if (str.indexOf(":") > -1) {
try { try {
URL url = new URL(str); URL url = new URL(str);
in = url.openStream(); in = url.openStream();
} catch (MalformedURLException mux) { } catch (MalformedURLException mux) {
in = new FileInputStream(str); in = new FileInputStream(str);
} catch (IOException iox) {
return null;
} }
} else { } else {
in = new FileInputStream((String) arg); in = new FileInputStream(str);
}
}
if (in == null) {
String msg = "Unrecognized argument in Image.getInfo(): ";
msg += arg == null ? "null" : arg.getClass().toString();
throw new IllegalArgumentException(msg);
}
info.setInput(in);
if (info.check()) {
ret = Context.toObject(info, scope);
}
} catch (IOException e) {
// do nothing, returns null later
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ee) {
} }
} catch (FileNotFoundException fnf) {
return null;
} }
} }
return ret;
if (in == null) {
String msg = "Unrecognized argument in Image.getInfo(): ";
msg += arg == null ? "null" : arg.getClass().toString();
throw new IllegalArgumentException(msg);
}
info.setInput(in);
if (info.check()) {
return Context.toObject(info, scope);
}
return null;
} }
} }
} }

View file

@ -448,24 +448,27 @@ public abstract class AbstractServletClient extends HttpServlet {
InputStream in = cx.getResourceAsStream(forward); InputStream in = cx.getResourceAsStream(forward);
if (in == null) if (in == null)
throw new IOException("Can't read "+path); throw new IOException("Can't read "+path);
try {
OutputStream out = res.getOutputStream(); OutputStream out = res.getOutputStream();
int bufferSize = 4096; int bufferSize = 4096;
byte buffer[] = new byte[bufferSize]; byte buffer[] = new byte[bufferSize];
int l = bufferSize; int l = bufferSize;
while (length>0) { while (length>0) {
if (length < bufferSize) if (length < bufferSize)
l = in.read(buffer, 0, length); l = in.read(buffer, 0, length);
else else
l=in.read(buffer, 0, bufferSize); l=in.read(buffer, 0, bufferSize);
if (l == -1) if (l == -1)
break; break;
length -= l; length -= l;
out.write(buffer, 0, l); out.write(buffer, 0, l);
}
} finally {
in.close();
} }
} }
@ -738,6 +741,10 @@ public abstract class AbstractServletClient extends HttpServlet {
} }
// append trailing "/" if it is contained in original URI // append trailing "/" if it is contained in original URI
// append trailing "/" if it is contained in original URI
if (uri.endsWith("/"))
pathbuffer.append('/');
if (uri.endsWith("/")) if (uri.endsWith("/"))
pathbuffer.append('/'); pathbuffer.append('/');