Re: StringIndexOutOfBoundsException in File Downloads
Vadim Nasardinov <[email protected]> Tue, 8 Jun 2004 16:27:34 -0400
| Newsgroups | gmane.linux.redhat.ccm.general |
|---|---|
| Organization | Red Hat / Westford |
| Message-ID | <[email protected]> |
On Tuesday 08 June 2004 15:53, Srikanth Addala wrote:
> I think we are using cms version 5.1.2. I have attached the file to
> this mail. I think you have sent this file to one of my colleague
> (Steve) in Sept'03 to handle the file names properly.
I assume by "you" you mean the collective "you" as in "you Red Hat".
If you really do mean me personally, I can't recall what I was smoking
at the time.
The code in question falls short of perfection on a number of counts.
To my taste, some of the things that are wrong with it include the
following:
* The instance fields m_queryPart, m_fileNamePart, endingPart are
only used by the offending method stripFileName. As such, these
variables should be local to the method rather than being fields.
* Rather than traversing the URL backwards looking for '/' and '?',
it would be much clearer if you used String#lastIndexOf(char) to
find and extract the substring you are looking for.
* The use of StringBuffer#insert(0, ch) is terrible performance-wise.
> Can you please let me know if you see any thing that could be wrong.
I suggest you test this method in isolation like so:
$ cd /tmp
$ cat Main.java
public class Main {
private StringBuffer m_queryPart = new StringBuffer();
private StringBuffer m_fileNamePart = new StringBuffer();
private String endingPart;
public static void main(String[] args) {
new Main().run();
}
private void run() {
log("http://example.com/foo/bar/baz?foo=quuz&a=1");
log("http://example.com/foo/bar/?foo=quuz&a=1");
log("http://example.com/foo/bar/?foo=quuz&f=/tmp/bar");
}
private void log(String url) {
System.out.println(url);
System.out.print(" ");
System.out.println(stripFileName(url));
}
private String stripFileName(String url) {
endingPart = null;
m_queryPart.delete(0, m_queryPart.length());
m_fileNamePart.delete(0, m_queryPart.length());
for (int i = 1; i < url.length(); i++) {
char ch = url.charAt(url.length() - i);
// s_log.debug("ch: " + ch);
if (ch == '/') {
break;
}
m_queryPart.insert(0, ch);
if (ch =='?') {
for (int j = i; j < url.length(); j++) {
ch = url.charAt(url.length() - j);
if (ch == '/') {
break;
}
m_fileNamePart.insert(0, ch);
}
}
}
endingPart = m_fileNamePart.toString() + m_queryPart.toString();
url = url.substring(0, url.length() - endingPart.length());
return url;
}
}
$ javac Main.java
$ java -cp . Main
http://example.com/foo/bar/baz?foo=quuz&a=1
http://example.com/foo/
http://example.com/foo/bar/?foo=quuz&a=1
http://example.com/foo
http://example.com/foo/bar/?foo=quuz&f=/tmp/bar
http://example.com/foo/bar/?foo=quuz&f=
--
Redhat-ccm-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/redhat-ccm-list
Archives: https://www.redhat.com/pipermail/redhat-ccm-list/