• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

link-intersystems / lis-commons / #304

21 Apr 2024 02:53PM UTC coverage: 90.065% (-0.007%) from 90.072%
#304

push

renelink
Added Directory and RegularFile facades for convenient file operations.

57 of 64 new or added lines in 4 files covered. (89.06%)

7769 of 8626 relevant lines covered (90.06%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/lis-commons-io/src/main/java/com/link_intersystems/io/file/RegularFile.java
1
package com.link_intersystems.io.file;
2

3
import com.link_intersystems.io.IOConsumer;
4

5
import java.io.File;
6
import java.io.IOException;
7
import java.io.OutputStreamWriter;
8
import java.io.PrintWriter;
9
import java.nio.charset.Charset;
10
import java.nio.file.Files;
11
import java.nio.file.Path;
12

13
import static java.nio.charset.StandardCharsets.UTF_8;
14
import static java.nio.file.StandardOpenOption.*;
15

16
public class RegularFile extends AbstractFile {
17

18

19
    /**
20
     * Creates a {@link RegularFile} based on the given {@link File}.
21
     */
22
    public RegularFile(File file) {
23
        this(file.toPath());
1✔
24
    }
1✔
25

26
    /**
27
     * Creates a {@link RegularFile} based on the given filepath.
28
     *
29
     * @param filepath the path of this {@link RegularFile}.
30
     */
31
    public RegularFile(Path filepath) {
32
        super(filepath);
1✔
33
    }
1✔
34

35
    /**
36
     * {@inheritDoc}
37
     * <p>
38
     * If the file is created it will be empty.
39
     * <p>
40
     * You do not have to call this method before any
41
     * {@link #write(IOConsumer, Charset)} or {@link #append(IOConsumer, Charset)}
42
     * invocation, because these methods will also create this file if it does not exist.
43
     *
44
     * @throws IOException
45
     */
46
    @Override
47
    public void create() throws IOException {
48
        append(IOConsumer.noop());
1✔
49
    }
1✔
50

51
    @Override
52
    public Directory getParent() {
NEW
53
        Path parent = getPath().getParent();
×
54

NEW
55
        if (parent != null) {
×
NEW
56
            return new Directory(parent);
×
57
        }
58

NEW
59
        return null;
×
60
    }
61

62
    /**
63
     * Writes the content provided by the {@link Appendable} to this {@link RegularFile} using {@link java.nio.charset.StandardCharsets#UTF_8}.
64
     *
65
     * @see #write(IOConsumer, Charset)
66
     */
67
    public void write(IOConsumer<Appendable> contentWriter) throws IOException {
68
        write(contentWriter, UTF_8);
1✔
69
    }
1✔
70

71
    /**
72
     * Writes the content provided by the {@link Appendable} to this {@link RegularFile} using the specified {@link Charset}.
73
     *
74
     * <ul>
75
     *     <li>If parent directories do not exist, they will be created.</li>
76
     *     <li>If the file does not exist, it will be created.</li>
77
     *     <li>If the file already exists, it will be overwritten.</li>
78
     *     <li>If the file is an existent directory, an {@link IOException} is raised.</li>
79
     * </ul>
80
     * <p>
81
     *  This method can also be used to create an empty file
82
     *
83
     *  <pre>
84
     *      RegularFile regularFile = ...;
85
     *      regularFile.write({@link IOConsumer#noop()});
86
     *  </pre>
87
     *
88
     * @param contentWriter an {@link Appendable} {@link IOConsumer} used to write the content of this file.
89
     * @throws IOException if the file is an existent directory or if the content could not be written.
90
     */
91
    public void write(IOConsumer<Appendable> contentWriter, Charset charset) throws IOException {
92
        ensureParentDirs();
1✔
93

94
        try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(getPath(), CREATE, TRUNCATE_EXISTING), charset))) {
1✔
95
            contentWriter.accept(pw);
1✔
96
        }
97
    }
1✔
98

99
    private void ensureParentDirs() throws IOException {
100
        Path filePath = getPath();
1✔
101
        Path parentPath = filePath.getParent();
1✔
102
        if (parentPath != null && !Files.exists(parentPath)) {
1✔
103
            Files.createDirectories(parentPath);
1✔
104
        }
105
    }
1✔
106

107
    public void append(IOConsumer<Appendable> contentWriter) throws IOException {
108
        append(contentWriter, UTF_8);
1✔
109
    }
1✔
110

111
    /**
112
     * Appends content provided by the {@link Appendable} to this {@link RegularFile} using the specified {@link Charset}.
113
     *
114
     * <ul>
115
     *     <li>If parent directories do not exist, they will be created.</li>
116
     *     <li>If the file does not exist, it will be created and the content will be appended.</li>
117
     *     <li>If the file already exists, the content will be appended.</li>
118
     *     <li>If the file is an existent directory an {@link IOException} is raised.</li>
119
     * </ul>
120
     *
121
     * @param contentWriter an {@link Appendable} {@link IOConsumer} used to append to the content of this file.
122
     * @throws IOException if the file is an existent directory or if the content could not be appended.
123
     */
124
    public void append(IOConsumer<Appendable> contentWriter, Charset charset) throws IOException {
125
        ensureParentDirs();
1✔
126

127
        try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(getPath(), CREATE, APPEND), charset))) {
1✔
128
            contentWriter.accept(pw);
1✔
129
        }
130
    }
1✔
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc