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