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

pgpainless / sop-java / #17

15 Nov 2023 01:23PM UTC coverage: 65.932% (-3.9%) from 69.789%
#17

push

other

vanitasvitae
Fix gradle version

1167 of 1770 relevant lines covered (65.93%)

0.66 hits per line

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

0.0
/external-sop/src/main/java/sop/external/operation/InlineDetachExternal.java
1
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package sop.external.operation;
6

7
import sop.ReadyWithResult;
8
import sop.Signatures;
9
import sop.exception.SOPGPException;
10
import sop.external.ExternalSOP;
11
import sop.operation.InlineDetach;
12

13
import javax.annotation.Nonnull;
14
import java.io.ByteArrayOutputStream;
15
import java.io.File;
16
import java.io.FileInputStream;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.io.OutputStream;
20
import java.util.ArrayList;
21
import java.util.List;
22
import java.util.Properties;
23

24
/**
25
 * Implementation of the {@link InlineDetach} operation using an external SOP binary.
26
 */
27
public class InlineDetachExternal implements InlineDetach {
28

29
    private final ExternalSOP.TempDirProvider tempDirProvider;
30
    private final List<String> commandList = new ArrayList<>();
×
31
    private final List<String> envList;
32

33
    public InlineDetachExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
×
34
        this.tempDirProvider = tempDirProvider;
×
35
        commandList.add(binary);
×
36
        commandList.add("inline-detach");
×
37
        envList = ExternalSOP.propertiesToEnv(environment);
×
38
    }
×
39

40
    @Override
41
    @Nonnull
42
    public InlineDetach noArmor() {
43
        commandList.add("--no-armor");
×
44
        return this;
×
45
    }
46

47
    @Override
48
    @Nonnull
49
    public ReadyWithResult<Signatures> message(@Nonnull InputStream messageInputStream) throws IOException, SOPGPException.BadData {
50
        File tempDir = tempDirProvider.provideTempDirectory();
×
51

52
        File signaturesOut = new File(tempDir, "signatures");
×
53
        signaturesOut.delete();
×
54
        commandList.add("--signatures-out=" + signaturesOut.getAbsolutePath());
×
55

56
        String[] command = commandList.toArray(new String[0]);
×
57
        String[] env = envList.toArray(new String[0]);
×
58

59
        try {
60
            Process process = Runtime.getRuntime().exec(command, env);
×
61
            OutputStream processOut = process.getOutputStream();
×
62
            InputStream processIn = process.getInputStream();
×
63

64
            return new ReadyWithResult<Signatures>() {
×
65
                @Override
66
                public Signatures writeTo(@Nonnull OutputStream outputStream) throws IOException {
67
                    byte[] buf = new byte[4096];
×
68
                    int r;
69
                    while ((r = messageInputStream.read(buf)) > 0) {
×
70
                        processOut.write(buf, 0, r);
×
71
                    }
72

73
                    messageInputStream.close();
×
74
                    processOut.close();
×
75

76
                    while ((r = processIn.read(buf)) > 0) {
×
77
                        outputStream.write(buf, 0 , r);
×
78
                    }
79

80
                    processIn.close();
×
81
                    outputStream.close();
×
82

83
                    ExternalSOP.finish(process);
×
84

85
                    FileInputStream signaturesOutIn = new FileInputStream(signaturesOut);
×
86
                    ByteArrayOutputStream signaturesBuffer = new ByteArrayOutputStream();
×
87
                    while ((r = signaturesOutIn.read(buf)) > 0) {
×
88
                        signaturesBuffer.write(buf, 0, r);
×
89
                    }
90
                    signaturesOutIn.close();
×
91
                    signaturesOut.delete();
×
92

93
                    final byte[] sigBytes = signaturesBuffer.toByteArray();
×
94
                    return new Signatures() {
×
95
                        @Override
96
                        public void writeTo(@Nonnull OutputStream signatureOutputStream) throws IOException {
97
                            signatureOutputStream.write(sigBytes);
×
98
                        }
×
99
                    };
100
                }
101
            };
102
        } catch (IOException e) {
×
103
            throw new RuntimeException(e);
×
104
        }
105
    }
106
}
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

© 2025 Coveralls, Inc