-
Bug
-
Resolution: Unresolved
-
P4
-
5.0, 6u38, 7, 8, 9, 10, 11, 12, 13, 14, 15
-
Cause Known
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2198675 | 7 | Andrew Brygin | P5 | Closed | Won't Fix |
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.
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.
- backported by
-
JDK-2198675 ImageIO.write() does not honor the read-only property of the given file
- Closed