if (args.length != 1) {
throw new IllegalArgumentException("Image.getInfo() expects one argument");
}
Object arg = args[0];
InputStream in = null;
ImageInfo info = new ImageInfo();
Object ret = null;
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 MimePart) {
in = new ByteArrayInputStream(((MimePart) arg).getContent());
} else if (arg instanceof File) {
in = new FileInputStream((File) arg);
} else if (arg instanceof FileObject) {
in = new FileInputStream(((FileObject)arg).getFile());
} 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.
if (str.indexOf(":") > -1) {
try {
URL url = new URL(str);
in = url.openStream();
} catch (MalformedURLException mux) {
in = new FileInputStream(str);
}
} else {
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) {
}
}
}
return ret;