Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6562235

ImageIO.write() does not honor the read-only property of the given file

    XMLWordPrintable

    Details

    • Subcomponent:
    • Understanding:
      Cause Known
    • CPU:
      generic
    • OS:
      generic

      Backports

        Description

        I am calling ImageIO.write(bimg, "jpg", outputFile) where outputFile points to a read-only file. I was expecting an IOException to be thrown by the write() method saying 'permission denied' but the write method has successfully written the given buffered image to the file. This is because write() method first deletes the given file and then creates a new one afresh and delete operation is permitted because the parent dir is writable. This is incorrect.

        Write method should first check if the file is writable and then only should attemp to modify it. This is how all other APIs behave (for ex, FileOutputStream, PrintWriter, etc) where they throw an exception saying permission is denied.

        Run the following sample code to reproduce the bug -
        --------------------------------------------
        import java.io.*;
        import javax.imageio.*;
        import javax.imageio.stream.*;
        import java.awt.image.BufferedImage;
        import java.awt.*;

        public class FileTest {

        public static void main (String args[]) {
        try {
        boolean testPass = false;
        File outputFile = new File("dummy1.jpg");
        String str = "This is a temp file created and made as read-only. Will be " +
        "used by ImageIO for writing an image";
        PrintWriter pw = new PrintWriter(outputFile);
        pw.println(str);
        pw.flush();

        outputFile.setWritable(false, false);

        System.out.println("is file writable? " + outputFile.canWrite());

        BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bimg.getGraphics();
        g.setColor(Color.red);
        g.fillRect(0, 0, 100, 100);
        g.dispose();

        long sizeBefore = outputFile.length();
        System.out.println("Size before ImageIO operation: " + sizeBefore);

        try {
        System.out.println("Write Successful? " + ImageIO.write(bimg, "jpg", outputFile));
        } catch (IOException ioe) {
        System.out.println("PASS: ImageIO.write() throws IOException when the given " +
        "file is read-only");
        testPass = true;
        }
        long sizeAfter = outputFile.length();
        System.out.println("Size after ImageIO operation: " + sizeAfter);

        if (!testPass && (sizeBefore != sizeAfter)) {
        System.out.println("FAIL: ImageIO.write() has altered the read-only file");
        System.exit(1);
        } else {
        System.exit(0);
        }

        } catch (Exception e) {
        e.printStackTrace();

        }
        }
        }
        -------------------------------

        This is reproducible on all platforms and atleast since JDK5.

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                jdv Jayathirth D V
                Reporter:
                pmohansunw Praveen Mohan (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Imported:
                  Indexed: