8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener
Reviewed-by: prr, bpb, serb, psadhukhan
This commit is contained in:
parent
f0cac034bd
commit
3f1ae2ad4e
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -156,6 +156,10 @@ public class BMPImageWriter extends ImageWriter implements BMPConstants {
|
|||||||
|
|
||||||
clearAbortRequest();
|
clearAbortRequest();
|
||||||
processImageStarted(0);
|
processImageStarted(0);
|
||||||
|
if (abortRequested()) {
|
||||||
|
processWriteAborted();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (param == null)
|
if (param == null)
|
||||||
param = getDefaultWriteParam();
|
param = getDefaultWriteParam();
|
||||||
|
|
||||||
@ -583,12 +587,8 @@ public class BMPImageWriter extends ImageWriter implements BMPConstants {
|
|||||||
stream.write(embedded_stream.toByteArray());
|
stream.write(embedded_stream.toByteArray());
|
||||||
embedded_stream = null;
|
embedded_stream = null;
|
||||||
|
|
||||||
if (abortRequested()) {
|
|
||||||
processWriteAborted();
|
|
||||||
} else {
|
|
||||||
processImageComplete();
|
processImageComplete();
|
||||||
stream.flushBefore(stream.getStreamPosition());
|
stream.flushBefore(stream.getStreamPosition());
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -606,9 +606,6 @@ public class BMPImageWriter extends ImageWriter implements BMPConstants {
|
|||||||
destScanlineLength = destScanlineBytes / (DataBuffer.getDataTypeSize(dataType)>>3);
|
destScanlineLength = destScanlineBytes / (DataBuffer.getDataTypeSize(dataType)>>3);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < h; i++) {
|
for (int i = 0; i < h; i++) {
|
||||||
if (abortRequested()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
int row = minY + i;
|
int row = minY + i;
|
||||||
|
|
||||||
@ -724,6 +721,9 @@ public class BMPImageWriter extends ImageWriter implements BMPConstants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
processImageProgress(100.0f * (((float)i) / ((float)h)));
|
processImageProgress(100.0f * (((float)i) / ((float)h)));
|
||||||
|
if (abortRequested()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compressionType == BI_RLE4 ||
|
if (compressionType == BI_RLE4 ||
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -574,7 +574,6 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
IIOMetadata sm,
|
IIOMetadata sm,
|
||||||
IIOImage iioimage,
|
IIOImage iioimage,
|
||||||
ImageWriteParam p) throws IOException {
|
ImageWriteParam p) throws IOException {
|
||||||
clearAbortRequest();
|
|
||||||
|
|
||||||
RenderedImage image = iioimage.getRenderedImage();
|
RenderedImage image = iioimage.getRenderedImage();
|
||||||
|
|
||||||
@ -829,11 +828,11 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
image.getTile(0, 0) : image.getData();
|
image.getTile(0, 0) : image.getData();
|
||||||
for (int y = dy; y < dh; y += ddy) {
|
for (int y = dy; y < dh; y += ddy) {
|
||||||
if (numRowsWritten % progressReportRowPeriod == 0) {
|
if (numRowsWritten % progressReportRowPeriod == 0) {
|
||||||
|
processImageProgress((numRowsWritten*100.0F)/dh);
|
||||||
if (abortRequested()) {
|
if (abortRequested()) {
|
||||||
processWriteAborted();
|
processWriteAborted();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
processImageProgress((numRowsWritten*100.0F)/dh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
raster.getSamples(sx, sy, sw, 1, 0, sbuf);
|
raster.getSamples(sx, sy, sw, 1, 0, sbuf);
|
||||||
@ -857,11 +856,11 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
lineStride *= ddy;
|
lineStride *= ddy;
|
||||||
for (int y = dy; y < dh; y += ddy) {
|
for (int y = dy; y < dh; y += ddy) {
|
||||||
if (numRowsWritten % progressReportRowPeriod == 0) {
|
if (numRowsWritten % progressReportRowPeriod == 0) {
|
||||||
|
processImageProgress((numRowsWritten*100.0F)/dh);
|
||||||
if (abortRequested()) {
|
if (abortRequested()) {
|
||||||
processWriteAborted();
|
processWriteAborted();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
processImageProgress((numRowsWritten*100.0F)/dh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compressor.compress(data, offset, dw);
|
compressor.compress(data, offset, dw);
|
||||||
@ -924,7 +923,12 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
|
|
||||||
int progressReportRowPeriod = Math.max(destHeight/20, 1);
|
int progressReportRowPeriod = Math.max(destHeight/20, 1);
|
||||||
|
|
||||||
|
clearAbortRequest();
|
||||||
processImageStarted(imageIndex);
|
processImageStarted(imageIndex);
|
||||||
|
if (abortRequested()) {
|
||||||
|
processWriteAborted();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (interlaceFlag) {
|
if (interlaceFlag) {
|
||||||
if (DEBUG) System.out.println("Writing interlaced");
|
if (DEBUG) System.out.println("Writing interlaced");
|
||||||
@ -973,6 +977,9 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
writeRowsOpt(data, offset, lineStride, compressor,
|
writeRowsOpt(data, offset, lineStride, compressor,
|
||||||
1, 2, destWidth, destHeight,
|
1, 2, destWidth, destHeight,
|
||||||
numRowsWritten, progressReportRowPeriod);
|
numRowsWritten, progressReportRowPeriod);
|
||||||
|
if (abortRequested()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
writeRows(image, compressor,
|
writeRows(image, compressor,
|
||||||
sourceXOffset, periodX,
|
sourceXOffset, periodX,
|
||||||
@ -1016,6 +1023,9 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
sourceWidth,
|
sourceWidth,
|
||||||
1, 2, destWidth, destHeight,
|
1, 2, destWidth, destHeight,
|
||||||
numRowsWritten, progressReportRowPeriod);
|
numRowsWritten, progressReportRowPeriod);
|
||||||
|
if (abortRequested()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (DEBUG) System.out.println("Writing non-interlaced");
|
if (DEBUG) System.out.println("Writing non-interlaced");
|
||||||
@ -1031,6 +1041,9 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
writeRowsOpt(data, offset, lineStride, compressor,
|
writeRowsOpt(data, offset, lineStride, compressor,
|
||||||
0, 1, destWidth, destHeight,
|
0, 1, destWidth, destHeight,
|
||||||
numRowsWritten, progressReportRowPeriod);
|
numRowsWritten, progressReportRowPeriod);
|
||||||
|
if (abortRequested()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
writeRows(image, compressor,
|
writeRows(image, compressor,
|
||||||
sourceXOffset, periodX,
|
sourceXOffset, periodX,
|
||||||
@ -1038,14 +1051,11 @@ public class GIFImageWriter extends ImageWriter {
|
|||||||
sourceWidth,
|
sourceWidth,
|
||||||
0, 1, destWidth, destHeight,
|
0, 1, destWidth, destHeight,
|
||||||
numRowsWritten, progressReportRowPeriod);
|
numRowsWritten, progressReportRowPeriod);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (abortRequested()) {
|
if (abortRequested()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
processImageProgress(100.0F);
|
}
|
||||||
|
|
||||||
compressor.flush();
|
compressor.flush();
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -1234,7 +1234,9 @@ public final class PNGImageWriter extends ImageWriter {
|
|||||||
clearAbortRequest();
|
clearAbortRequest();
|
||||||
|
|
||||||
processImageStarted(0);
|
processImageStarted(0);
|
||||||
|
if (abortRequested()) {
|
||||||
|
processWriteAborted();
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
write_magic();
|
write_magic();
|
||||||
write_IHDR();
|
write_IHDR();
|
||||||
@ -1274,3 +1276,4 @@ public final class PNGImageWriter extends ImageWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -2437,6 +2437,10 @@ public class TIFFImageWriter extends ImageWriter {
|
|||||||
|
|
||||||
clearAbortRequest();
|
clearAbortRequest();
|
||||||
processImageStarted(0);
|
processImageStarted(0);
|
||||||
|
if (abortRequested()) {
|
||||||
|
processWriteAborted();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Optionally write the header.
|
// Optionally write the header.
|
||||||
if (writeHeader) {
|
if (writeHeader) {
|
||||||
@ -2587,9 +2591,6 @@ public class TIFFImageWriter extends ImageWriter {
|
|||||||
nextSpace = pos + byteCount;
|
nextSpace = pos + byteCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
pixelsDone += tileRect.width*tileRect.height;
|
|
||||||
processImageProgress(100.0F*pixelsDone/totalPixels);
|
|
||||||
|
|
||||||
// Fill in the offset and byte count for the file
|
// Fill in the offset and byte count for the file
|
||||||
stream.mark();
|
stream.mark();
|
||||||
stream.seek(stripOrTileOffsetsPosition);
|
stream.seek(stripOrTileOffsetsPosition);
|
||||||
@ -2600,14 +2601,16 @@ public class TIFFImageWriter extends ImageWriter {
|
|||||||
stream.writeInt(byteCount);
|
stream.writeInt(byteCount);
|
||||||
stripOrTileByteCountsPosition += 4;
|
stripOrTileByteCountsPosition += 4;
|
||||||
stream.reset();
|
stream.reset();
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IIOException("I/O error writing TIFF file!", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
pixelsDone += tileRect.width*tileRect.height;
|
||||||
|
processImageProgress(100.0F*pixelsDone/totalPixels);
|
||||||
if (abortRequested()) {
|
if (abortRequested()) {
|
||||||
processWriteAborted();
|
processWriteAborted();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IIOException("I/O error writing TIFF file!", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
169
jdk/test/javax/imageio/WriteAbortTest.java
Normal file
169
jdk/test/javax/imageio/WriteAbortTest.java
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @bug 8164931
|
||||||
|
* @summary Test verifies that if we call ImageWriter.abort() in
|
||||||
|
* IIOWriteProgressListener.imageStarted() or
|
||||||
|
* IIOWriteProgressListener.imageProgress() are we
|
||||||
|
* calling IIOWriteProgressListener.readAborted() for all readers.
|
||||||
|
* @run main WriteAbortTest
|
||||||
|
*/
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.imageio.stream.ImageInputStream;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import javax.imageio.ImageWriter;
|
||||||
|
import javax.imageio.event.IIOWriteProgressListener;
|
||||||
|
import javax.imageio.stream.ImageOutputStream;
|
||||||
|
|
||||||
|
public class WriteAbortTest implements IIOWriteProgressListener {
|
||||||
|
|
||||||
|
ImageWriter writer = null;
|
||||||
|
ImageOutputStream ios = null;
|
||||||
|
BufferedImage bimg = null;
|
||||||
|
File file;
|
||||||
|
boolean startAbort = false;
|
||||||
|
boolean startAborted = false;
|
||||||
|
boolean progressAbort = false;
|
||||||
|
boolean progressAborted = false;
|
||||||
|
Color srccolor = Color.red;
|
||||||
|
int width = 100;
|
||||||
|
int heght = 100;
|
||||||
|
|
||||||
|
public WriteAbortTest(String format) throws Exception {
|
||||||
|
try {
|
||||||
|
System.out.println("Test for format " + format);
|
||||||
|
bimg = new BufferedImage(width, heght,
|
||||||
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
|
||||||
|
Graphics2D g = bimg.createGraphics();
|
||||||
|
g.setColor(srccolor);
|
||||||
|
g.fillRect(0, 0, width, heght);
|
||||||
|
g.dispose();
|
||||||
|
|
||||||
|
file = File.createTempFile("src_", "." + format, new File("."));
|
||||||
|
ImageInputStream ios = ImageIO.createImageOutputStream(file);
|
||||||
|
|
||||||
|
ImageWriter writer =
|
||||||
|
ImageIO.getImageWritersByFormatName(format).next();
|
||||||
|
|
||||||
|
writer.setOutput(ios);
|
||||||
|
writer.addIIOWriteProgressListener(this);
|
||||||
|
|
||||||
|
// Abort writing in IIOWriteProgressListener.imageStarted().
|
||||||
|
startAbort = true;
|
||||||
|
writer.write(bimg);
|
||||||
|
startAbort = false;
|
||||||
|
|
||||||
|
// Abort writing in IIOWriteProgressListener.imageProgress().
|
||||||
|
progressAbort = true;
|
||||||
|
writer.write(bimg);
|
||||||
|
progressAbort = false;
|
||||||
|
|
||||||
|
ios.close();
|
||||||
|
/*
|
||||||
|
* All abort requests from imageStarted,imageProgress
|
||||||
|
* from IIOWriteProgressListener should be reached
|
||||||
|
* otherwise throw RuntimeException.
|
||||||
|
*/
|
||||||
|
if (!(startAborted
|
||||||
|
&& progressAborted)) {
|
||||||
|
throw new RuntimeException("All IIOWriteProgressListener abort"
|
||||||
|
+ " requests are not processed for format "
|
||||||
|
+ format);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
Files.delete(file.toPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Abstract methods that we need to implement from
|
||||||
|
* IIOWriteProgressListener, and relevant for this test case.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void imageStarted(ImageWriter source, int imageIndex) {
|
||||||
|
System.out.println("imageStarted called");
|
||||||
|
if (startAbort) {
|
||||||
|
source.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void imageProgress(ImageWriter source, float percentageDone) {
|
||||||
|
System.out.println("imageProgress called");
|
||||||
|
if (progressAbort) {
|
||||||
|
source.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeAborted(ImageWriter source) {
|
||||||
|
System.out.println("writeAborted called");
|
||||||
|
// Verify IIOWriteProgressListener.imageStarted() abort request.
|
||||||
|
if (startAbort) {
|
||||||
|
System.out.println("imageStarted aborted ");
|
||||||
|
startAborted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify IIOWriteProgressListener.imageProgress() abort request.
|
||||||
|
if (progressAbort) {
|
||||||
|
System.out.println("imageProgress aborted ");
|
||||||
|
progressAborted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String args[]) throws Exception {
|
||||||
|
final String[] formats = {"bmp", "png", "gif", "jpg", "tif"};
|
||||||
|
for (String format : formats) {
|
||||||
|
new WriteAbortTest(format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Remaining abstract methods that we need to implement from
|
||||||
|
* IIOWriteProgressListener, but not relevant for this test case.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void imageComplete(ImageWriter source) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void thumbnailStarted(ImageWriter source, int imageIndex,
|
||||||
|
int thumbnailIndex) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void thumbnailProgress(ImageWriter source, float percentageDone) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void thumbnailComplete(ImageWriter source) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user