1616import java .util .regex .Pattern ;
1717
1818import static java .lang .String .format ;
19-
19+ /**
20+ * Utility class for working with mapped files, specifically dealing with parsing the "/proc/self/maps" file on Linux systems.
21+ * This provides information about virtual memory mappings.
22+ * <p>
23+ * NOTE: This utility is designed to work on Linux or Linux-like environments.
24+ */
2025public enum MappedFileUtil {
2126 ;
27+
2228 private static final Logger LOGGER = LoggerFactory .getLogger (MappedFileUtil .class );
2329 private static final Path PROC_SELF_MAPS = Paths .get ("/proc/self/maps" );
2430
25- // See https://man7.org/linux/man-pages/man5/proc.5.html
31+ // Regular expression pattern to parse lines from /proc/self/maps.
2632 private static final Pattern LINE_PATTERN = Pattern .compile ("([\\ p{XDigit}\\ -]+)\\ s+([rwxsp\\ -]+)\\ s+(\\ p{XDigit}+)\\ s+(\\ p{XDigit}+:\\ p{XDigit}+)\\ s+(\\ d+)(?:\\ s+(.*))?" );
33+ // Index constants for the parsed groups.
2734 private static final int ADDRESS_INDEX = 1 ;
2835 private static final int PERMS_INDEX = 2 ;
2936 private static final int OFFSET_INDEX = 3 ;
@@ -32,9 +39,7 @@ public enum MappedFileUtil {
3239 private static final int PATH_INDEX = 6 ;
3340
3441 /**
35- * Get the distinct files that are currently mapped to the current process
36- * <p>
37- * NOTE: this is only likely to work on linux or linux-like environments
42+ * Gets the distinct files that are currently mapped to the current process.
3843 *
3944 * @return A set of the distinct files listed in /proc/self/maps
4045 * @throws UnsupportedOperationException if /proc/self/maps does not exist or can't be read
@@ -56,6 +61,7 @@ public static Set<String> getAllMappedFiles() {
5661 }
5762 }
5863
64+ // Internal method to process each line from /proc/self/maps
5965 private static void processProcSelfMaps (Set <String > fileList , BufferedReader reader ) throws IOException {
6066 String line ;
6167 while ((line = reader .readLine ()) != null ) {
@@ -68,6 +74,7 @@ private static void processProcSelfMaps(Set<String> fileList, BufferedReader rea
6874 }
6975 }
7076
77+ // Processes a single line and adds the file to the list if applicable
7178 private static void processOneLine (Set <String > fileList , Matcher matcher ) {
7279 String filename = getPath (matcher );
7380 if (filename == null ) {
@@ -80,15 +87,33 @@ private static void processOneLine(Set<String> fileList, Matcher matcher) {
8087 }
8188 }
8289
90+ /**
91+ * Parses the provided line using the pattern defined for parsing lines from "/proc/self/maps".
92+ *
93+ * @param line The line to be parsed
94+ * @return A Matcher object after matching the pattern
95+ */
8396 public static Matcher parseMapsLine (String line ) {
8497 return LINE_PATTERN .matcher (line );
8598 }
8699
100+ /**
101+ * Gets the path (if any) from the provided Matcher object.
102+ *
103+ * @param matcher Matcher object containing the parsed line
104+ * @return The path if available, null otherwise
105+ */
87106 @ Nullable
88107 public static String getPath (Matcher matcher ) {
89108 return matcher .group (PATH_INDEX );
90109 }
91110
111+ /**
112+ * Gets the memory address range from the provided Matcher object.
113+ *
114+ * @param matcher Matcher object containing the parsed line
115+ * @return The address range as a string
116+ */
92117 public static String getAddress (Matcher matcher ) {
93118 return matcher .group (ADDRESS_INDEX );
94119 }
0 commit comments