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,7 +127,9 @@ public class ImageObject {
Object arg = args[0];
InputStream in = null;
ImageInfo info = new ImageInfo();
Object ret = null;
try {
if (arg instanceof Wrapper) {
arg = ((Wrapper) arg).unwrap();
}
@ -137,42 +139,24 @@ public class ImageObject {
} 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 {
in = new FileInputStream((File) arg);
} catch (FileNotFoundException fnf) {
return null;
}
} else if (arg instanceof FileObject) {
try {
in = new FileInputStream(((FileObject)arg).getFile());
} catch (FileNotFoundException fnf) {
return null;
}
} else if (arg instanceof String) {
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) {
try {
URL url = new URL(str);
in = url.openStream();
} catch (MalformedURLException mux) {
in = new FileInputStream(str);
} catch (IOException iox) {
return null;
}
} else {
in = new FileInputStream((String) arg);
}
} catch (FileNotFoundException fnf) {
return null;
in = new FileInputStream(str);
}
}
@ -184,10 +168,20 @@ public class ImageObject {
info.setInput(in);
if (info.check()) {
return Context.toObject(info, scope);
ret = Context.toObject(info, scope);
}
return null;
} catch (IOException e) {
// do nothing, returns null later
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ee) {
}
}
}
return ret;
}
}
}

View file

@ -448,7 +448,7 @@ public abstract class AbstractServletClient extends HttpServlet {
InputStream in = cx.getResourceAsStream(forward);
if (in == null)
throw new IOException("Can't read "+path);
try {
OutputStream out = res.getOutputStream();
int bufferSize = 4096;
@ -467,6 +467,9 @@ public abstract class AbstractServletClient extends HttpServlet {
length -= l;
out.write(buffer, 0, l);
}
} finally {
in.close();
}
}
FileUpload getUpload(HttpServletRequest request, String encoding) throws Exception {
@ -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
if (uri.endsWith("/"))
pathbuffer.append('/');
if (uri.endsWith("/"))
pathbuffer.append('/');