main class file code fixes
This commit is contained in:
		
							parent
							
								
									60c578b8bf
								
							
						
					
					
						commit
						4ba6832845
					
				| @ -1,18 +1,19 @@ | |||||||
| import java.util.Objects; | import java.util.Objects; | ||||||
|  | import java.lang.Exception; | ||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||||
| import java.util.Scanner; | import java.util.Scanner; | ||||||
| import java.io.FileWriter; |  | ||||||
| 
 | 
 | ||||||
| import java.net.URL; | import java.net.URL; | ||||||
| import java.io.BufferedInputStream; | import java.io.BufferedInputStream; | ||||||
|  | import java.io.FileOutputStream; | ||||||
| 
 | 
 | ||||||
| public class Main { | public class Main { | ||||||
|     private boolean disable_networking = false; |  | ||||||
| 
 | 
 | ||||||
|     public void main(String[] args) { |     public static void main(String[] args) { | ||||||
|         boolean help_enqueued = false; |         boolean help_enqueued = false; | ||||||
|  |         boolean allow_networking = true; | ||||||
|         int verbosity = 1; |         int verbosity = 1; | ||||||
|          |          | ||||||
|         int p = 0; |         int p = 0; | ||||||
| @ -20,13 +21,14 @@ public class Main { | |||||||
|             try { |             try { | ||||||
|                 if (Objects.equals(args[p], "-h")) {     |                 if (Objects.equals(args[p], "-h")) {     | ||||||
|                     help_enqueued = true; |                     help_enqueued = true; | ||||||
|  |                     p += 1; | ||||||
|                 } |                 } | ||||||
|                 else if (Objects.equals(args[p], "-v")) { |                 else if (Objects.equals(args[p], "-v")) { | ||||||
|                     verbosity = Integer.parseInt(args[p+1]); |                     verbosity = Integer.parseInt(args[p+1]); | ||||||
|                     p += 2; |                     p += 2; | ||||||
|                 } |                 } | ||||||
|                 else if (Objects.equals(args[p], "--offline")) { |                 else if (Objects.equals(args[p], "--offline")) { | ||||||
|                     disable_networking = true; |                     allow_networking = false; | ||||||
|                     p += 2; |                     p += 2; | ||||||
|                 } |                 } | ||||||
|                 else { |                 else { | ||||||
| @ -42,48 +44,53 @@ public class Main { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (help_enqueued) { |         if (help_enqueued) { | ||||||
|             printHelp(); |             printHelp(allow_networking); | ||||||
|             System.exit(0); |             System.exit(0); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public fetchResource(String remote_url, String output_filename) { |     private static void fetchResource(String remote_url, String output_filename) { | ||||||
|  |         try { | ||||||
|             BufferedInputStream in = new BufferedInputStream(new URL(remote_url).openStream()); |             BufferedInputStream in = new BufferedInputStream(new URL(remote_url).openStream()); | ||||||
|         FileOutputStream fileOutputStream = new FileOutputStream() |             FileOutputStream fileOutputStream = new FileOutputStream(output_filename); | ||||||
|      |      | ||||||
|             byte dataBuffer[] = new byte[1024]; |             byte dataBuffer[] = new byte[1024]; | ||||||
|             int bytesRead; |             int bytesRead; | ||||||
|             while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { |             while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { | ||||||
|                 fileOutputStream.write(dataBuffer, 0, bytesRead); |                 fileOutputStream.write(dataBuffer, 0, bytesRead); | ||||||
|             } |             } | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public printHelp() { |     private static void printHelp(boolean allow_net) { | ||||||
|         String help_text = ""; |  | ||||||
| 
 |  | ||||||
|         try { |         try { | ||||||
|             File help_file = new File("src/help.txt"); |             File help_file = new File("src/help.txt"); | ||||||
|             Scanner help_file_scanner = new Scanner(help_file); |  | ||||||
| 
 | 
 | ||||||
|         } catch (FileNotFoundException e) { |             if (!help_file.exists()) { | ||||||
|             System.err.println("[WARN] Help file missing."); |                 System.err.println("[WARN] Help file is missing."); | ||||||
|             if (!disable_networking) { |                 if (allow_net) { | ||||||
|                     System.err.println("[INFO] Trying to recover it from the git server"); |                     System.err.println("[INFO] Trying to recover it from the git server"); | ||||||
|                 fetchResource("http://139.162.162.130:3000/", "src/help.txt"); |                     fetchResource("http://139.162.162.130:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt"); | ||||||
|                 File help_file = new File("src/help.txt"); |  | ||||||
|                 Scanner help_file_scanner = new Scanner(help_file); |  | ||||||
|                 } else { |                 } else { | ||||||
|                 System.err.println("[INFO] --offline flag is set, not recovering"); |                     System.err.println("[INFO] Networking is disabled, not recovering"); | ||||||
|                     System.exit(1); |                     System.exit(1); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|      |      | ||||||
|  |             Scanner help_file_scanner = new Scanner(help_file); | ||||||
|  |             String help_text = ""; | ||||||
|  |      | ||||||
|             while (help_file_scanner.hasNextLine()) { |             while (help_file_scanner.hasNextLine()) { | ||||||
|                 help_text += help_file_scanner.nextLine(); |                 help_text += help_file_scanner.nextLine(); | ||||||
|             } |             } | ||||||
|      |      | ||||||
|             help_file_scanner.close();  |             help_file_scanner.close();  | ||||||
|                      |  | ||||||
|             System.out.println(help_text); |             System.out.println(help_text); | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             System.out.println("[ERROR] Failed to read help due to the following exception: " + e); | ||||||
|  |             System.exit(1); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user