|
1 | 1 | package com.unixtools.command.networking;
|
2 | 2 |
|
3 |
| -public class WgetCommand { |
4 |
| - |
| 3 | +import com.unixtools.core.Command; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URI; |
| 8 | +import java.net.URL; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class WgetCommand implements Command { |
| 13 | + private static final String VALID_FLAGS = "Oq"; |
| 14 | + private String outputFileName = null; |
| 15 | + private boolean quietMode = false; |
| 16 | + |
| 17 | + @Override |
| 18 | + public void execute(String[] args) { |
| 19 | + List<String> flags = new ArrayList<>(); |
| 20 | + List<String> urls = new ArrayList<>(); |
| 21 | + parseArguments(args, flags, urls); |
| 22 | + |
| 23 | + if (!areFlagsValid(flags)) { |
| 24 | + System.out.println("Invalid flags. Valid flags are: " + VALID_FLAGS); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (urls.isEmpty()) { |
| 29 | + System.out.println("wget: URL not specified."); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (urls.size() > 1) { |
| 34 | + System.out.println("wget: Only one URL can be specified."); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + String url = urls.get(0); |
| 39 | + downloadFile(url); |
| 40 | + } |
| 41 | + |
| 42 | + private void parseArguments(String[] args, List<String> flags, List<String> urls) { |
| 43 | + for (int i = 0; i < args.length; i++) { |
| 44 | + if (args[i].startsWith("-")) { |
| 45 | + for (char flag : args[i].substring(1).toCharArray()) { |
| 46 | + if (flag == 'O' && i + 1 < args.length) { |
| 47 | + outputFileName = args[++i]; |
| 48 | + } else if (flag == 'q') { |
| 49 | + quietMode = true; |
| 50 | + } else { |
| 51 | + flags.add(String.valueOf(flag)); |
| 52 | + } |
| 53 | + } |
| 54 | + } else { |
| 55 | + urls.add(args[i]); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private boolean areFlagsValid(List<String> flags) { |
| 61 | + for (String flag : flags) { |
| 62 | + if (!VALID_FLAGS.contains(flag)) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + private void downloadFile(String urlString) { |
| 70 | + try { |
| 71 | + URL url = URI.create(urlString).toURL(); |
| 72 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 73 | + try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream()); |
| 74 | + OutputStream out = determineOutputStream()) { |
| 75 | + byte[] buffer = new byte[1024]; |
| 76 | + int count; |
| 77 | + while ((count = in.read(buffer)) != -1) { |
| 78 | + out.write(buffer, 0, count); |
| 79 | + } |
| 80 | + if (!quietMode) { |
| 81 | + System.out.println("\nDownload completed: " + urlString); |
| 82 | + } |
| 83 | + } |
| 84 | + } catch (IOException e) { |
| 85 | + System.out.println("Error downloading file: " + e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private OutputStream determineOutputStream() throws FileNotFoundException { |
| 90 | + if (outputFileName != null) { |
| 91 | + return new FileOutputStream(outputFileName); |
| 92 | + } else { |
| 93 | + return System.out; |
| 94 | + } |
| 95 | + } |
5 | 96 | }
|
0 commit comments