Simple, concise Java examples that do useful things.
Saturday, August 26, 2006
Friday, August 25, 2006
XMP metadata dumper
XMP is a metatdata format from Adobe. One way to add and edit XMP is with PixVue. This is a minimalist XMP extractor/dumper for the jpeg format.
import java.io.*;
public class XMPDumper {
public static void main(String[] args) throws IOException {
File f = new File("myimage.jpg");
String xmp = new XMPDumper().getXMP(f);
System.out.println(xmp);
}
public String getXMP(File f)
throws IOException {
FileInputStream is = new FileInputStream(f);
int i=0; String result=null;
while ((i=is.read()) != -1)
if (i==0xFF && is.read()==0xE1) {
result = checkNamespace(is);
if (result!=null) break;
}
return result;
}
public static final String namespace =
"http://ns.adobe.com/xap/1.0/";
private String checkNamespace(FileInputStream is)
throws IOException {
String s = getSegment(is);
return (s.startsWith(namespace)) ? s.substring(29) : null;
}
private String getSegment(InputStream is)
throws IOException {
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[dis.readShort()-2];
dis.read(buffer);
return new String(buffer);
}
}
Resource Consumption Management API
Resource Consumption Management API JSR
This sounds like JINI or JMX to me...the JCP is becomming an echo chamber.
This sounds like JINI or JMX to me...the JCP is becomming an echo chamber.
Thursday, August 24, 2006
Simple template replacer
Often I find I need a simple template engine to replace named regions.
public abstract class Replace {
public static void main(String[] args) {
Replace r = new Replace() {
public String lookup(String key) {
return "*newvalue*";
}
};
String s =
"Welcome ${name}, you have just won a new ${prize}.";
System.out.println(r.replace(s));
}
public String replace(String s) {
return replace(s, 0, new StringBuilder()).toString();
}
private StringBuilder replace(String s, int offset, StringBuilder b) {
int startKey = s.indexOf("${", offset);
if (startKey == -1)
return b.append(s.substring(offset));
else {
b.append(s.substring(offset, startKey));
int keyEnd = s.indexOf('}', startKey);
String key = s.substring(startKey + 2, keyEnd);
b.append(lookup(key));
return replace(s, keyEnd + 1, b);
}
}
abstract public String lookup(String key);
}
Subscribe to:
Posts (Atom)