aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java
blob: 9e6ede7611e61a22596f45dd65da0d2b39de3e09 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package org.sufficientlysecure.keychain.testsupport;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

/**
 * Misc support functions. Would just use Guava / Apache Commons but
 * avoiding extra dependencies.
 */
public class TestDataUtil {
    public static byte[] readFully(InputStream input) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        appendToOutput(input, output);
        return output.toByteArray();
    }

    private static void appendToOutput(InputStream input, ByteArrayOutputStream output) {
        byte[] buffer = new byte[8192];
        int bytesRead;
        try {
            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] readAllFully(Collection<String> inputResources) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        for (String inputResource : inputResources) {
            appendToOutput(getResourceAsStream(inputResource), output);
        }
        return output.toByteArray();
    }


    public static InputStream getResourceAsStream(String resourceName) {
        return TestDataUtil.class.getResourceAsStream(resourceName);
    }
}