mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-11-26 09:02:06 +01:00
Kotlin conversion: ProxyOutputStream
This commit is contained in:
parent
b7007cc007
commit
05886228df
2 changed files with 75 additions and 80 deletions
|
@ -1,80 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2021 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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link 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 {@link ProxyOutputStream}.
|
|
||||||
* <p>
|
|
||||||
* This class is useful if we need to provide an {@link OutputStream} at one point in time when the final
|
|
||||||
* target output stream is not yet known.
|
|
||||||
*/
|
|
||||||
public class ProxyOutputStream extends OutputStream {
|
|
||||||
|
|
||||||
private final ByteArrayOutputStream buffer;
|
|
||||||
private OutputStream swapped;
|
|
||||||
|
|
||||||
public ProxyOutputStream() {
|
|
||||||
this.buffer = new ByteArrayOutputStream();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void replaceOutputStream(OutputStream underlying) throws IOException {
|
|
||||||
if (underlying == null) {
|
|
||||||
throw new NullPointerException("Underlying OutputStream cannot be null.");
|
|
||||||
}
|
|
||||||
this.swapped = underlying;
|
|
||||||
|
|
||||||
byte[] bufferBytes = buffer.toByteArray();
|
|
||||||
swapped.write(bufferBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized void write(byte[] b) throws IOException {
|
|
||||||
if (swapped == null) {
|
|
||||||
buffer.write(b);
|
|
||||||
} else {
|
|
||||||
swapped.write(b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized void write(byte[] b, int off, int len) throws IOException {
|
|
||||||
if (swapped == null) {
|
|
||||||
buffer.write(b, off, len);
|
|
||||||
} else {
|
|
||||||
swapped.write(b, off, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized void flush() throws IOException {
|
|
||||||
buffer.flush();
|
|
||||||
if (swapped != null) {
|
|
||||||
swapped.flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized void close() throws IOException {
|
|
||||||
buffer.close();
|
|
||||||
if (swapped != null) {
|
|
||||||
swapped.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized void write(int i) throws IOException {
|
|
||||||
if (swapped == null) {
|
|
||||||
buffer.write(i);
|
|
||||||
} else {
|
|
||||||
swapped.write(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
75
sop-java/src/main/kotlin/sop/util/ProxyOutputStream.kt
Normal file
75
sop-java/src/main/kotlin/sop/util/ProxyOutputStream.kt
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
// 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.
|
||||||
|
*/
|
||||||
|
class ProxyOutputStream {
|
||||||
|
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)
|
||||||
|
fun write(b: ByteArray) {
|
||||||
|
if (swapped == null) {
|
||||||
|
buffer.write(b)
|
||||||
|
} else {
|
||||||
|
swapped!!.write(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
@Throws(IOException::class)
|
||||||
|
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)
|
||||||
|
fun flush() {
|
||||||
|
buffer.flush()
|
||||||
|
if (swapped != null) {
|
||||||
|
swapped!!.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun close() {
|
||||||
|
buffer.close()
|
||||||
|
if (swapped != null) {
|
||||||
|
swapped!!.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun write(i: Int) {
|
||||||
|
if (swapped == null) {
|
||||||
|
buffer.write(i)
|
||||||
|
} else {
|
||||||
|
swapped!!.write(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue