Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** A function that extracts the filename from an URL (taken from the Mozilla project).
- * Examine the code and explain whether the code represents an "error checking" solution,
- * a Design by Contract solution, or neither.
- */
- public class DBCdemo
- {
- // function that extracts the filename from a url
- public static String getFileNameFromUrl(String urlstr)
- {
- if (urlstr.length() == 0)
- {
- return null;
- }
- // For "http://foo/bar/cheese.jpg", return "cheese.jpg".
- // For "imap://[email protected]:143/fetch>UID>/INBOX>951?part=1.2&type=image/gif&filename=foo.jpeg", return "foo.jpeg".
- // The 2nd url (ie, "imap://...") is generated for inline images by MimeInlineImage_parse_begin() in mimeiimg.cpp.
- String tail = urlstr.substring(urlstr.lastIndexOf("/")+1);
- if (tail.length()>0)
- {
- int nameIndex = tail.lastIndexOf( "filename=" );
- if (nameIndex != -1)
- {
- return (tail.substring(nameIndex+9));
- }
- else
- {
- return tail;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement