}
public static void DecompressCore(IAPEDecompress spAPEDecompress, String pOutputFilename, int nOutputMode, int nCompressionLevel, ProgressCallback progressor) throws IOException, JMACSkippedException, JMACStoppedByUserException {
// variable declares
java.io.RandomAccessFile spioOutput = null;
IAPECompress spAPECompress = null;
try {
// create the core
WaveFormat wfeInput = spAPEDecompress.getApeInfoWaveFormatEx();
// allocate space for the header
byte[] waveHeaderBuffer = spAPEDecompress.getApeInfoWavHeaderData(spAPEDecompress.getApeInfoWavHeaderBytes());
// initialize the output
if (nOutputMode == UNMAC_DECODER_OUTPUT_WAV) {
// create the file
spioOutput = new RandomAccessFile(pOutputFilename, "rw");
// output the header
spioOutput.write(waveHeaderBuffer);
} else if (nOutputMode == UNMAC_DECODER_OUTPUT_APE) {
// quit if there is nothing to do
if (spAPEDecompress.getApeInfoFileVersion() == Globals.MAC_VERSION_NUMBER && spAPEDecompress.getApeInfoCompressionLevel() == nCompressionLevel)
throw new JMACSkippedException();
// create and start the compressor
spAPECompress = IAPECompress.CreateIAPECompress();
spAPECompress.Start(pOutputFilename, wfeInput, spAPEDecompress.getApeInfoDecompressTotalBlocks() * spAPEDecompress.getApeInfoBlockAlign(),
nCompressionLevel, waveHeaderBuffer, spAPEDecompress.getApeInfoWavHeaderBytes());
}
int blockAlign = spAPEDecompress.getApeInfoBlockAlign();
// allocate space for decompression
byte[] spTempBuffer = new byte[blockAlign * BLOCKS_PER_DECODE];
int nBlocksLeft = spAPEDecompress.getApeInfoDecompressTotalBlocks();
// create the progress helper
ProgressHelper spMACProgressHelper = new ProgressHelper(nBlocksLeft / BLOCKS_PER_DECODE, progressor);
// main decoding loop
while (nBlocksLeft > 0) {
// decode data
int nBlocksDecoded = spAPEDecompress.GetData(spTempBuffer, BLOCKS_PER_DECODE);
// handle the output
if (nOutputMode == UNMAC_DECODER_OUTPUT_WAV)
spioOutput.write(spTempBuffer, 0, nBlocksDecoded * blockAlign);
else if (nOutputMode == UNMAC_DECODER_OUTPUT_APE)
spAPECompress.AddData(spTempBuffer, nBlocksDecoded * spAPEDecompress.getApeInfoBlockAlign());
// update amount remaining
nBlocksLeft -= nBlocksDecoded;
// update progress and kill flag
spMACProgressHelper.UpdateProgress();
if (spMACProgressHelper.isKillFlag())
throw new JMACStoppedByUserException();
}
// terminate the output
if (nOutputMode == UNMAC_DECODER_OUTPUT_WAV) {
// write any terminating WAV data
if (spAPEDecompress.getApeInfoWavTerminatingBytes() > 0) {
byte[] termData = spAPEDecompress.getApeInfoWavTerminatingData(spAPEDecompress.getApeInfoWavTerminatingBytes());
int nBytesToWrite = spAPEDecompress.getApeInfoWavTerminatingBytes();
spioOutput.write(termData, 0, nBytesToWrite);
}
} else if (nOutputMode == UNMAC_DECODER_OUTPUT_APE) {
// write the WAV data and any tag
int nTagBytes = spAPEDecompress.getApeInfoTag().GetTagBytes();
boolean bHasTag = (nTagBytes > 0);
int nTerminatingBytes = nTagBytes;
nTerminatingBytes += spAPEDecompress.getApeInfoWavTerminatingBytes();
if (nTerminatingBytes > 0) {
spTempBuffer = spAPEDecompress.getApeInfoWavTerminatingData(nTerminatingBytes);
if (bHasTag) {
spAPEDecompress.getApeInfoIoSource().seek(spAPEDecompress.getApeInfoIoSource().length() - nTagBytes);
spAPEDecompress.getApeInfoIoSource().read(spTempBuffer, spAPEDecompress.getApeInfoWavTerminatingBytes(), nTagBytes);
}
spAPECompress.Finish(spTempBuffer, nTerminatingBytes, spAPEDecompress.getApeInfoWavTerminatingBytes());
} else
spAPECompress.Finish(null, 0, 0);
}
// fire the "complete" progress notification
spMACProgressHelper.UpdateProgressComplete();
} finally {
if (spioOutput != null)
spioOutput.close();
if (spAPECompress != null)
spAPECompress.Kill();
}
}