[Japi-cvs] SF.net SVN: japi: [518] tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-07-07 13:03:23
|
Revision: 518 http://svn.sourceforge.net/japi/?rev=518&view=rev Author: christianhujer Date: 2007-07-07 06:03:22 -0700 (Sat, 07 Jul 2007) Log Message: ----------- Improved JWGet to use ArgParser and better I/O exception handling. Modified Paths: -------------- tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java Modified: tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java =================================================================== --- tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java 2007-07-07 12:57:48 UTC (rev 517) +++ tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java 2007-07-07 13:03:22 UTC (rev 518) @@ -24,16 +24,17 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.util.List; +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; import org.jetbrains.annotations.NotNull; /** WGet implementation in Java. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> - * @todo use argparser */ -public class JWGet { +public class JWGet extends BasicCommand { /** Size of the I/O buffer. */ private static final int BUF_SIZE = 8192; @@ -42,8 +43,13 @@ * @param args command line arguments */ public static void main(@NotNull final String... args) { + ArgParser.simpleParseAndRun(new JWGet(), args); + } + + /** {@inheritDoc} */ + public int run(@NotNull final List<String> args) throws Exception { int returnCode = 0; - if (args.length == 0) { + if (args.size() == 0) { System.err.println("Usage: JWGet url..."); returnCode++; } @@ -51,31 +57,29 @@ for (final String arg : args) { try { final URL url = new URL(arg); - OutputStream out = null; - InputStream in = null; + final URLConnection con = url.openConnection(); + final InputStream in = con.getInputStream(); try { - final URLConnection con = url.openConnection(); - in = con.getInputStream(); final String location = con.getHeaderField("Content-Location"); final String outputFilename = new File((location != null ? new URL(url, location) : url).getFile()).getName(); System.err.println(outputFilename); - out = new FileOutputStream(outputFilename); - for (int bytesRead; (bytesRead = in.read(buf)) != -1; ) { - out.write(buf, 0, bytesRead); + final OutputStream out = new FileOutputStream(outputFilename); + try { + for (int bytesRead; (bytesRead = in.read(buf)) != -1; ) { + out.write(buf, 0, bytesRead); + } + } finally { + out.close(); } - } catch (final IOException e) { - System.err.println(e); - returnCode++; } finally { - try { in.close(); } catch (final Exception ignore) { /* ignore */ } - try { out.close(); } catch (final Exception ignore) { /* ignore */ } + in.close(); } - } catch (final MalformedURLException e) { + } catch (final IOException e) { System.err.println(e); returnCode++; } } - System.exit(returnCode); + return returnCode; } } // class JWGet This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |