All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.lib.json.FileHandler Maven / Gradle / Ivy

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 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();
		}
	}

	@Override
	Object decode(Decoder dec, 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;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy