use of com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException in project ats-framework by Axway.
the class LocalFileSystemOperations method findTextInFileAfterGivenPosition.
@Override
public FileMatchInfo findTextInFileAfterGivenPosition(String fileName, String[] searchTexts, boolean isRegex, long searchFromPosition, int currentLineNumber) {
File targetFile = new File(fileName);
if (!targetFile.exists()) {
throw new FileDoesNotExistException(fileName);
}
if (searchFromPosition < 0l) {
searchFromPosition = 0l;
} else if (searchFromPosition > targetFile.length()) {
throw new FileSystemOperationException("The file '" + fileName + "' has size(" + targetFile.length() + " bytes) lower than the starting position for search (" + searchFromPosition + " byte). It is possible that file size had been cut since previous inspection but this is not supported.");
}
if (currentLineNumber <= 0) {
currentLineNumber = 1;
}
int matches = 0;
RandomAccessFile raf = null;
try {
List<Pattern> searchPatterns = null;
if (isRegex) {
searchPatterns = new ArrayList<Pattern>();
for (String searchText : searchTexts) {
searchPatterns.add(Pattern.compile(searchText, Pattern.DOTALL));
}
}
raf = new RandomAccessFile(targetFile, "r");
raf.seek(searchFromPosition);
List<String> matchedLines = new ArrayList<String>();
List<String> matchedPatterns = new ArrayList<String>();
List<Integer> matchedLineNumbers = new ArrayList<Integer>();
String line = null;
long lastLineByteOffset = searchFromPosition;
while ((line = IoUtils.readLineWithEOL(raf)) != null) {
for (int i = 0; i < searchTexts.length; i++) {
if ((isRegex && searchPatterns.get(i).matcher(line).matches()) || (!isRegex && line.contains(searchTexts[i]))) {
matches++;
matchedLines.add(line.trim());
matchedPatterns.add(searchTexts[i]);
matchedLineNumbers.add(currentLineNumber);
lastLineByteOffset = raf.getFilePointer();
break;
}
}
if (line.endsWith("\n") || line.endsWith("\r")) {
currentLineNumber++;
lastLineByteOffset = raf.getFilePointer();
}
}
return new FileMatchInfo(matches, currentLineNumber, lastLineByteOffset, matchedLines.toArray(new String[matchedLines.size()]), matchedLineNumbers.toArray(new Integer[matchedLines.size()]), matchedPatterns.toArray(new String[matchedPatterns.size()]));
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not read file '" + fileName + "' seeking from byte " + searchFromPosition, ioe);
} finally {
IoUtils.closeStream(raf);
}
}
use of com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException in project ats-framework by Axway.
the class LocalFileSystemOperations method computeMd5Sum.
@Override
public String computeMd5Sum(String sourceFile, Md5SumMode mode) {
InputStream input = null;
MessageDigest digest;
try {
input = new BufferedInputStream(new FileInputStream(sourceFile));
//obtain MD5 digest
digest = MessageDigest.getInstance("MD5");
switch(mode) {
case BINARY:
{
digestBinContent(input, digest);
break;
}
case ASCII:
{
digestNormContent(input, digest);
break;
}
default:
{
throw new FileSystemOperationException("MD5 digest mode '" + mode + "' not supported");
}
}
} catch (FileNotFoundException fnfe) {
throw new FileDoesNotExistException(sourceFile);
} catch (NoSuchAlgorithmException nsae) {
throw new FileSystemOperationException("MD5 sum cannot be calculated", nsae);
} catch (IOException ioe) {
throw new FileSystemOperationException("Could read content of file '" + sourceFile + "'", ioe);
} finally {
IoUtils.closeStream(input, "Could not close stream");
}
//retrieve final MD5 value and convert to hex
return StringUtils.byteArray2Hex(digest.digest());
}
Aggregations