Skip to content

Commit 603526b

Browse files
committed
8364768: JDK javax.imageio ImageWriters do not all flush the output stream
Reviewed-by: psadhukhan, azvegint
1 parent c01b4fc commit 603526b

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/java.desktop/share/classes/com/sun/imageio/plugins/gif/GIFImageWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ private void write(boolean writeHeader,
734734
if (writeTrailer) {
735735
writeTrailer();
736736
}
737+
stream.flush();
737738
}
738739

739740
/**

src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,7 @@ public void write(IIOMetadata sm,
23272327
if (abortRequested()) {
23282328
resetPositions();
23292329
}
2330+
stream.flush();
23302331
}
23312332

23322333
private void writeHeader() throws IOException {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8364768
27+
* @summary Tests that the standard plugins flush the stream after writing a complete image.
28+
*/
29+
30+
import static java.awt.Color.WHITE;
31+
import java.awt.Graphics2D;
32+
import java.awt.image.BufferedImage;
33+
import java.io.IOException;
34+
import java.io.ByteArrayOutputStream;
35+
import javax.imageio.ImageIO;
36+
import javax.imageio.ImageWriter;
37+
import javax.imageio.stream.FileCacheImageOutputStream;
38+
39+
public class FlushTest {
40+
41+
static final int SZ = 1000;
42+
static BufferedImage bi;
43+
static final String[] FORMATS = { "jpg", "png", "gif", "tiff", "bmp", "wbmp" } ;
44+
static boolean failed = false;
45+
46+
public static void main(String[] args) throws IOException {
47+
48+
bi = new BufferedImage(SZ, SZ, BufferedImage.TYPE_BYTE_BINARY);
49+
Graphics2D g2d = bi.createGraphics();
50+
g2d.setPaint(WHITE);
51+
g2d.fillRect(0, 0, SZ, SZ);
52+
53+
for (String f : FORMATS) {
54+
testWrite(f);
55+
}
56+
if (failed) {
57+
throw new RuntimeException("Stream sizes differ.");
58+
}
59+
}
60+
61+
static void testWrite(String fmt) throws IOException {
62+
ImageWriter iw = ImageIO.getImageWritersBySuffix(fmt).next();
63+
System.out.println(iw);
64+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
65+
FileCacheImageOutputStream fcs = new FileCacheImageOutputStream(baos, null);
66+
iw.setOutput(fcs);
67+
iw.write(bi);
68+
int sz0 = baos.size();
69+
fcs.close();
70+
int sz1 = baos.size();
71+
System.out.println("fmt=" + fmt + " sizes=" + sz0 + ", " + sz1);
72+
if (sz0 != sz1) {
73+
failed = true;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)