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