From 2318caad7b8d86a4e092db4957614c44249dc2eb Mon Sep 17 00:00:00 2001 From: Bernd Busse Date: Wed, 24 Jan 2024 18:44:59 +0100 Subject: [PATCH] classlib: add static nullOutputStream() to OutputStream (#878) --- .../teavm/classlib/java/io/TOutputStream.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/classlib/src/main/java/org/teavm/classlib/java/io/TOutputStream.java b/classlib/src/main/java/org/teavm/classlib/java/io/TOutputStream.java index 57d1d4b88..f36d17e3a 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/io/TOutputStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/io/TOutputStream.java @@ -19,6 +19,36 @@ import java.io.IOException; import org.teavm.classlib.java.lang.TObject; public abstract class TOutputStream extends TObject implements TCloseable, TFlushable { + public static TOutputStream nullOutputStream() { + return new TOutputStream() { + private boolean isClosed; + + @Override + public void write(int b) throws IOException { + if (isClosed) { + throw new IOException("Stream closed"); + } + } + + @Override + public void write(byte[] b) throws IOException { + write(b, 0, b.length); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (isClosed) { + throw new IOException("Stream closed"); + } + } + + @Override + public void close() throws IOException { + isClosed = true; + } + }; + } + public abstract void write(int b) throws IOException; public void write(byte[] b) throws IOException {