Search in sources :

Example 1 with FileDoesNotExistException

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);
    }
}
Also used : FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) Pattern(java.util.regex.Pattern) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) FileMatchInfo(com.axway.ats.common.filesystem.FileMatchInfo) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 2 with FileDoesNotExistException

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());
}
Also used : FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) BufferedInputStream(java.io.BufferedInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Aggregations

FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)2 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)2 IOException (java.io.IOException)2 FileMatchInfo (com.axway.ats.common.filesystem.FileMatchInfo)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1