FileReader.java

1
package com.renomad.minum.utils;
2
3
import com.renomad.minum.logging.ILogger;
4
5
import java.io.ByteArrayOutputStream;
6
import java.io.IOException;
7
import java.io.RandomAccessFile;
8
import java.nio.ByteBuffer;
9
import java.nio.channels.FileChannel;
10
import java.nio.file.Files;
11
import java.nio.file.Path;
12
import java.util.Map;
13
14
import static com.renomad.minum.utils.FileUtils.checkForBadFilePatterns;
15
16
/**
17
 * Reads files from disk, optionally storing into a LRU cache.
18
 */
19
public final class FileReader implements IFileReader {
20
21
    private final Map<String, byte[]> lruCache;
22
    private final boolean useCacheForStaticFiles;
23
    private final ILogger logger;
24
25
    public FileReader(Map<String, byte[]> lruCache, boolean useCacheForStaticFiles, ILogger logger) {
26
        this.lruCache = lruCache;
27
        this.useCacheForStaticFiles = useCacheForStaticFiles;
28
        this.logger = logger;
29
    }
30
31
    @Override
32
    public byte[] readFile(String path) throws IOException {
33 2 1. readFile : negated conditional → KILLED
2. readFile : negated conditional → KILLED
        if (useCacheForStaticFiles && lruCache.containsKey(path)) {
34 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
            return lruCache.get(path);
35
        }
36
37
        try {
38 1 1. readFile : removed call to com/renomad/minum/utils/FileUtils::checkForBadFilePatterns → KILLED
            checkForBadFilePatterns(path);
39
        } catch (Exception ex) {
40
            logger.logDebug(() -> String.format("Bad path requested at readFile: %s. Exception: %s", path, ex.getMessage()));
41 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
            return new byte[0];
42
        }
43
44 1 1. readFile : negated conditional → KILLED
        if (!Files.exists(Path.of(path))) {
45
            logger.logDebug(() -> String.format("No file found at %s, returning an empty byte array", path));
46 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
            return new byte[0];
47
        }
48
49 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
        return readTheFile(path, logger, useCacheForStaticFiles, lruCache);
50
    }
51
52
    static byte[] readTheFile(String path, ILogger logger, boolean useCacheForStaticFiles, Map<String, byte[]> lruCache) throws IOException {
53
        try (RandomAccessFile reader = new RandomAccessFile(path, "r");
54
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
55
            FileChannel channel = reader.getChannel();
56
            int bufferSize = 8 * 1024;
57 2 1. readTheFile : negated conditional → TIMED_OUT
2. readTheFile : changed conditional boundary → KILLED
            if (bufferSize > channel.size()) {
58
                bufferSize = (int) channel.size();
59
            }
60
            ByteBuffer buff = ByteBuffer.allocate(bufferSize);
61
62 2 1. readTheFile : changed conditional boundary → TIMED_OUT
2. readTheFile : negated conditional → KILLED
            while (channel.read(buff) > 0) {
63 1 1. readTheFile : removed call to java/io/ByteArrayOutputStream::write → KILLED
                out.write(buff.array(), 0, buff.position());
64
                buff.clear();
65
            }
66
67
            byte[] bytes = out.toByteArray();
68 1 1. readTheFile : negated conditional → KILLED
            if (bytes.length == 0) {
69
                logger.logTrace(() -> path + " filesize was 0, returning empty byte array");
70
                return new byte[0];
71
            } else {
72
                String s = path + " filesize was " + bytes.length + " bytes.";
73
                logger.logTrace(() -> s);
74
75
                if (useCacheForStaticFiles) {
76
                    logger.logDebug(() -> "Storing " + path + " in the cache");
77
                    lruCache.put(path, bytes);
78
                }
79
                return bytes;
80
            }
81
        }
82
    }
83
84
}

Mutations

33

1.1
Location : readFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_readStaticFile_JS(com.renomad.minum.web.WebFrameworkTests)
negated conditional → KILLED

2.2
Location : readFile
Killed by : com.renomad.minum.utils.FileReaderTests.test_ReadFile_InCache(com.renomad.minum.utils.FileReaderTests)
negated conditional → KILLED

34

1.1
Location : readFile
Killed by : com.renomad.minum.utils.FileReaderTests.test_ReadFile_InCache(com.renomad.minum.utils.FileReaderTests)
replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED

38

1.1
Location : readFile
Killed by : com.renomad.minum.utils.FileReaderTests.test_ReadFile_BadPath(com.renomad.minum.utils.FileReaderTests)
removed call to com/renomad/minum/utils/FileUtils::checkForBadFilePatterns → KILLED

41

1.1
Location : readFile
Killed by : com.renomad.minum.utils.FileReaderTests.test_ReadFile_BadPath(com.renomad.minum.utils.FileReaderTests)
replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED

44

1.1
Location : readFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_readStaticFile_JS(com.renomad.minum.web.WebFrameworkTests)
negated conditional → KILLED

46

1.1
Location : readFile
Killed by : com.renomad.minum.utils.FileUtilsTests
replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED

49

1.1
Location : readFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_readStaticFile_JS(com.renomad.minum.web.WebFrameworkTests)
replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED

57

1.1
Location : readTheFile
Killed by : none
negated conditional → TIMED_OUT

2.2
Location : readTheFile
Killed by : com.renomad.minum.FunctionalTests
changed conditional boundary → KILLED

62

1.1
Location : readTheFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_readStaticFile_JS(com.renomad.minum.web.WebFrameworkTests)
negated conditional → KILLED

2.2
Location : readTheFile
Killed by : none
changed conditional boundary → TIMED_OUT

63

1.1
Location : readTheFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_readStaticFile_JS(com.renomad.minum.web.WebFrameworkTests)
removed call to java/io/ByteArrayOutputStream::write → KILLED

68

1.1
Location : readTheFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_readStaticFile_JS(com.renomad.minum.web.WebFrameworkTests)
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0