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