diff --git a/src/com/synthbot/audioplugin/vst/vst2/JVstPersistence.java b/src/com/synthbot/audioplugin/vst/vst2/JVstPersistence.java index 8326abf..62ef031 100644 --- a/src/com/synthbot/audioplugin/vst/vst2/JVstPersistence.java +++ b/src/com/synthbot/audioplugin/vst/vst2/JVstPersistence.java @@ -74,82 +74,79 @@ public static void loadPreset(JVstHost2 vst, File file) throws DataFormatExcepti DataInputStream fxp = new DataInputStream(new FileInputStream(file)); - byte[] fourBytes = new byte[4]; + try { + byte[] fourBytes = new byte[4]; - fxp.read(fourBytes); - if (!CHUNK_MAGIC.equals(new String(fourBytes))) { - fxp.close(); - throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag."); - } - - // fileLength is read an assigned to a variable for debugging purposes only - @SuppressWarnings("unused") - int fileLength = fxp.readInt(); + fxp.read(fourBytes); + if (!CHUNK_MAGIC.equals(new String(fourBytes))) { + throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag."); + } - fxp.read(fourBytes); - String chunkDataType = new String(fourBytes); - boolean isRegularChunk = true; - if (REGULAR_PRESET_MAGIC.equals(chunkDataType)) { - isRegularChunk = true; - } else if (OPAQUE_PRESET_MAGIC.equals(chunkDataType)) { - if (!vst.acceptsProgramsAsChunks()) { - fxp.close(); - throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks."); + // fileLength is read an assigned to a variable for debugging purposes only + @SuppressWarnings("unused") + int fileLength = fxp.readInt(); + + fxp.read(fourBytes); + String chunkDataType = new String(fourBytes); + boolean isRegularChunk = true; + if (REGULAR_PRESET_MAGIC.equals(chunkDataType)) { + isRegularChunk = true; + } else if (OPAQUE_PRESET_MAGIC.equals(chunkDataType)) { + if (!vst.acceptsProgramsAsChunks()) { + throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks."); + } else { + isRegularChunk = false; + } } else { - isRegularChunk = false; + throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks."); } - } else { - throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks."); - } - int fileVersion = fxp.readInt(); - if (fileVersion > 1) { - fxp.close(); - throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported."); - } + int fileVersion = fxp.readInt(); + if (fileVersion > 1) { + throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported."); + } - // unique id - fxp.read(fourBytes); - String uniqueId = new String(fourBytes); - if (!vst.getUniqueId().equals(uniqueId)) { - fxp.close(); - throw new DataFormatException("Unique plugin ID in file does not match given plugin. " + - "Is this file really for " + vst.getEffectName() + "?"); - } + // unique id + fxp.read(fourBytes); + String uniqueId = new String(fourBytes); + if (!vst.getUniqueId().equals(uniqueId)) { + throw new DataFormatException("Unique plugin ID in file does not match given plugin. " + + "Is this file really for " + vst.getEffectName() + "?"); + } - int filePluginVersion = fxp.readInt(); - if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) { - fxp.close(); - throw new DataFormatException("This file contains data for a later plugin version " + - Integer.toString(filePluginVersion) + ", and the given plugin is only version " + - Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin."); - } + int filePluginVersion = fxp.readInt(); + if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) { + throw new DataFormatException("This file contains data for a later plugin version " + + Integer.toString(filePluginVersion) + ", and the given plugin is only version " + + Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin."); + } - int numParameters = fxp.readInt(); + int numParameters = fxp.readInt(); - byte[] programNameBytes = new byte[28]; - fxp.read(programNameBytes); - String programName = new String(programNameBytes); - vst.setProgramName(programName); + byte[] programNameBytes = new byte[28]; + fxp.read(programNameBytes); + String programName = new String(programNameBytes); + vst.setProgramName(programName); - if (isRegularChunk) { - for (int i = 0; i < numParameters; i++) { - vst.setParameter(i, fxp.readFloat()); + if (isRegularChunk) { + for (int i = 0; i < numParameters; i++) { + vst.setParameter(i, fxp.readFloat()); + } + } else { + byte[] chunkData = new byte[fxp.readInt()]; + fxp.read(chunkData); + vst.setProgramChunk(chunkData); } - } else { - byte[] chunkData = new byte[fxp.readInt()]; - fxp.read(chunkData); - vst.setProgramChunk(chunkData); + } finally { + fxp.close(); } - - fxp.close(); } /** * Loads a preset bank from file to the current bank of the plugin. * @param vst The plugin to load the data into. * @param file The file to read which contains the preset data. - * @author Uri Shaked + * @author Uri Shaked */ public static void loadBank(JVstHost2 vst, File file) throws DataFormatException, IOException { if (vst == null) { @@ -167,78 +164,75 @@ public static void loadBank(JVstHost2 vst, File file) throws DataFormatException DataInputStream fxp = new DataInputStream(new FileInputStream(file)); - byte[] fourBytes = new byte[4]; + try { + byte[] fourBytes = new byte[4]; - fxp.read(fourBytes); - if (!CHUNK_MAGIC.equals(new String(fourBytes))) { - fxp.close(); - throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag."); - } - - // fileLength is read an assigned to a variable for debugging purposes only - @SuppressWarnings("unused") - int fileLength = fxp.readInt(); + fxp.read(fourBytes); + if (!CHUNK_MAGIC.equals(new String(fourBytes))) { + throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag."); + } - fxp.read(fourBytes); - String chunkDataType = new String(fourBytes); - boolean isRegularChunk = true; - if (REGULAR_BANK_MAGIC.equals(chunkDataType)) { - isRegularChunk = true; - } else if (OPAQUE_BANK_MAGIC.equals(chunkDataType)) { - if (!vst.acceptsProgramsAsChunks()) { - fxp.close(); - throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks."); + // fileLength is read an assigned to a variable for debugging purposes only + @SuppressWarnings("unused") + int fileLength = fxp.readInt(); + + fxp.read(fourBytes); + String chunkDataType = new String(fourBytes); + boolean isRegularChunk = true; + if (REGULAR_BANK_MAGIC.equals(chunkDataType)) { + isRegularChunk = true; + } else if (OPAQUE_BANK_MAGIC.equals(chunkDataType)) { + if (!vst.acceptsProgramsAsChunks()) { + throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks."); + } else { + isRegularChunk = false; + } } else { - isRegularChunk = false; + throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks."); } - } else { - throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks."); - } - - int fileVersion = fxp.readInt(); - if (fileVersion > 2) { - fxp.close(); - throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported."); - } - - // unique id - fxp.read(fourBytes); - String uniqueId = new String(fourBytes); - if (!vst.getUniqueId().equals(uniqueId)) { - fxp.close(); - throw new DataFormatException("Unique plugin ID in file does not match given plugin. " + - "Is this file really for " + vst.getEffectName() + "?"); - } - int filePluginVersion = fxp.readInt(); - if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) { - fxp.close(); - throw new DataFormatException("This file contains data for a later plugin version " + - Integer.toString(filePluginVersion) + ", and the given plugin is only version " + - Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin."); - } - - int numPrograms = fxp.readInt(); + int fileVersion = fxp.readInt(); + if (fileVersion > 2) { + throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported."); + } - int currentProgram = fxp.readInt(); + // unique id + fxp.read(fourBytes); + String uniqueId = new String(fourBytes); + if (!vst.getUniqueId().equals(uniqueId)) { + throw new DataFormatException("Unique plugin ID in file does not match given plugin. " + + "Is this file really for " + vst.getEffectName() + "?"); + } - // reserved (zero) - fxp.read(new byte[124]); + int filePluginVersion = fxp.readInt(); + if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) { + throw new DataFormatException("This file contains data for a later plugin version " + + Integer.toString(filePluginVersion) + ", and the given plugin is only version " + + Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin."); + } - if (isRegularChunk) { - for (int i = currentProgram; i < currentProgram + numPrograms; i++) { - vst.setProgram(i); - for (int j = 0; j < vst.numParameters(); j++) { - vst.setParameter(i, fxp.readFloat()); + int numPrograms = fxp.readInt(); + + int currentProgram = fxp.readInt(); + + // reserved (zero) + fxp.read(new byte[124]); + + if (isRegularChunk) { + for (int i = currentProgram; i < currentProgram + numPrograms; i++) { + vst.setProgram(i); + for (int j = 0; j < vst.numParameters(); j++) { + vst.setParameter(i, fxp.readFloat()); + } } + } else { + byte[] chunkData = new byte[fxp.readInt()]; + fxp.read(chunkData); + vst.setBankChunk(chunkData); } - } else { - byte[] chunkData = new byte[fxp.readInt()]; - fxp.read(chunkData); - vst.setBankChunk(chunkData); + } finally { + fxp.close(); } - - fxp.close(); } /**