Java: PNGException: Premature End of JPEG File

  • Java
  • Thread starter haxor489
  • Start date
  • Tags
    Java
In summary: This could potentially be a memory leak if the contentLength() ever changed.In summary, the input to the image decoder was corrupted and the image could not be produced.
  • #1
haxor489
25
0
I'm getting this error

sun.awt.image.PNGImageDecoder$PNGException: crc corruption
at sun.awt.image.PNGImageDecoder.getChunk(Unknown Source)
at sun.awt.image.PNGImageDecoder.getData(Unknown Source)
at sun.awt.image.PNGFilterInputStream.read(Unknown Source)
at java.util.zip.InflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.awt.image.PNGImageDecoder.produceImage(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
This is the segment of code causing the error.

Code:
URLConnection con = new URL("www.example.com").openConnection();
		InputStream is = con.getInputStream();
		byte bytes[] = new byte[con.getContentLength()];
		
		BufferedInputStream bis = new BufferedInputStream(is);
		bis.read(bytes);
		bis.close();
		con.getInputStream().close();
		
		Toolkit tk = getToolkit();
		
		image.setImage(tk.createImage(bytes));
		File save = new File("picture");
		FileOutputStream fos =  new FileOutputStream(save);
		
		fos.write(bytes);
		fos.close();
		if(tk.prepareImage(image.getImage(), 900, 900, null)){
			System.out.println("True");
		}else
			System.out.println("False");

The file i output to shows that the image is corrupt, only a few lines of the image are visible and the rest is black. tk.prepareImage() also returns false. This segment sits in a method called once in its class's constructor method. I'm contacting the maps.google server for a map image with the proper parameters. The image returns fine with the same url in a browser . I've checked the google maps API documentation but nothing stands out. Any ideas?Edit: Attempted requesting a different image format, but now i get "Premature end of JPEG file"
 
Last edited:
Technology news on Phys.org
  • #2
haxor489 said:
This is the segment of code causing the error.

Code:
URLConnection con = new URL("www.example.com").openConnection();
		InputStream is = con.getInputStream();
		byte bytes[] = new byte[con.getContentLength()];
		
		BufferedInputStream bis = new BufferedInputStream(is);
		bis.read(bytes);
		bis.close();
		con.getInputStream().close();
		
		Toolkit tk = getToolkit();
		
		image.setImage(tk.createImage(bytes));
		File save = new File("picture");
		FileOutputStream fos =  new FileOutputStream(save);
		
		fos.write(bytes);
		fos.close();
		if(tk.prepareImage(image.getImage(), 900, 900, null)){
			System.out.println("True");
		}else
			System.out.println("False");

The file i output to shows that the image is corrupt, only a few lines of the image are visible and the rest is black. tk.prepareImage() also returns false. This segment sits in a method called once in its class's constructor method. I'm contacting the maps.google server for a map image with the proper parameters. The image returns fine with the same url in a browser . I've checked the google maps API documentation but nothing stands out. Any ideas?Edit: Attempted requesting a different image format, but now i get "Premature end of JPEG file"

This seems like a really convoluted way to grab an image from a URL. If the URL contains nothing but the image, why not just use something like
Code:
try {
    Image image = tk.createImage(new URL("www.example.com"));
} catch (//whatever exceptions URL's constructor & ToolKit.createImage(URL u) throw) {
    //handle exceptions
}
 
Last edited:
  • #3
I figured it out. Your way is a lot easier, I'm just used to doing things on a much lower level (C programmer). The stream reader would only read what was available() into the array and not the full contentLength() but a while check sum loop read the full content.
 

What is a PNGException?

A PNGException is an error that occurs when trying to read or write a PNG file in Java. It is a type of IOException and indicates that there was a problem with the PNG file.

What does "Premature End of JPEG File" mean?

"Premature End of JPEG File" means that the Java program encountered the end of a JPEG file before it was expected to. This can happen if the file is corrupted or if the program is trying to read the file in the wrong way.

Why am I getting a PNGException when working with JPEG files?

Although the error message says "Premature End of JPEG File", it is still considered a PNGException because the PNG format is often used as a container for JPEG images. So, even if the file is actually a JPEG, it may still be read as a PNG by the Java program.

How can I fix a PNGException: Premature End of JPEG File?

The best way to fix this error is to check the file you are trying to read or write. Make sure it is a valid PNG or JPEG file and try to open it in a different program. If the file is corrupted, you may need to find a different version or recreate the file.

Can I prevent a PNGException from occurring?

In some cases, you may be able to prevent a PNGException by checking the file before trying to read or write it. This can involve checking the file type, size, or any other relevant information. Additionally, you may need to handle the exception in your code to prevent the program from crashing if the error does occur.

Back
Top