Thread: [Japi-cvs] SF.net SVN: japi: [471] tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-06-30 11:46:06
|
Revision: 471 http://svn.sourceforge.net/japi/?rev=471&view=rev Author: christianhujer Date: 2007-06-30 04:46:02 -0700 (Sat, 30 Jun 2007) Log Message: ----------- Minor improvements: * Added missing @Nullable / @NotNull annotations. * Introduced constant for buffer size. * Made code (for-loop) more readable / maintainable. 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-06-30 11:45:02 UTC (rev 470) +++ tools/jwget/trunk/src/net/sf/japi/jwget/JWGet.java 2007-06-30 11:46:02 UTC (rev 471) @@ -27,6 +27,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import org.jetbrains.annotations.NotNull; /** WGet implementation in Java. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> @@ -34,16 +35,19 @@ */ public class JWGet { + /** Size of the I/O buffer. */ + private static final int BUF_SIZE = 8192; + /** Main program. * @param args command line arguments */ - public static void main(final String... args) { + public static void main(@NotNull final String... args) { int returnCode = 0; if (args.length == 0) { System.err.println("Usage: JWGet url..."); returnCode++; } - final byte[] buf = new byte[8192]; + final byte[] buf = new byte[BUF_SIZE]; for (final String arg : args) { try { final URL url = new URL(arg); @@ -56,7 +60,9 @@ 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)); + for (int bytesRead; (bytesRead = in.read(buf)) != -1; ) { + out.write(buf, 0, bytesRead); + } } catch (final IOException e) { System.err.println(e); returnCode++; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |