2

I want to write XMP to jpg. I use Apache Sanselan library. In the following code I'm trying to read XMP XML and write it again to the same file but get the exception(ImageReadException: Unexpected EOF.).

File file=new File("./img/file.jpg");//input file
    if(file.canWrite()){
        try {
            String xmpXml = Sanselan.getXmpXml(file);
            JpegXmpRewriter xmpWriter=new JpegXmpRewriter();
            //File newfile = new File("./img/newfile.jpg"); //output file
            //xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(newfile)), xmpXml);    
            xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(file)), xmpXml);             
        } catch (ImageReadException | IOException | ImageWriteException e) {
            e.printStackTrace();
        }
    }else {
        System.out.println("Can NOT Write");
    } 

If the input and output files are different the code works fine.

File newfile = new File("./img/newfile.jpg"); //output file
xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(newfile)), xmpXml);

But if I read from and write to the same file i get an exception.

    org.apache.sanselan.ImageReadException: Unexpected EOF.
    at org.apache.sanselan.common.BinaryFileFunctions.readAndVerifyBytes(BinaryFileFunctions.java:129)
    at org.apache.sanselan.formats.jpeg.JpegUtils.traverseJFIF(JpegUtils.java:61)
    at org.apache.sanselan.formats.jpeg.xmp.JpegRewriter.analyzeJFIF(JpegRewriter.java:204)
    at org.apache.sanselan.formats.jpeg.xmp.JpegXmpRewriter.updateXmpXml(JpegXmpRewriter.java:187)
    at Editor$1.mouseClicked(Editor.java:147)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
Alexey
  • 7,127
  • 9
  • 57
  • 94
  • 1
    Sanselan has joined Apache and will be released as Apache Commons Imaging 1.0 in the near future. You might want to try a more recent build of the library, maybe the issue you found has been fixed. You'll find the daily snapshots here: https://repository.apache.org/content/groups/snapshots/org/apache/commons/commons-imaging/1.0-SNAPSHOT/ – Emmanuel Bourg Jun 02 '12 at 22:38
  • 3
    `xmpWriter.updateXmpXml(new ByteSourceFile(file), new BufferedOutputStream(new FileOutputStream(file)), xmpXml);` reads and writes destructively to the same file. No wonder. – Joop Eggen Jun 02 '12 at 23:18
  • I updated to the new version but now String xmpXml = Sanselan.getXmpXml(file); Sansealan is not recognized. How to read the xmp xml with the new lib? – Alexey Jun 03 '12 at 06:46
  • Also I tried with the new lib and got the same exception org.apache.commons.imaging.ImageReadException: Unexpected EOF. – Alexey Jun 03 '12 at 07:19
  • 5
    I'm hitting upon the same problem (with Sanselan 0.97, on Android). While realizing I'm commenting/answering on a three year old question, I thought I might as well share my workaround. Which is the obvious, use a temp destination file, temp.jpg. Then, after updating temp.jpg, simply copy temp.jpg to dest.jpg. – joakimk Nov 29 '15 at 19:18

0 Answers0