]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/FileCacher.java
da354aac8b253895324e84b8a2b67d0ed74bbcb3
[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                                 if (currLine != null && currLine.startsWith("<?xml")) {
46                                         return; // it's an xml file, it shouldn't use this cacher
47                                 }
48                                 while (currLine != null)
49                                 {
50                                         if (currLine.indexOf('\0') >= 0) {
51                                                 return; // it's a binary file, shouldn't use this cacher
52                                         }
53                                         if (currLine.trim().length() > 0)
54                                                 contentList.add(currLine);
55                                         currLine = reader.readLine();
56                                 }
57                         }
58                         catch (IOException ioe) {}
59                         finally
60                         {
61                                 // close file ignoring errors
62                                 try
63                                 {
64                                         if (reader != null) reader.close();
65                                 }
66                                 catch (Exception e) {}
67                         }
68                 }
69                 // Convert into String array for keeps
70                 int numLines = contentList.size();
71                 _contentArray = new String[numLines];
72                 for (int i=0; i<numLines; i++)
73                         _contentArray[i] = contentList.get(i);
74         }
75
76
77         /**
78          * @return Contents of the file as array of non-blank Strings
79          */
80         public String[] getContents()
81         {
82                 return _contentArray;
83         }
84
85
86         /**
87          * Get the top section of the file for preview
88          * @param inNumRows number of lines to extract
89          * @param inMaxWidth max length of Strings (longer ones will be chopped)
90          * @return String array containing non-blank lines from the file
91          */
92         public String[] getSnippet(int inNumRows, int inMaxWidth)
93         {
94                 final int MIN_SNIPPET_SIZE = 3;
95                 // Check size is within sensible limits
96                 int numToCopy = inNumRows;
97                 if (numToCopy > getNumLines()) numToCopy = getNumLines();
98                 int size = numToCopy;
99                 if (size < MIN_SNIPPET_SIZE) size = MIN_SNIPPET_SIZE;
100                 String[] result = new String[size];
101                 // Copy Strings across
102                 System.arraycopy(_contentArray, 0, result, 0, numToCopy);
103                 // Chop Strings to max width if necessary
104                 if (inMaxWidth > 10)
105                 {
106                         for (int i=0; i<size; i++)
107                         {
108                                 if (result[i] == null)
109                                         result[i] = "";
110                                 else
111                                 {
112                                         if (result[i].length() > inMaxWidth)
113                                                 result[i] = result[i].trim();
114                                         if (result[i].length() > inMaxWidth)
115                                                 result[i] = result[i].substring(0, inMaxWidth);
116                                 }
117                         }
118                 }
119                 return result;
120         }
121
122         /**
123          * @return the number of non-blank lines in the file
124          */
125         public int getNumLines()
126         {
127                 return _contentArray.length;
128         }
129
130
131         /**
132          * Clear the memory
133          */
134         public void clear()
135         {
136                 _file = null;
137                 _contentArray = null;
138         }
139 }