* @return <code>List</code> of <code>FilePart</code> items
*/
public static List<File> split(File splitDir, File inputFile, long maxBytes, boolean skipHeader) {
// validations
if(splitDir == null) {
throw new DataUtilException("The split directory parameter can not be a null value");
} else if(!splitDir.exists()) {
throw new DataUtilException("The split directory is not a valid path");
}
if(inputFile == null) {
throw new DataUtilException("The input file parameter can not be a null value");
} else if(!inputFile.exists()) {
throw new DataUtilException("The input file doesn't exist");
}
if(maxBytes < DEFAULT_MIN_BYTES) {
throw new DataUtilException("Invalid max bytes specification: " + maxBytes + ", minimum is: "
+ DEFAULT_MIN_BYTES);
}
ArrayList<File> splitFiles = new ArrayList<File>();
// open file
FileInputStream inputStream = null;
LinkedList<String> rows = new LinkedList<String>();
try {
// create FilePart
inputStream = new FileInputStream(inputFile);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
long charsTotal = 0;
String line;
int maxChunk = 60;
int rowNumber = 0;
while((line=br.readLine()) != null) {
rowNumber += 1;
if(skipHeader && rowNumber == 1) {
continue;
}
if(storageCalculation(rows.size(), line.length(), charsTotal) > maxBytes) {
if(maxChunk-- <= 0) {
break;
}
// we have to stop or we may exceed max part size
File splitFile = writePartToFile(splitDir, rows);
splitFiles.add(splitFile);
rows.clear();
charsTotal = 0;
}
rows.add(line);
charsTotal += line.length() + DataUtilDefaults.lineTerminator.length();
}
// no more data, finish up the final part
if(charsTotal > 0) {
File splitFile = writePartToFile(splitDir, rows);
splitFiles.add(splitFile);
}
inputStream.close();
inputStream = null;
} catch (FileNotFoundException e) {
throw new DataUtilException(e);
} catch (UnsupportedEncodingException e) {
throw new DataUtilException(e);
} catch (IOException e) {
throw new DataUtilException(e);
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {