Skip to content

Commit

Permalink
Close streams properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 9, 2024
1 parent 631a90f commit 9e24d30
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
package net.raphimc.noteblocktool.audio.soundsystem.impl;

import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.raphimc.noteblocklib.util.Instrument;
import net.raphimc.noteblocktool.audio.SoundMap;
import net.raphimc.noteblocktool.audio.soundsystem.SoundSystem;
import net.raphimc.noteblocktool.util.IOUtil;
import net.raphimc.noteblocktool.util.SampleOutputStream;
import net.raphimc.noteblocktool.util.SoundSampleUtil;
import org.lwjgl.openal.*;
Expand Down Expand Up @@ -259,7 +259,7 @@ private int loadAudioFile(final InputStream inputStream) {
final AudioInputStream audioInputStream = SoundSampleUtil.readAudioFile(inputStream);
final AudioFormat audioFormat = audioInputStream.getFormat();

final byte[] audioBytes = ByteStreams.toByteArray(audioInputStream);
final byte[] audioBytes = IOUtil.readFully(audioInputStream);
final ByteBuffer audioBuffer = MemoryUtil.memAlloc(audioBytes.length).put(audioBytes);
audioBuffer.flip();
AL10.alBufferData(buffer, this.getAlAudioFormat(audioFormat), audioBuffer, (int) audioFormat.getSampleRate());
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/net/raphimc/noteblocktool/util/IOUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of NoteBlockTool - https://github.com/RaphiMC/NoteBlockTool
* Copyright (C) 2022-2024 RK_01/RaphiMC and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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 <http://www.gnu.org/licenses/>.
*/
package net.raphimc.noteblocktool.util;

import com.google.common.io.ByteStreams;

import java.io.IOException;
import java.io.InputStream;

public class IOUtil {

public static byte[] readFully(final InputStream inputStream) throws IOException {
try {
return ByteStreams.toByteArray(inputStream);
} finally {
inputStream.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package net.raphimc.noteblocktool.util;

import com.google.common.io.ByteStreams;
import org.lwjgl.PointerBuffer;
import org.lwjgl.stb.STBVorbis;
import org.lwjgl.system.MemoryStack;
Expand Down Expand Up @@ -46,7 +45,7 @@ public static AudioInputStream readAudioFile(final InputStream inputStream) thro
bis.read(magic);
bis.reset();
if (Arrays.equals(magic, OGG_MAGIC)) {
final byte[] data = ByteStreams.toByteArray(bis);
final byte[] data = IOUtil.readFully(bis);
final ByteBuffer dataBuffer = (ByteBuffer) MemoryUtil.memAlloc(data.length).put(data).flip();
try (MemoryStack memoryStack = MemoryStack.stackPush()) {
final IntBuffer channels = memoryStack.callocInt(1);
Expand Down Expand Up @@ -77,7 +76,7 @@ public static AudioInputStream readAudioFile(final InputStream inputStream) thro
public static int[] readSamples(final InputStream inputStream, final AudioFormat targetFormat) throws UnsupportedAudioFileException, IOException {
AudioInputStream in = readAudioFile(inputStream);
if (!in.getFormat().matches(targetFormat)) in = AudioSystem.getAudioInputStream(targetFormat, in);
final byte[] audioBytes = ByteStreams.toByteArray(in);
final byte[] audioBytes = IOUtil.readFully(in);
final SampleInputStream sis = new SampleInputStream(new ByteArrayInputStream(audioBytes), targetFormat);

final int sampleSize = targetFormat.getSampleSizeInBits() / 8;
Expand All @@ -86,6 +85,7 @@ public static int[] readSamples(final InputStream inputStream, final AudioFormat
samples[i] = sis.readSample();
}

sis.close();
return samples;
}

Expand Down

0 comments on commit 9e24d30

Please sign in to comment.