From 9941d071541d1bc73651c0fa24be6851d5444b04 Mon Sep 17 00:00:00 2001 From: J2ObjC Team Date: Tue, 24 Sep 2024 11:35:07 -0700 Subject: [PATCH] Update InputStreamReader and OutputStreamReader to use their lock instead of themselves as the encoder/decoder lock. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stream decoder will continue to take in a lock via the public factory methods. The internal initializers will still call super with the lock. To ensure the initializers will not be called, they will be made private. It’s up to the InputStreamReader to pass in the correct lock instead of itself. So to fix this, the lock created by the InputStreamReader’s superclass (Reader) at construction time will be passed in instead. Did the same thing for the OutputStreamWriter. (StreamEncoder constructors were already private) This was causing a retain cycle in iOS. Additionally, the reader and decoder were using different objects as their lock which may lead to some contention PiperOrigin-RevId: 678332165 --- .../src/main/java/java/io/InputStreamReader.java | 12 ++++-------- .../src/main/java/java/io/OutputStreamWriter.java | 8 ++++---- .../src/main/java/sun/nio/cs/StreamDecoder.java | 6 +++--- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/InputStreamReader.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/InputStreamReader.java index bb76cdce5f..d388621e4a 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/InputStreamReader.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/InputStreamReader.java @@ -69,8 +69,7 @@ public class InputStreamReader extends Reader { * @param in An InputStream */ public InputStreamReader(InputStream in) { - super(in); - sd = StreamDecoder.forInputStreamReader(in, this, + sd = StreamDecoder.forInputStreamReader(in, this.lock, Charset.defaultCharset()); // ## check lock object } @@ -90,10 +89,9 @@ public InputStreamReader(InputStream in) { public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException { - super(in); if (charsetName == null) throw new NullPointerException("charsetName"); - sd = StreamDecoder.forInputStreamReader(in, this, charsetName); + sd = StreamDecoder.forInputStreamReader(in, this.lock, charsetName); } /** @@ -106,10 +104,9 @@ public InputStreamReader(InputStream in, String charsetName) * @spec JSR-51 */ public InputStreamReader(InputStream in, Charset cs) { - super(in); if (cs == null) throw new NullPointerException("charset"); - sd = StreamDecoder.forInputStreamReader(in, this, cs); + sd = StreamDecoder.forInputStreamReader(in, this.lock, cs); } /** @@ -122,10 +119,9 @@ public InputStreamReader(InputStream in, Charset cs) { * @spec JSR-51 */ public InputStreamReader(InputStream in, CharsetDecoder dec) { - super(in); if (dec == null) throw new NullPointerException("charset decoder"); - sd = StreamDecoder.forInputStreamReader(in, this, dec); + sd = StreamDecoder.forInputStreamReader(in, this.lock, dec); } /** diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/OutputStreamWriter.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/OutputStreamWriter.java index 5f7b9e34bc..e88e81ac1d 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/OutputStreamWriter.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/java/io/OutputStreamWriter.java @@ -97,7 +97,7 @@ public OutputStreamWriter(OutputStream out, String charsetName) super(out); if (charsetName == null) throw new NullPointerException("charsetName"); - se = StreamEncoder.forOutputStreamWriter(out, this, charsetName); + se = StreamEncoder.forOutputStreamWriter(out, this.lock, charsetName); } /** @@ -108,7 +108,7 @@ public OutputStreamWriter(OutputStream out, String charsetName) public OutputStreamWriter(OutputStream out) { super(out); try { - se = StreamEncoder.forOutputStreamWriter(out, this, (String)null); + se = StreamEncoder.forOutputStreamWriter(out, this.lock, (String)null); } catch (UnsupportedEncodingException e) { throw new Error(e); } @@ -130,7 +130,7 @@ public OutputStreamWriter(OutputStream out, Charset cs) { super(out); if (cs == null) throw new NullPointerException("charset"); - se = StreamEncoder.forOutputStreamWriter(out, this, cs); + se = StreamEncoder.forOutputStreamWriter(out, this.lock, cs); } /** @@ -149,7 +149,7 @@ public OutputStreamWriter(OutputStream out, CharsetEncoder enc) { super(out); if (enc == null) throw new NullPointerException("charset encoder"); - se = StreamEncoder.forOutputStreamWriter(out, this, enc); + se = StreamEncoder.forOutputStreamWriter(out, this.lock, enc); } /** diff --git a/jre_emul/android/platform/libcore/ojluni/src/main/java/sun/nio/cs/StreamDecoder.java b/jre_emul/android/platform/libcore/ojluni/src/main/java/sun/nio/cs/StreamDecoder.java index 0d469949ed..c9014539e1 100644 --- a/jre_emul/android/platform/libcore/ojluni/src/main/java/sun/nio/cs/StreamDecoder.java +++ b/jre_emul/android/platform/libcore/ojluni/src/main/java/sun/nio/cs/StreamDecoder.java @@ -233,14 +233,14 @@ private static FileChannel getChannel(FileInputStream in) { private InputStream in; private ReadableByteChannel ch; - StreamDecoder(InputStream in, Object lock, Charset cs) { + private StreamDecoder(InputStream in, Object lock, Charset cs) { this(in, lock, cs.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE)); } - StreamDecoder(InputStream in, Object lock, CharsetDecoder dec) { + private StreamDecoder(InputStream in, Object lock, CharsetDecoder dec) { super(lock); this.cs = dec.charset(); this.decoder = dec; @@ -260,7 +260,7 @@ private static FileChannel getChannel(FileInputStream in) { bb.flip(); // So that bb is initially empty } - StreamDecoder(ReadableByteChannel ch, CharsetDecoder dec, int mbc) { + private StreamDecoder(ReadableByteChannel ch, CharsetDecoder dec, int mbc) { this.in = null; this.ch = ch; this.decoder = dec;