Simple, concise Java examples that do useful things.

Saturday, September 02, 2006

XMP Dumper reduced

I was able to trim this one down a bit more.


import java.io.*;

public class XMPDumper {

public String getXMP(File f) throws IOException {
return getXMP(new FileInputStream(f));
}

public String getXMP(InputStream is)
throws IOException {
for (int i=0;(i=is.read())!=-1;)
if (i==0xFF && is.read()==0xE1) {
String result = getXMPSegment(is);
if (result.startsWith("http://ns.adobe.com/xap/1.0/"))
return result.substring(29);
}
return null;
}

private String getXMPSegment(InputStream is)
throws IOException {
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[dis.readShort()-2];
dis.read(buffer);
return new String(buffer);
}
}

No comments: