Path: blob/master/test/jdk/javax/imageio/spi/MarkTryFinallyReproducer.java
41149 views
/*1* Copyright 2015 Red Hat, Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 814407126* @run main/othervm MarkTryFinallyReproducer27* @summary Test that call to canDecodeInput in ImageIO don't corrupt28* mark/reset stack in ImageInputStream29* @author Jiri Vanek30*/3132import java.awt.image.BufferedImage;33import java.io.ByteArrayInputStream;34import java.io.IOException;35import java.nio.ByteOrder;36import java.util.Locale;37import javax.imageio.ImageIO;38import javax.imageio.ImageReader;39import javax.imageio.spi.IIORegistry;40import javax.imageio.spi.ImageReaderSpi;41import javax.imageio.stream.IIOByteBuffer;42import javax.imageio.stream.ImageInputStream;434445public class MarkTryFinallyReproducer {4647private static final byte[] bmp = new byte[]{48127,127, 66, 77, -86, 0, 0, 0, 0, 0, 0, 0,49122, 0, 0, 0, 108, 0, 0, 0, 4, 0, 0, 0, 4,500, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 48, 0, 0,510, 19, 11, 0, 0, 19, 11, 0, 0, 0, 0, 0, 0, 0,520, 0, 0, 66, 71, 82, 115, 0, 0, 0, 0, 0, 0, 0,530, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,540, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,550, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,560, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, -1, -1,57-1, -1, -1, 0, -1, 0, -1, -1, -1, -1, 0, 0, 0, -17,580, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, -1, -1, -1,59-1, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, -160};61//first two are evil, we are skipping them later. Others are normal BMP6263private static class NotClosingImageInputStream implements ImageInputStream {6465private final ImageInputStream src;6667private NotClosingImageInputStream(ImageInputStream createImageInputStream) {68this.src = createImageInputStream;69}7071@Override72public void setByteOrder(ByteOrder byteOrder) {73src.setByteOrder(byteOrder);74}7576@Override77public ByteOrder getByteOrder() {78return src.getByteOrder();79}8081@Override82public int read() throws IOException {83return src.read();84}8586@Override87public int read(byte[] b) throws IOException {88return src.read(b);89}9091@Override92public int read(byte[] b, int off, int len) throws IOException {93return src.read(b, off, len);94}9596@Override97public void readBytes(IIOByteBuffer buf, int len) throws IOException {98src.readBytes(buf, len);99}100101@Override102public boolean readBoolean() throws IOException {103return src.readBoolean();104}105106@Override107public byte readByte() throws IOException {108return src.readByte();109}110111@Override112public int readUnsignedByte() throws IOException {113return src.readUnsignedByte();114}115116@Override117public short readShort() throws IOException {118return src.readShort();119}120121@Override122public int readUnsignedShort() throws IOException {123return src.readUnsignedShort();124}125126@Override127public char readChar() throws IOException {128return src.readChar();129}130131@Override132public int readInt() throws IOException {133return src.readInt();134}135136@Override137public long readUnsignedInt() throws IOException {138return src.readUnsignedInt();139}140141@Override142public long readLong() throws IOException {143return src.readLong();144}145146@Override147public float readFloat() throws IOException {148return src.readFloat();149}150151@Override152public double readDouble() throws IOException {153return src.readDouble();154}155156@Override157public String readLine() throws IOException {158return src.readLine();159}160161@Override162public String readUTF() throws IOException {163return src.readUTF();164}165166@Override167public void readFully(byte[] b, int off, int len) throws IOException {168src.readFully(b, off, len);169}170171@Override172public void readFully(byte[] b) throws IOException {173src.readFully(b);174}175176@Override177public void readFully(short[] s, int off, int len) throws IOException {178src.readFully(s, off, len);179}180181@Override182public void readFully(char[] c, int off, int len) throws IOException {183src.readFully(c, off, len);184}185186@Override187public void readFully(int[] i, int off, int len) throws IOException {188src.readFully(i, off, len);189}190191@Override192public void readFully(long[] l, int off, int len) throws IOException {193src.readFully(l, off, len);194}195196@Override197public void readFully(float[] f, int off, int len) throws IOException {198src.readFully(f, off, len);199}200201@Override202public void readFully(double[] d, int off, int len) throws IOException {203src.readFully(d, off, len);204}205206@Override207public long getStreamPosition() throws IOException {208return src.getStreamPosition();209}210211@Override212public int getBitOffset() throws IOException {213return src.getBitOffset();214}215216@Override217public void setBitOffset(int bitOffset) throws IOException {218src.setBitOffset(bitOffset);219}220221@Override222public int readBit() throws IOException {223return src.readBit();224}225226@Override227public long readBits(int numBits) throws IOException {228return src.readBits(numBits);229}230231@Override232public long length() throws IOException {233return src.length();234}235236@Override237public int skipBytes(int n) throws IOException {238return src.skipBytes(n);239}240241@Override242public long skipBytes(long n) throws IOException {243return src.skipBytes(n);244}245246@Override247public void seek(long pos) throws IOException {248src.seek(pos);249}250251@Override252public void mark() {253src.mark();254}255256@Override257public void reset() throws IOException {258src.reset();259}260261@Override262public void flushBefore(long pos) throws IOException {263src.flushBefore(pos);264}265266@Override267public void flush() throws IOException {268src.flush();269}270271@Override272public long getFlushedPosition() {273return src.getFlushedPosition();274}275276@Override277public boolean isCached() {278return src.isCached();279}280281@Override282public boolean isCachedMemory() {283return src.isCachedMemory();284}285286@Override287public boolean isCachedFile() {288return src.isCachedFile();289}290291@Override292public void close() throws IOException {293//the only important one. nothing294}295}296297static final String readerClassName298= MarkTryFinallyReproducerSpi.class.getName();299static final String[] localNames = {"myNames"};300static final String[] localSuffixes = {"mySuffixes"};301static final String[] localMIMETypes = {"myMimes"};302303public static class MarkTryFinallyReproducerSpi extends ImageReaderSpi {304305public MarkTryFinallyReproducerSpi() {306super("MarkTryFinallyReproducerSpi",307"1.0",308localNames,309localSuffixes,310localMIMETypes,311readerClassName,312new Class[]{ImageInputStream.class},313new String[0],314false,315null,316null,317new String[0],318new String[0],319false,320null,321null,322new String[0],323new String[0]);324}325326@Override327public String getDescription(Locale locale) {328return "";329}330331@Override332public boolean canDecodeInput(Object input) throws IOException {333throw new IOException("Bad luck");334}335336@Override337public ImageReader createReaderInstance(Object extension) {338return null;339}340}341342public static void main(String[] args) throws IOException {343MarkTryFinallyReproducerSpi spi = new MarkTryFinallyReproducerSpi();344IIORegistry.getDefaultInstance().registerServiceProvider(spi);345346ImageInputStream iis1 =347new NotClosingImageInputStream(ImageIO.createImageInputStream(new ByteArrayInputStream(bmp)));348iis1.readByte();349iis1.mark();350long p1 = iis1.getStreamPosition();351iis1.readByte();352iis1.mark();353long p2 = iis1.getStreamPosition();354BufferedImage bi1 = ImageIO.read(iis1);355iis1.reset();356long pn2 = iis1.getStreamPosition();357iis1.reset();358long pn1 = iis1.getStreamPosition();359if (p1 != pn1 || p2!= pn2) {360throw new RuntimeException("Exception from call to canDecodeInput in ImageIO. " +361"Corrupted stack in ImageInputStream");362}363364}365366}367368369