1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-17 17:51:08 +01:00

Workaround for #159: Avoid to prevent swallowing IOExceptions

This commit is contained in:
Paul Schaub 2021-07-26 16:19:30 +02:00
parent 10bb033e40
commit fc311fe781
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
25 changed files with 216 additions and 72 deletions

View file

@ -32,7 +32,6 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.key.OpenPgpV4Fingerprint;
@ -143,7 +142,7 @@ public class ArmorUtils {
public static String toAsciiArmoredString(InputStream inputStream, MultiMap<String, String> additionalHeaderValues) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armor = toAsciiArmoredStream(out, additionalHeaderValues);
Streams.pipeAll(inputStream, armor);
StreamUtil.pipeAll(inputStream, armor);
armor.close();
return out.toString();

View file

@ -0,0 +1,47 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamUtil {
/**
* Pipe all data from the given {@link InputStream} to the given {@link OutputStream}.
*
* This utility method is required, since {@link org.bouncycastle.util.io.Streams#pipeAll(InputStream, OutputStream)}
* internally uses {@link InputStream#read(byte[], int, int)} which silently swallows {@link IOException IOExceptions}.
*
* @see <a href="https://github.com/pgpainless/pgpainless/issues/159#issuecomment-886694555">Explanation</a>
* @see <a href="https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/io/InputStream.java#L286">
* InputStream swallowing IOExceptions</a>
*
* @param inputStream input stream
* @param outputStream output stream
* @throws IOException io exceptions
*/
public static void pipeAll(InputStream inputStream, OutputStream outputStream) throws IOException {
do {
int i = inputStream.read();
if (i == -1) {
break;
}
outputStream.write(i);
} while (true);
}
}