Add a convenience method to write CSR to an OutputStream

pull/17/merge
Richard Körber 2016-03-06 16:53:39 +01:00
parent ebbec52a6e
commit 7213eede8c
2 changed files with 22 additions and 1 deletions

View File

@ -14,6 +14,8 @@
package org.shredzone.acme4j.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.security.KeyPair;
import java.security.PrivateKey;
@ -189,7 +191,7 @@ public class CSRBuilder {
}
/**
* Writes the signed certificate request to a file.
* Writes the signed certificate request to a {@link Writer}.
*
* @param w
* {@link Writer} to write the PEM file to
@ -204,6 +206,16 @@ public class CSRBuilder {
}
}
/**
* Writes the signed certificate request to an {@link OutputStream}.
*
* @param out
* {@link OutputStream} to write the PEM file to
*/
public void write(OutputStream out) throws IOException {
write(new OutputStreamWriter(out, "utf-8"));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();

View File

@ -16,6 +16,7 @@ package org.shredzone.acme4j.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
@ -180,6 +181,14 @@ public class CSRBuilderTest {
// Verify that both keypairs are the same
assertThat(builder.getCSR(), not(sameInstance(readCsr)));
assertThat(builder.getEncoded(), is(equalTo(readCsr.getEncoded())));
// OutputStream is identical?
byte[] pemBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
builder.write(baos);
pemBytes = baos.toByteArray();
}
assertThat(new String(pemBytes, "utf-8"), is(equalTo(pem)));
}
/**