aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/docx/InMemoryDocxFile.java
blob: 3c6d4b4cf1d07cbcf5e091e3a33eacc65738aa4e (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
45
46
package org.zwobble.mammoth.docx;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class InMemoryDocxFile implements DocxFile {
    public static DocxFile fromStream(InputStream stream) throws IOException {
        ZipInputStream zipStream = new ZipInputStream(stream);
        ImmutableMap.Builder<String, byte[]> entries = ImmutableMap.builder();
        ZipEntry entry;
        while ((entry = zipStream.getNextEntry()) != null) {
            entries.put(entry.getName(), ByteStreams.toByteArray(zipStream));
        }
        return new InMemoryDocxFile(entries.build());
    }

    public static DocxFile fromStrings(Map<String, String> entries) {
        return new InMemoryDocxFile(Maps.transformValues(entries, value -> value.getBytes(StandardCharsets.UTF_8)));
    }

    private final Map<String, byte[]> entries;

    public InMemoryDocxFile(Map<String, byte[]> entries) {
        this.entries = entries;
    }

    @Override
    public Optional<InputStream> tryGetInputStream(String name) throws IOException {
        return Optional.ofNullable(entries.get(name))
            .map(ByteArrayInputStream::new);
    }

    @Override
    public void close() throws IOException {
    }
}