mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-08 17:59:43 +02:00
Delete ProxyOutputStream and test
This commit is contained in:
parent
cdcbae7e5f
commit
21766a1f39
2 changed files with 0 additions and 117 deletions
|
@ -1,77 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package sop.util
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream
|
|
||||||
import java.io.IOException
|
|
||||||
import java.io.OutputStream
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [OutputStream] that buffers data being written into it, until its underlying output stream is
|
|
||||||
* being replaced. At that point, first all the buffered data is being written to the underlying
|
|
||||||
* stream, followed by any successive data that may get written to the [ProxyOutputStream]. This
|
|
||||||
* class is useful if we need to provide an [OutputStream] at one point in time when the final
|
|
||||||
* target output stream is not yet known.
|
|
||||||
*/
|
|
||||||
@Deprecated("Marked for removal.")
|
|
||||||
// TODO: Remove in 11.X
|
|
||||||
class ProxyOutputStream : OutputStream() {
|
|
||||||
private val buffer = ByteArrayOutputStream()
|
|
||||||
private var swapped: OutputStream? = null
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
fun replaceOutputStream(underlying: OutputStream) {
|
|
||||||
this.swapped = underlying
|
|
||||||
swapped!!.write(buffer.toByteArray())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
@Throws(IOException::class)
|
|
||||||
override fun write(b: ByteArray) {
|
|
||||||
if (swapped == null) {
|
|
||||||
buffer.write(b)
|
|
||||||
} else {
|
|
||||||
swapped!!.write(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
@Throws(IOException::class)
|
|
||||||
override fun write(b: ByteArray, off: Int, len: Int) {
|
|
||||||
if (swapped == null) {
|
|
||||||
buffer.write(b, off, len)
|
|
||||||
} else {
|
|
||||||
swapped!!.write(b, off, len)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
@Throws(IOException::class)
|
|
||||||
override fun flush() {
|
|
||||||
buffer.flush()
|
|
||||||
if (swapped != null) {
|
|
||||||
swapped!!.flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
@Throws(IOException::class)
|
|
||||||
override fun close() {
|
|
||||||
buffer.close()
|
|
||||||
if (swapped != null) {
|
|
||||||
swapped!!.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
@Throws(IOException::class)
|
|
||||||
override fun write(i: Int) {
|
|
||||||
if (swapped == null) {
|
|
||||||
buffer.write(i)
|
|
||||||
} else {
|
|
||||||
swapped!!.write(i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package sop.util;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class ProxyOutputStreamTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void replaceOutputStreamThrowsNPEForNull() {
|
|
||||||
ProxyOutputStream proxy = new ProxyOutputStream();
|
|
||||||
assertThrows(NullPointerException.class, () -> proxy.replaceOutputStream(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSwappingStreamPreservesWrittenBytes() throws IOException {
|
|
||||||
byte[] firstSection = "Foo\nBar\n".getBytes(StandardCharsets.UTF_8);
|
|
||||||
byte[] secondSection = "Baz\n".getBytes(StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
ProxyOutputStream proxy = new ProxyOutputStream();
|
|
||||||
proxy.write(firstSection);
|
|
||||||
|
|
||||||
ByteArrayOutputStream swappedStream = new ByteArrayOutputStream();
|
|
||||||
proxy.replaceOutputStream(swappedStream);
|
|
||||||
|
|
||||||
proxy.write(secondSection);
|
|
||||||
proxy.close();
|
|
||||||
|
|
||||||
assertEquals("Foo\nBar\nBaz\n", swappedStream.toString());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue