diff --git a/trunk/src/com/jpexs/decompiler/flash/SWF.java b/trunk/src/com/jpexs/decompiler/flash/SWF.java index f66bcdebc..021acc1de 100644 --- a/trunk/src/com/jpexs/decompiler/flash/SWF.java +++ b/trunk/src/com/jpexs/decompiler/flash/SWF.java @@ -112,6 +112,7 @@ import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; +import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -992,57 +993,52 @@ public final class SWF { public byte[] exportSound(Tag t) throws IOException { boolean mp3 = true; boolean wave = true; - ByteArrayOutputStream fos = new ByteArrayOutputStream(); + try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) { - if (t instanceof DefineSoundTag) { - DefineSoundTag st = (DefineSoundTag) t; - if ((st.soundFormat == DefineSoundTag.FORMAT_ADPCM) && wave) { - fos = new ByteArrayOutputStream(); - createWavFromAdpcm(fos, st.soundRate, st.soundSize, st.soundType, st.soundData); - } else if ((st.soundFormat == DefineSoundTag.FORMAT_MP3) && mp3) { - fos = new ByteArrayOutputStream(); - fos.write(st.soundData, 2, st.soundData.length - 2); - } else { - fos = new ByteArrayOutputStream(); - FLVOutputStream flv = new FLVOutputStream(fos); - flv.writeHeader(true, false); - flv.writeTag(new FLVTAG(0, new AUDIODATA(st.soundFormat, st.soundRate, st.soundSize, st.soundType, st.soundData))); + if (t instanceof DefineSoundTag) { + DefineSoundTag st = (DefineSoundTag) t; + if ((st.soundFormat == DefineSoundTag.FORMAT_ADPCM) && wave) { + createWavFromAdpcm(fos, st.soundRate, st.soundSize, st.soundType, st.soundData); + } else if ((st.soundFormat == DefineSoundTag.FORMAT_MP3) && mp3) { + fos.write(st.soundData, 2, st.soundData.length - 2); + } else { + FLVOutputStream flv = new FLVOutputStream(fos); + flv.writeHeader(true, false); + flv.writeTag(new FLVTAG(0, new AUDIODATA(st.soundFormat, st.soundRate, st.soundSize, st.soundType, st.soundData))); + } } - } - if (t instanceof SoundStreamHeadTypeTag) { - SoundStreamHeadTypeTag shead = (SoundStreamHeadTypeTag) t; - List blocks = new ArrayList<>(); - populateSoundStreamBlocks(this.tags, t, blocks); - if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_ADPCM) && wave) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for (int b = 0; b < blocks.size(); b++) { - byte[] data = blocks.get(b).getData(SWF.DEFAULT_VERSION); - baos.write(data); - } - fos = new ByteArrayOutputStream(); - createWavFromAdpcm(fos, shead.getSoundRate(), shead.getSoundSize(), shead.getSoundType(), baos.toByteArray()); - } else if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_MP3) && mp3) { - fos = new ByteArrayOutputStream(); - for (int b = 0; b < blocks.size(); b++) { - byte[] data = blocks.get(b).getData(SWF.DEFAULT_VERSION); - fos.write(data, 4, data.length - 4); - } - } else { - fos = new ByteArrayOutputStream(); - FLVOutputStream flv = new FLVOutputStream(fos); - flv.writeHeader(true, false); - - int ms = (int) (1000.0f / ((float) frameRate)); - for (int b = 0; b < blocks.size(); b++) { - byte[] data = blocks.get(b).getData(SWF.DEFAULT_VERSION); - if (shead.getSoundFormat() == 2) { //MP3 - data = Arrays.copyOfRange(data, 4, data.length); + if (t instanceof SoundStreamHeadTypeTag) { + SoundStreamHeadTypeTag shead = (SoundStreamHeadTypeTag) t; + List blocks = new ArrayList<>(); + populateSoundStreamBlocks(this.tags, t, blocks); + if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_ADPCM) && wave) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + for (int b = 0; b < blocks.size(); b++) { + byte[] data = blocks.get(b).getData(SWF.DEFAULT_VERSION); + baos.write(data); + } + createWavFromAdpcm(fos, shead.getSoundRate(), shead.getSoundSize(), shead.getSoundType(), baos.toByteArray()); + } else if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_MP3) && mp3) { + for (int b = 0; b < blocks.size(); b++) { + byte[] data = blocks.get(b).getData(SWF.DEFAULT_VERSION); + fos.write(data, 4, data.length - 4); + } + } else { + FLVOutputStream flv = new FLVOutputStream(fos); + flv.writeHeader(true, false); + + int ms = (int) (1000.0f / ((float) frameRate)); + for (int b = 0; b < blocks.size(); b++) { + byte[] data = blocks.get(b).getData(SWF.DEFAULT_VERSION); + if (shead.getSoundFormat() == 2) { //MP3 + data = Arrays.copyOfRange(data, 4, data.length); + } + flv.writeTag(new FLVTAG(ms * b, new AUDIODATA(shead.getSoundFormat(), shead.getSoundRate(), shead.getSoundSize(), shead.getSoundType(), data))); } - flv.writeTag(new FLVTAG(ms * b, new AUDIODATA(shead.getSoundFormat(), shead.getSoundRate(), shead.getSoundSize(), shead.getSoundType(), data))); } } + return fos.toByteArray(); } - return fos.toByteArray(); } private static void writeLE(OutputStream os, long val, int size) throws IOException { @@ -1053,50 +1049,40 @@ public final class SWF { } private static void createWavFromAdpcm(OutputStream fos, int soundRate, int soundSize, int soundType, byte[] data) throws IOException { - try { - byte[] pcmData = AdpcmDecoder.decode(data, soundType == 1 ? true : false); + byte[] pcmData = AdpcmDecoder.decode(data, soundType == 1 ? true : false); - ByteArrayOutputStream subChunk1Data = new ByteArrayOutputStream(); - int audioFormat = 1; //PCM - writeLE(subChunk1Data, audioFormat, 2); - int numChannels = soundType == 1 ? 2 : 1; - writeLE(subChunk1Data, numChannels, 2); - int[] rateMap = {5512, 11025, 22050, 44100}; - int sampleRate = rateMap[soundRate]; - writeLE(subChunk1Data, sampleRate, 4); - int bitsPerSample = soundSize == 1 ? 16 : 8; - int byteRate = sampleRate * numChannels * bitsPerSample / 8; - writeLE(subChunk1Data, byteRate, 4); - int blockAlign = numChannels * bitsPerSample / 8; - writeLE(subChunk1Data, blockAlign, 2); - writeLE(subChunk1Data, bitsPerSample, 2); + ByteArrayOutputStream subChunk1Data = new ByteArrayOutputStream(); + int audioFormat = 1; //PCM + writeLE(subChunk1Data, audioFormat, 2); + int numChannels = soundType == 1 ? 2 : 1; + writeLE(subChunk1Data, numChannels, 2); + int[] rateMap = {5512, 11025, 22050, 44100}; + int sampleRate = rateMap[soundRate]; + writeLE(subChunk1Data, sampleRate, 4); + int bitsPerSample = soundSize == 1 ? 16 : 8; + int byteRate = sampleRate * numChannels * bitsPerSample / 8; + writeLE(subChunk1Data, byteRate, 4); + int blockAlign = numChannels * bitsPerSample / 8; + writeLE(subChunk1Data, blockAlign, 2); + writeLE(subChunk1Data, bitsPerSample, 2); - ByteArrayOutputStream chunks = new ByteArrayOutputStream(); - chunks.write(Utf8Helper.getBytes("fmt ")); - byte[] subChunk1DataBytes = subChunk1Data.toByteArray(); - writeLE(chunks, subChunk1DataBytes.length, 4); - chunks.write(subChunk1DataBytes); + ByteArrayOutputStream chunks = new ByteArrayOutputStream(); + chunks.write(Utf8Helper.getBytes("fmt ")); + byte[] subChunk1DataBytes = subChunk1Data.toByteArray(); + writeLE(chunks, subChunk1DataBytes.length, 4); + chunks.write(subChunk1DataBytes); - chunks.write(Utf8Helper.getBytes("data")); - writeLE(chunks, pcmData.length, 4); - chunks.write(pcmData); + chunks.write(Utf8Helper.getBytes("data")); + writeLE(chunks, pcmData.length, 4); + chunks.write(pcmData); - fos.write(Utf8Helper.getBytes("RIFF")); - byte[] chunkBytes = chunks.toByteArray(); - writeLE(fos, 4 + chunkBytes.length, 4); - fos.write(Utf8Helper.getBytes("WAVE")); - fos.write(chunkBytes); - //size1=>16bit*/ - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException ex) { - //ignore - } - } - } + fos.write(Utf8Helper.getBytes("RIFF")); + byte[] chunkBytes = chunks.toByteArray(); + writeLE(fos, 4 + chunkBytes.length, 4); + fos.write(Utf8Helper.getBytes("WAVE")); + fos.write(chunkBytes); + //size1=>16bit*/ } public List exportSounds(AbortRetryIgnoreHandler handler, String outdir, List tags, boolean mp3, boolean wave) throws IOException { @@ -1114,95 +1100,96 @@ public final class SWF { } for (Tag t : tags) { File newfile = null; - FileOutputStream fos = null; - //File file = null; - try { - int id = 0; - if (t instanceof DefineSoundTag) { - id = ((DefineSoundTag) t).soundId; - } + int id = 0; + if (t instanceof DefineSoundTag) { + id = ((DefineSoundTag) t).soundId; + } - if (t instanceof DefineSoundTag) { - final DefineSoundTag st = (DefineSoundTag) t; + if (t instanceof DefineSoundTag) { + final DefineSoundTag st = (DefineSoundTag) t; - if ((st.soundFormat == DefineSoundTag.FORMAT_ADPCM) && wave) { - final File file = new File(outdir + File.separator + st.getCharacterExportFileName() + ".wav"); - newfile = file; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - FileOutputStream fos = new FileOutputStream(file); - createWavFromAdpcm(fos, st.soundRate, st.soundSize, st.soundType, st.soundData); + if ((st.soundFormat == DefineSoundTag.FORMAT_ADPCM) && wave) { + final File file = new File(outdir + File.separator + st.getCharacterExportFileName() + ".wav"); + newfile = file; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + createWavFromAdpcm(os, st.soundRate, st.soundSize, st.soundType, st.soundData); } - }, handler).run(); - } else if ((st.soundFormat == DefineSoundTag.FORMAT_MP3) && mp3) { - final File file = new File(outdir + File.separator + st.getCharacterExportFileName() + ".mp3"); - newfile = file; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - FileOutputStream fos = new FileOutputStream(file); + } + }, handler).run(); + } else if ((st.soundFormat == DefineSoundTag.FORMAT_MP3) && mp3) { + final File file = new File(outdir + File.separator + st.getCharacterExportFileName() + ".mp3"); + newfile = file; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(st.soundData, 2, st.soundData.length - 2); } - }, handler).run(); - } else { - final File file = new File(outdir + File.separator + st.getCharacterExportFileName() + ".flv"); - newfile = file; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - FileOutputStream fos = new FileOutputStream(file); - try (FLVOutputStream flv = new FLVOutputStream(fos)) { - flv.writeHeader(true, false); - flv.writeTag(new FLVTAG(0, new AUDIODATA(st.soundFormat, st.soundRate, st.soundSize, st.soundType, st.soundData))); - } - } - }, handler).run(); - } - } - if (t instanceof SoundStreamHeadTypeTag) { - final SoundStreamHeadTypeTag shead = (SoundStreamHeadTypeTag) t; - final List blocks = new ArrayList<>(); - List objs = new ArrayList<>(); - objs.addAll(this.tags); - populateSoundStreamBlocks(objs, t, blocks); - if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_ADPCM) && wave) { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for (int b = 0; b < blocks.size(); b++) { - byte data[] = blocks.get(b).getData(SWF.DEFAULT_VERSION); - baos.write(data); } - final File file = new File(outdir + File.separator + id + ".wav"); - newfile = file; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - FileOutputStream fos = new FileOutputStream(file); + }, handler).run(); + } else { + final File file = new File(outdir + File.separator + st.getCharacterExportFileName() + ".flv"); + newfile = file; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + FileOutputStream fos = new FileOutputStream(file); + try (FLVOutputStream flv = new FLVOutputStream(fos)) { + flv.writeHeader(true, false); + flv.writeTag(new FLVTAG(0, new AUDIODATA(st.soundFormat, st.soundRate, st.soundSize, st.soundType, st.soundData))); + } + } + }, handler).run(); + } + } + if (t instanceof SoundStreamHeadTypeTag) { + final SoundStreamHeadTypeTag shead = (SoundStreamHeadTypeTag) t; + final List blocks = new ArrayList<>(); + List objs = new ArrayList<>(); + objs.addAll(this.tags); + populateSoundStreamBlocks(objs, t, blocks); + if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_ADPCM) && wave) { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + for (int b = 0; b < blocks.size(); b++) { + byte data[] = blocks.get(b).getData(SWF.DEFAULT_VERSION); + baos.write(data); + } + final File file = new File(outdir + File.separator + id + ".wav"); + newfile = file; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) { createWavFromAdpcm(fos, shead.getSoundRate(), shead.getSoundSize(), shead.getSoundType(), baos.toByteArray()); } - }, handler).run(); - } else if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_MP3) && mp3) { - final File file = new File(outdir + File.separator + id + ".mp3"); - newfile = file; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - FileOutputStream fos = new FileOutputStream(file); + } + }, handler).run(); + } else if ((shead.getSoundFormat() == DefineSoundTag.FORMAT_MP3) && mp3) { + final File file = new File(outdir + File.separator + id + ".mp3"); + newfile = file; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { for (int b = 0; b < blocks.size(); b++) { byte data[] = blocks.get(b).getData(SWF.DEFAULT_VERSION); - fos.write(data, 2, data.length - 2); + os.write(data, 2, data.length - 2); } } - }, handler).run(); - } else { - final File file = new File(outdir + File.separator + id + ".flv"); - newfile = file; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - FileOutputStream fos = new FileOutputStream(file); - FLVOutputStream flv = new FLVOutputStream(fos); + } + }, handler).run(); + } else { + final File file = new File(outdir + File.separator + id + ".flv"); + newfile = file; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + FLVOutputStream flv = new FLVOutputStream(os); flv.writeHeader(true, false); int ms = (int) (1000.0f / ((float) frameRate)); @@ -1214,16 +1201,8 @@ public final class SWF { flv.writeTag(new FLVTAG(ms * b, new AUDIODATA(shead.getSoundFormat(), shead.getSoundRate(), shead.getSoundSize(), shead.getSoundType(), data))); } } - }, handler).run(); - } - } - } finally { - if (fos != null) { - try { - fos.close(); - } catch (Exception ignore) { - //ignore - } + } + }, handler).run(); } } if (newfile != null) { diff --git a/trunk/src/com/jpexs/decompiler/flash/configuration/Configuration.java b/trunk/src/com/jpexs/decompiler/flash/configuration/Configuration.java index 8c6fd540c..85bcb294c 100644 --- a/trunk/src/com/jpexs/decompiler/flash/configuration/Configuration.java +++ b/trunk/src/com/jpexs/decompiler/flash/configuration/Configuration.java @@ -276,7 +276,7 @@ public class Configuration { } } } else { - try (PrintWriter pw = new PrintWriter(new Utf8OutputStreamWriter(new FileOutputStream(replacementsFile)))) { + try (PrintWriter pw = new PrintWriter(new Utf8OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(replacementsFile))))) { for (Replacement r : replacements) { pw.println(r.urlPattern); pw.println(r.targetFile); @@ -349,7 +349,7 @@ public class Configuration { Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex); } } - try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { + try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { oos.writeObject(config); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Cannot save configuration.", "Error", JOptionPane.ERROR_MESSAGE); diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/MainFrame.java b/trunk/src/com/jpexs/decompiler/flash/gui/MainFrame.java index 941559318..c74d456f2 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/MainFrame.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/MainFrame.java @@ -105,6 +105,7 @@ import com.jpexs.helpers.AsyncResult; import com.jpexs.helpers.Cache; import com.jpexs.helpers.Callback; import com.jpexs.helpers.Helper; +import com.jpexs.helpers.Stopwatch; import com.jpexs.process.ProcessTools; import java.awt.BorderLayout; import java.awt.CardLayout; @@ -147,10 +148,12 @@ import java.awt.event.WindowEvent; import java.awt.event.WindowStateListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; @@ -3230,13 +3233,9 @@ public final class MainFrame extends AppRibbonFrame implements ActionListener, T if (tagObj instanceof DefineSoundTag) { frameCount = 1; } - try (FileOutputStream fos = new FileOutputStream(tempFile)) { - SWFOutputStream sos = new SWFOutputStream(fos, 10); - sos.write("FWS".getBytes()); - sos.write(swf.version); - - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte[] data; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { SWFOutputStream sos2 = new SWFOutputStream(baos, 10); int width = swf.displayRect.Xmax - swf.displayRect.Xmin; int height = swf.displayRect.Ymax - swf.displayRect.Ymin; @@ -3547,8 +3546,13 @@ public final class MainFrame extends AppRibbonFrame implements ActionListener, T }//not showframe sos2.writeTag(new EndTag(null)); - byte[] data = baos.toByteArray(); + data = baos.toByteArray(); + } + try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(tempFile))) { + SWFOutputStream sos = new SWFOutputStream(fos, 10); + sos.write("FWS".getBytes()); + sos.write(swf.version); sos.writeUI32(sos.getPos() + data.length + 4); sos.write(data); fos.flush(); diff --git a/trunk/src/com/jpexs/helpers/Stopwatch.java b/trunk/src/com/jpexs/helpers/Stopwatch.java new file mode 100644 index 000000000..48f6eace0 --- /dev/null +++ b/trunk/src/com/jpexs/helpers/Stopwatch.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2010-2013 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.helpers; + +import java.util.Date; + +/** + * + * @author JPEXS + */ +public class Stopwatch { + + private long startTime, elapsedTime; + private boolean running; + public Date startDate, endDate; + + public static Stopwatch startNew() { + Stopwatch sw = new Stopwatch(); + sw.start(); + return sw; + } + + public void start() { + running = true; + startDate = new Date(); + startTime = System.nanoTime(); + } + + public void stop() { + elapsedTime = System.nanoTime() - startTime; + endDate = new Date(); + running = false; + } + + public long getElapsedNanoseconds() { + if (running) { + return System.nanoTime() - startTime; + } + return elapsedTime; + } + + public long getElapsedMilliseconds() { + return getElapsedNanoseconds() / 1000000; + } +}