/** * Convert an MVD to a string * * @param mvd the MVD to convert * @param rb database properties file * @param folderId id of the folder to contain it in * @throws Exception raised if an error occurred */ public static String externalise(MVD mvd) throws Exception { int size = mvd.dataSize(); byte[] data = new byte[size]; int nBytes = mvd.serialise(data); assert nBytes == size : "MVD shorter than predicted"; return Base64.encodeBytes(data, Base64.GZIP); }
/** * Save an MVD to a file * * @param mvd the MVD to save * @param dst the file to save it to * @param rb database properties file * @param folderId id of the folder to contain it in * @throws Exception raised if an error occurred */ public static void externalise(MVD mvd, File dst, int folderId, Properties rb) throws Exception { long start = System.nanoTime() / 1000; System.gc(); long startMem = Runtime.getRuntime().freeMemory(); int size = mvd.dataSize(); byte[] data = new byte[size]; int nBytes = mvd.serialise(data); assert nBytes == size : "MVD shorter than predicted"; String str = Base64.encodeBytes(data, Base64.GZIP); if (rb == null) writeToFile(dst, str); else writeToDatabase(dst.getName(), str, mvd.description, folderId, rb); long end = System.nanoTime() / 1000; long endMem = Runtime.getRuntime().freeMemory(); System.out.println("internalise took " + (end - start) + " microseconds"); System.out.println("Memory used " + (startMem - endMem) + " bytes"); }
/** * Decode the base-64 encoded text into a byte array, check the magic string at the start of the * file, and decompress it. * * @param base64Data the base64 data to decode * @return the byte array containing the decoded data. * @throws an exception if it is not an MVD file */ private static byte[] base64Decode(String base64Data) throws Exception { byte[] data = Base64.decode(base64Data); if (!magicOK(data)) throw new MVDException("Not a valid MVD file"); return data; }