]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/FileCacher.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / load / FileCacher.java
1 package tim.prune.load;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.ArrayList;
8
9 /**
10  * Class to load the contents of a file
11  * into an array for later retrieval
12  */
13 public class FileCacher
14 {
15         /** File to cache */
16         private File _file = null;
17         /** Array to hold lines of file */
18         private String[] _contentArray = null;
19
20
21         /**
22          * Constructor
23          * @param inFile File object to cache
24          */
25         public FileCacher(File inFile)
26         {
27                 _file = inFile;
28                 loadFile();
29         }
30
31
32         /**
33          * Load the specified file into memory
34          */
35         private void loadFile()
36         {
37                 ArrayList<String> contentList = new ArrayList<String>();
38                 if (_file != null && _file.exists() && _file.canRead())
39                 {
40                         BufferedReader reader = null;
41                         try
42                         {
43                                 reader = new BufferedReader(new FileReader(_file));
44                                 String currLine = reader.readLine();
45                                 while (currLine != null)
46                                 {
47                                         if (currLine.trim().length() > 0)
48                                                 contentList.add(currLine);
49                                         currLine = reader.readLine();
50                                 }
51                         }
52                         catch (IOException ioe) {}
53                         finally
54                         {
55                                 // close file ignoring errors
56                                 try
57                                 {
58                                         if (reader != null) reader.close();
59                                 }
60                                 catch (Exception e) {}
61                         }
62                 }
63                 // Convert into String array for keeps
64                 int numLines = contentList.size();
65                 _contentArray = new String[numLines];
66                 for (int i=0; i<numLines; i++)
67                         _contentArray[i] = contentList.get(i);
68         }
69
70
71         /**
72          * @return Contents of the file as array of non-blank Strings
73          */
74         public String[] getContents()
75         {
76                 return _contentArray;
77         }
78
79
80         /**
81          * Get the top section of the file for preview
82          * @param inNumRows number of lines to extract
83          * @param inMaxWidth max length of Strings (longer ones will be chopped)
84          * @return String array containing non-blank lines from the file
85          */
86         public String[] getSnippet(int inNumRows, int inMaxWidth)
87         {
88                 final int MIN_SNIPPET_SIZE = 3;
89                 // Check size is within sensible limits
90                 int numToCopy = inNumRows;
91                 if (numToCopy > getNumLines()) numToCopy = getNumLines();
92                 int size = numToCopy;
93                 if (size < MIN_SNIPPET_SIZE) size = MIN_SNIPPET_SIZE;
94                 String[] result = new String[size];
95                 // Copy Strings across
96                 System.arraycopy(_contentArray, 0, result, 0, numToCopy);
97                 // Chop Strings to max width if necessary
98                 if (inMaxWidth > 10)
99                 {
100                         for (int i=0; i<size; i++)
101                         {
102                                 if (result[i] == null)
103                                         result[i] = "";
104                                 else
105                                 {
106                                         if (result[i].length() > inMaxWidth)
107                                                 result[i] = result[i].trim();
108                                         if (result[i].length() > inMaxWidth)
109                                                 result[i] = result[i].substring(0, inMaxWidth);
110                                 }
111                         }
112                 }
113                 return result;
114         }
115
116         /**
117          * @return the number of non-blank lines in the file
118          */
119         public int getNumLines()
120         {
121                 return _contentArray.length;
122         }
123
124
125         /**
126          * Clear the memory
127          */
128         public void clear()
129         {
130                 _file = null;
131                 _contentArray = null;
132         }
133 }