| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import java.io.BufferedInputStream;
|
| 4 | import java.io.FilterInputStream;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStream;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import javax.sound.sampled.AudioFormat;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class LoopingAudioStream implements AudioStream {
|
| 14 | private final LoopingAudioStream.AudioStreamProvider provider;
|
| 15 | private AudioStream stream;
|
| 16 | private final BufferedInputStream bufferedInputStream;
|
| 17 |
|
| 18 | public LoopingAudioStream(final LoopingAudioStream.AudioStreamProvider provider, final InputStream originalInputStream) throws IOException {
|
| 19 | this.provider = provider;
|
| 20 | this.bufferedInputStream = new BufferedInputStream(originalInputStream);
|
| 21 | this.bufferedInputStream.mark(Integer.MAX_VALUE);
|
| 22 | this.stream = provider.create(new LoopingAudioStream.NoCloseBuffer(this.bufferedInputStream));
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public AudioFormat getFormat() {
|
| 27 | return this.stream.getFormat();
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public ByteBuffer read(final int expectedSize) throws IOException {
|
| 32 | ByteBuffer result = this.stream.read(expectedSize);
|
| 33 | if (!result.hasRemaining()) {
|
| 34 | this.stream.close();
|
| 35 | this.bufferedInputStream.reset();
|
| 36 | this.stream = this.provider.create(new LoopingAudioStream.NoCloseBuffer(this.bufferedInputStream));
|
| 37 | result = this.stream.read(expectedSize);
|
| 38 | }
|
| 39 |
|
| 40 | return result;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void close() throws IOException {
|
| 45 | this.stream.close();
|
| 46 | this.bufferedInputStream.close();
|
| 47 | }
|
| 48 |
|
| 49 | @FunctionalInterface
|
| 50 | @Environment(EnvType.CLIENT)
|
| 51 | public interface AudioStreamProvider {
|
| 52 | AudioStream create(final InputStream inputStream) throws IOException;
|
| 53 | }
|
| 54 |
|
| 55 | @Environment(EnvType.CLIENT)
|
| 56 | private static class NoCloseBuffer extends FilterInputStream {
|
| 57 | private NoCloseBuffer(final InputStream in) {
|
| 58 | super(in);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void close() {
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|