mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 12:52:07 +01:00
Move File-based MultiPassStrategy from factory method into own class
This commit is contained in:
parent
1a1a387a16
commit
80a6baf0b1
2 changed files with 84 additions and 23 deletions
|
@ -17,16 +17,35 @@ package org.pgpainless.signature.cleartext_signatures;
|
|||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Interface that describes a strategy to deal with the fact that detached signatures require multiple passes
|
||||
* to do verification.
|
||||
*
|
||||
* This interface can be used to write the signed data stream out via {@link #getMessageOutputStream()} and later
|
||||
* get access to the data again via {@link #getMessageInputStream()}.
|
||||
* Thereby the detail where the data is being stored (memory, file, etc.) can be abstracted away.
|
||||
*/
|
||||
public interface MultiPassStrategy {
|
||||
|
||||
/**
|
||||
* Provide an {@link OutputStream} into which the signed data can be read into.
|
||||
*
|
||||
* @return output stream
|
||||
* @throws IOException io error
|
||||
*/
|
||||
OutputStream getMessageOutputStream() throws IOException;
|
||||
|
||||
/**
|
||||
* Provide an {@link InputStream} which contains the data that was previously written away in
|
||||
* {@link #getMessageOutputStream()}.
|
||||
*
|
||||
* @return input stream
|
||||
* @throws IOException io error
|
||||
*/
|
||||
InputStream getMessageInputStream() throws IOException;
|
||||
|
||||
/**
|
||||
|
@ -38,27 +57,7 @@ public interface MultiPassStrategy {
|
|||
* @return strategy
|
||||
*/
|
||||
static MultiPassStrategy writeMessageToFile(File file) {
|
||||
|
||||
return new MultiPassStrategy() {
|
||||
@Override
|
||||
public OutputStream getMessageOutputStream() throws IOException {
|
||||
if (!file.exists()) {
|
||||
boolean created = file.createNewFile();
|
||||
if (!created) {
|
||||
throw new IOException("New file '" + file.getAbsolutePath() + "' was not created.");
|
||||
}
|
||||
}
|
||||
return new FileOutputStream(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getMessageInputStream() throws IOException {
|
||||
if (!file.exists()) {
|
||||
throw new IOException("File '" + file.getAbsolutePath() + "' does no longer exist.");
|
||||
}
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
};
|
||||
return new WriteToFileMultiPassStrategy(file);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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.signature.cleartext_signatures;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* File-based multi-pass strategy.
|
||||
* When processing the signed data the first time, the data is being written out into a file.
|
||||
* For the second pass, that file is being read again.
|
||||
*/
|
||||
public class WriteToFileMultiPassStrategy implements MultiPassStrategy {
|
||||
|
||||
private final File file;
|
||||
|
||||
/**
|
||||
* Create a {@link MultiPassStrategy} which writes data to a file.
|
||||
* Note that {@link #getMessageOutputStream()} will create the file if necessary.
|
||||
*
|
||||
* @param file file to write the data to and read from
|
||||
*/
|
||||
public WriteToFileMultiPassStrategy(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getMessageOutputStream() throws IOException {
|
||||
if (!file.exists()) {
|
||||
boolean created = file.createNewFile();
|
||||
if (!created) {
|
||||
throw new IOException("New file '" + file.getAbsolutePath() + "' was not created.");
|
||||
}
|
||||
}
|
||||
return new FileOutputStream(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getMessageInputStream() throws IOException {
|
||||
if (!file.exists()) {
|
||||
throw new IOException("File '" + file.getAbsolutePath() + "' does no longer exist.");
|
||||
}
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue