blob: 2f0eea506c2bb45851b05d032012dbdeb7275535 [file] [log] [blame]
package aQute.lib.json;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import aQute.lib.base64.*;
public class FileHandler extends Handler {
@Override void encode(Encoder app, Object object, Map<Object, Type> visited)
throws IOException, Exception {
File f = (File) object;
if ( !f.isFile())
throw new RuntimeException("Encoding a file requires the file to exist and to be a normal file " + f );
FileInputStream in = new FileInputStream(f);
try {
app.append('"');
Base64.encode(in, app);
app.append('"');
} finally {
in.close();
}
}
Object decode(String s) throws Exception {
File tmp = File.createTempFile("json", ".bin");
FileOutputStream fout = new FileOutputStream(tmp);
try {
Base64.decode(new StringReader(s), fout);
} finally {
fout.close();
}
return tmp;
}
}