Merge pull request #3909 from stejbac/fix-silent-sound-player-resource-leak

Fix potential resource leak in AvoidStandbyModeService
This commit is contained in:
Christoph Atteneder 2020-01-27 15:51:11 +01:00 committed by GitHub
commit 4243236107
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 51 deletions

View File

@ -22,6 +22,7 @@ import bisq.common.util.Utilities;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -32,6 +33,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
@ -80,7 +82,7 @@ public class FileUtil {
if (files != null) {
List<File> filesList = Arrays.asList(files);
if (filesList.size() > numMaxBackupFiles) {
filesList.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
filesList.sort(Comparator.comparing(File::getName));
File file = filesList.get(0);
if (file.isFile()) {
if (!file.delete())
@ -155,15 +157,12 @@ public class FileUtil {
}
public static void resourceToFile(String resourcePath, File destinationFile) throws ResourceNotFoundException, IOException {
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath);
if (inputStream == null)
try (InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath)) {
if (inputStream == null) {
throw new ResourceNotFoundException(resourcePath);
}
try (FileOutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
IOUtils.copy(inputStream, fileOutputStream);
}
}
}

View File

@ -20,17 +20,14 @@ package bisq.core.app;
import bisq.core.user.Preferences;
import bisq.common.config.Config;
import bisq.common.storage.FileUtil;
import bisq.common.storage.ResourceNotFoundException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import lombok.extern.slf4j.Slf4j;
@ -40,6 +37,7 @@ import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
@Slf4j
@ -81,23 +79,12 @@ public class AvoidStandbyModeService {
private void play() {
if (!isStopped) {
OutputStream outputStream = null;
InputStream inputStream = null;
try {
inputStream = getClass().getClassLoader().getResourceAsStream("prevent-app-nap-silent-sound.aiff");
File soundFile = new File(config.appDataDir, "prevent-app-nap-silent-sound.aiff");
if (!soundFile.exists()) {
outputStream = new FileOutputStream(soundFile);
IOUtils.copy(inputStream, outputStream);
}
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
byte tempBuffer[] = new byte[10000];
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
while (!isStopped) {
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getSoundFile());
SourceDataLine sourceDataLine = getSourceDataLine(audioInputStream.getFormat())) {
byte[] tempBuffer = new byte[10000];
sourceDataLine.open(audioInputStream.getFormat());
sourceDataLine.start();
int cnt;
while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1 && !isStopped) {
@ -106,20 +93,24 @@ public class AvoidStandbyModeService {
}
}
sourceDataLine.drain();
sourceDataLine.close();
play();
}
}
} catch (Exception e) {
log.error(e.toString());
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
if (outputStream != null)
outputStream.close();
} catch (IOException ignore) {
}
}
private File getSoundFile() throws IOException, ResourceNotFoundException {
File soundFile = new File(config.appDataDir, "prevent-app-nap-silent-sound.aiff");
if (!soundFile.exists()) {
FileUtil.resourceToFile("prevent-app-nap-silent-sound.aiff", soundFile);
}
return soundFile;
}
private SourceDataLine getSourceDataLine(AudioFormat audioFormat) throws LineUnavailableException {
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
return (SourceDataLine) AudioSystem.getLine(dataLineInfo);
}
}