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.badFilePathPatterns;
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 1 1. readFile : negated conditional → KILLED
        if (badFilePathPatterns.matcher(path).find()) {
38
            logger.logDebug(() -> String.format("Bad path requested at readFile: %s", path));
39 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
            return new byte[0];
40
        }
41
42 1 1. readFile : negated conditional → KILLED
        if (!Files.exists(Path.of(path))) {
43
            logger.logDebug(() -> String.format("No file found at %s, returning an empty byte array", path));
44 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
            return new byte[0];
45
        }
46
47 1 1. readFile : replaced return value with null for com/renomad/minum/utils/FileReader::readFile → KILLED
        return readTheFile(path, logger, useCacheForStaticFiles, lruCache);
48
    }
49
50
    static byte[] readTheFile(String path, ILogger logger, boolean useCacheForStaticFiles, Map<String, byte[]> lruCache) throws IOException {
51
        try (RandomAccessFile reader = new RandomAccessFile(path, "r");
52
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
53
            FileChannel channel = reader.getChannel();
54
            int bufferSize = 8 * 1024;
55 2 1. readTheFile : changed conditional boundary → SURVIVED
2. readTheFile : negated conditional → KILLED
            if (bufferSize > channel.size()) {
56
                bufferSize = (int) channel.size();
57
            }
58
            ByteBuffer buff = ByteBuffer.allocate(bufferSize);
59
60 2 1. readTheFile : changed conditional boundary → TIMED_OUT
2. readTheFile : negated conditional → KILLED
            while (channel.read(buff) > 0) {
61 1 1. readTheFile : removed call to java/io/ByteArrayOutputStream::write → KILLED
                out.write(buff.array(), 0, buff.position());
62
                buff.clear();
63
            }
64
65
            byte[] bytes = out.toByteArray();
66 1 1. readTheFile : negated conditional → KILLED
            if (bytes.length == 0) {
67
                logger.logTrace(() -> path + " filesize was 0, returning empty byte array");
68
                return new byte[0];
69
            } else {
70
                String s = path + " filesize was " + bytes.length + " bytes.";
71
                logger.logTrace(() -> s);
72
73
                if (useCacheForStaticFiles) {
74
                    logger.logDebug(() -> "Storing " + path + " in the cache");
75
                    lruCache.put(path, bytes);
76
                }
77
                return bytes;
78
            }
79
        }
80
    }
81
82
}

Mutations

33

1.1
Location : readFile
Killed by : com.renomad.minum.web.WebFrameworkTests.test_Edge_ApplicationOctetStream(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

37

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

39

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

42

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

44

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

47

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

55

1.1
Location : readTheFile
Killed by : com.renomad.minum.FunctionalTests
negated conditional → KILLED

2.2
Location : readTheFile
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

60

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

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

61

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

66

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

Active mutators

Tests examined


Report generated by PIT 1.17.0