From 79c9e7a36cc139d41f99004b61f88dcd01c0c530 Mon Sep 17 00:00:00 2001 From: Google Java Core Libraries Date: Mon, 21 Oct 2024 10:41:28 -0700 Subject: [PATCH] Remove a couple more obsolete workarounds for `FilterOutputStream` trouble. See https://github.com/google/guava/issues/1330. I have the others taken care of in cl/687314611. PiperOrigin-RevId: 688191691 --- .../src/com/google/common/io/ByteSink.java | 21 ++++++++++++++++--- .../src/com/google/common/io/CharSink.java | 9 +++++++- guava/src/com/google/common/io/ByteSink.java | 21 ++++++++++++++++--- guava/src/com/google/common/io/CharSink.java | 9 +++++++- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/android/guava/src/com/google/common/io/ByteSink.java b/android/guava/src/com/google/common/io/ByteSink.java index f6727a987a2d..8f3c3d15fa76 100644 --- a/android/guava/src/com/google/common/io/ByteSink.java +++ b/android/guava/src/com/google/common/io/ByteSink.java @@ -99,8 +99,15 @@ public OutputStream openBufferedStream() throws IOException { public void write(byte[] bytes) throws IOException { checkNotNull(bytes); - try (OutputStream out = openStream()) { + Closer closer = Closer.create(); + try { + OutputStream out = closer.register(openStream()); out.write(bytes); + out.flush(); // https://github.com/google/guava/issues/1330 + } catch (Throwable e) { + throw closer.rethrow(e); + } finally { + closer.close(); } } @@ -115,8 +122,16 @@ public void write(byte[] bytes) throws IOException { public long writeFrom(InputStream input) throws IOException { checkNotNull(input); - try (OutputStream out = openStream()) { - return ByteStreams.copy(input, out); + Closer closer = Closer.create(); + try { + OutputStream out = closer.register(openStream()); + long written = ByteStreams.copy(input, out); + out.flush(); // https://github.com/google/guava/issues/1330 + return written; + } catch (Throwable e) { + throw closer.rethrow(e); + } finally { + closer.close(); } } diff --git a/android/guava/src/com/google/common/io/CharSink.java b/android/guava/src/com/google/common/io/CharSink.java index 14f350e833c8..0947eee1ffd4 100644 --- a/android/guava/src/com/google/common/io/CharSink.java +++ b/android/guava/src/com/google/common/io/CharSink.java @@ -92,8 +92,15 @@ public Writer openBufferedStream() throws IOException { public void write(CharSequence charSequence) throws IOException { checkNotNull(charSequence); - try (Writer out = openStream()) { + Closer closer = Closer.create(); + try { + Writer out = closer.register(openStream()); out.append(charSequence); + out.flush(); // https://github.com/google/guava/issues/1330 + } catch (Throwable e) { + throw closer.rethrow(e); + } finally { + closer.close(); } } diff --git a/guava/src/com/google/common/io/ByteSink.java b/guava/src/com/google/common/io/ByteSink.java index f6727a987a2d..8f3c3d15fa76 100644 --- a/guava/src/com/google/common/io/ByteSink.java +++ b/guava/src/com/google/common/io/ByteSink.java @@ -99,8 +99,15 @@ public OutputStream openBufferedStream() throws IOException { public void write(byte[] bytes) throws IOException { checkNotNull(bytes); - try (OutputStream out = openStream()) { + Closer closer = Closer.create(); + try { + OutputStream out = closer.register(openStream()); out.write(bytes); + out.flush(); // https://github.com/google/guava/issues/1330 + } catch (Throwable e) { + throw closer.rethrow(e); + } finally { + closer.close(); } } @@ -115,8 +122,16 @@ public void write(byte[] bytes) throws IOException { public long writeFrom(InputStream input) throws IOException { checkNotNull(input); - try (OutputStream out = openStream()) { - return ByteStreams.copy(input, out); + Closer closer = Closer.create(); + try { + OutputStream out = closer.register(openStream()); + long written = ByteStreams.copy(input, out); + out.flush(); // https://github.com/google/guava/issues/1330 + return written; + } catch (Throwable e) { + throw closer.rethrow(e); + } finally { + closer.close(); } } diff --git a/guava/src/com/google/common/io/CharSink.java b/guava/src/com/google/common/io/CharSink.java index 3e0d22a59b50..8164f533394e 100644 --- a/guava/src/com/google/common/io/CharSink.java +++ b/guava/src/com/google/common/io/CharSink.java @@ -94,8 +94,15 @@ public Writer openBufferedStream() throws IOException { public void write(CharSequence charSequence) throws IOException { checkNotNull(charSequence); - try (Writer out = openStream()) { + Closer closer = Closer.create(); + try { + Writer out = closer.register(openStream()); out.append(charSequence); + out.flush(); // https://github.com/google/guava/issues/1330 + } catch (Throwable e) { + throw closer.rethrow(e); + } finally { + closer.close(); } }