|
1 | 1 | package com.unixtools.command.filecontent;
|
2 | 2 |
|
3 |
| -public class HeadCommand { |
4 |
| - |
| 3 | +import com.unixtools.core.Command; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class HeadCommand implements Command { |
| 12 | + private static final String VALID_FLAGS = "nc"; |
| 13 | + private int numberOfLines = 10; |
| 14 | + private int numberOfBytes = -1; |
| 15 | + private boolean isBytesFlagUsed = false; |
| 16 | + |
| 17 | + @Override |
| 18 | + public void execute(String[] args) { |
| 19 | + List<String> flags = new ArrayList<>(); |
| 20 | + List<String> filePaths = new ArrayList<>(); |
| 21 | + parseArguments(args, flags, filePaths); |
| 22 | + |
| 23 | + if (!areFlagsValid(flags)) { |
| 24 | + System.out.println("Invalid flags. Valid flags are: " + VALID_FLAGS); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (filePaths.isEmpty()) { |
| 29 | + System.out.println("head: No file specified."); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + for (String filePath : filePaths) { |
| 34 | + if (filePaths.size() > 1) { |
| 35 | + System.out.println("==> " + filePath + " <=="); |
| 36 | + } |
| 37 | + displayFileContents(normalizePath(filePath)); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private void parseArguments(String[] args, List<String> flags, List<String> paths) { |
| 42 | + for (int i = 0; i < args.length; i++) { |
| 43 | + if (args[i].startsWith("-")) { |
| 44 | + switch (args[i]) { |
| 45 | + case "-n": |
| 46 | + numberOfLines = Integer.parseInt(args[++i]); |
| 47 | + break; |
| 48 | + case "-c": |
| 49 | + isBytesFlagUsed = true; |
| 50 | + numberOfBytes = Integer.parseInt(args[++i]); |
| 51 | + break; |
| 52 | + default: |
| 53 | + flags.add(args[i]); |
| 54 | + break; |
| 55 | + } |
| 56 | + } else { |
| 57 | + paths.add(args[i]); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private boolean areFlagsValid(List<String> flags) { |
| 63 | + return flags.stream().allMatch(flag -> VALID_FLAGS.contains(flag.replace("-", ""))); |
| 64 | + } |
| 65 | + |
| 66 | + private void displayFileContents(String filePath) { |
| 67 | + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { |
| 68 | + if (isBytesFlagUsed) { |
| 69 | + readBytes(reader, numberOfBytes); |
| 70 | + } else { |
| 71 | + readLines(reader, numberOfLines); |
| 72 | + } |
| 73 | + } catch (IOException e) { |
| 74 | + System.out.println("Error reading file: " + e.getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void readLines(BufferedReader reader, int numLines) throws IOException { |
| 79 | + for (int i = 0; i < numLines; i++) { |
| 80 | + String line = reader.readLine(); |
| 81 | + if (line == null) { |
| 82 | + break; |
| 83 | + } |
| 84 | + System.out.println(line); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void readBytes(BufferedReader reader, int numBytes) throws IOException { |
| 89 | + int remainingBytes = numBytes; |
| 90 | + int ch; |
| 91 | + while (remainingBytes > 0 && (ch = reader.read()) != -1) { |
| 92 | + System.out.print((char) ch); |
| 93 | + remainingBytes--; |
| 94 | + } |
| 95 | + System.out.println(); |
| 96 | + } |
| 97 | + |
| 98 | + private String normalizePath(String path) { |
| 99 | + String normalizedPath = path.replace("\\", "/"); |
| 100 | + Path pathObj = Paths.get(normalizedPath).normalize(); |
| 101 | + return pathObj.toString(); |
| 102 | + } |
5 | 103 | }
|
0 commit comments