*/
public class FileCacher
{
+ /** File to cache */
private File _file = null;
+ /** Array to hold lines of file */
private String[] _contentArray = null;
*/
private void loadFile()
{
- ArrayList contentList = new ArrayList();
+ ArrayList<String> contentList = new ArrayList<String>();
if (_file != null && _file.exists() && _file.canRead())
{
BufferedReader reader = null;
{
reader = new BufferedReader(new FileReader(_file));
String currLine = reader.readLine();
+ if (currLine != null && currLine.startsWith("<?xml")) {
+ return; // it's an xml file, it shouldn't use this cacher
+ }
while (currLine != null)
{
+ if (currLine.indexOf('\0') >= 0)
+ {
+ try {reader.close();} catch (IOException ioe2) {}
+ return; // it's a binary file, shouldn't use this cacher
+ }
if (currLine.trim().length() > 0)
contentList.add(currLine);
currLine = reader.readLine();
int numLines = contentList.size();
_contentArray = new String[numLines];
for (int i=0; i<numLines; i++)
- _contentArray[i] = contentList.get(i).toString();
+ _contentArray[i] = contentList.get(i);
}
/**
* Get the top section of the file for preview
- * @param inSize number of lines to extract
+ * @param inNumRows number of lines to extract
+ * @param inMaxWidth max length of Strings (longer ones will be chopped)
* @return String array containing non-blank lines from the file
*/
public String[] getSnippet(int inNumRows, int inMaxWidth)
{
for (int i=0; i<size; i++)
{
- if (result[i].length() > inMaxWidth)
- result[i] = result[i].trim();
- if (result[i].length() > inMaxWidth)
- result[i] = result[i].substring(0, inMaxWidth);
+ if (result[i] == null)
+ result[i] = "";
+ else
+ {
+ if (result[i].length() > inMaxWidth)
+ result[i] = result[i].trim();
+ if (result[i].length() > inMaxWidth)
+ result[i] = result[i].substring(0, inMaxWidth);
+ }
}
}
return result;