aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/util/Files.java
blob: 164f9d22fecee81d00ab8681c56396938a54f150 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.stream.Stream;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class Files {
    public static Stream<File> allDescendantFiles(File file) {
        if (file.isFile()) {
            return Stream.of(file);
        } else if (file.isDirectory()) {
            return Stream.of(file.listFiles()).flatMap(Files::allDescendantFiles);
        } else {
            return Stream.empty();
        }
    }

    public static <T> T withFileOutputStream(File file, ThrowingFunction<FileOutputStream, T> f) {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            return f.apply(fos);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}