2828public class AudioFileStreaming implements AudioFile , GeneralAudioStream {
2929 private static final Logger LOGGER = Logger .getLogger (AudioFileStreaming .class );
3030 private final CacheManager .Handler cacheHandler ;
31- private final ByteString fileId ;
31+ private final Metadata . AudioFile file ;
3232 private final byte [] key ;
3333 private final Session session ;
3434 private final ExecutorService executorService = Executors .newCachedThreadPool (new NameThreadFactory (r -> "request-chunk-" + r .hashCode ()));
@@ -37,19 +37,19 @@ public class AudioFileStreaming implements AudioFile, GeneralAudioStream {
3737
3838 public AudioFileStreaming (@ NotNull Session session , @ NotNull Metadata .AudioFile file , byte [] key ) throws IOException {
3939 this .session = session ;
40- this .fileId = file .getFileId ();
41- this .cacheHandler = session . cache (). forFileId ( Utils . bytesToHex ( fileId )) ;
40+ this .cacheHandler = session . cache (). forFileId ( Utils . bytesToHex ( file .getFileId ()) );
41+ this .file = file ;
4242 this .key = key ;
4343 }
4444
4545 @ Override
4646 public @ NotNull SuperAudioFormat codec () {
47- return SuperAudioFormat .VORBIS ;
47+ return SuperAudioFormat .get ( file . getFormat ()) ;
4848 }
4949
5050 @ Override
5151 public @ NotNull String describe () {
52- return "{fileId: " + Utils .bytesToHex (fileId ) + "}" ;
52+ return "{fileId: " + Utils .bytesToHex (file . getFileId () ) + "}" ;
5353 }
5454
5555 @ NotNull
@@ -58,18 +58,24 @@ public InputStream stream() {
5858 return chunksBuffer .stream ();
5959 }
6060
61- private void requestChunk (@ NotNull ByteString fileId , int index , @ NotNull AudioFile file ) throws IOException {
62- if (cacheHandler == null || !tryCacheChunk (index ))
63- session .channel ().requestChunk (fileId , index , file );
61+ private void requestChunk (@ NotNull ByteString fileId , int index , @ NotNull AudioFile file ) {
62+ if (cacheHandler == null || !tryCacheChunk (index )) {
63+ try {
64+ session .channel ().requestChunk (fileId , index , file );
65+ } catch (IOException ex ) {
66+ LOGGER .fatal (String .format ("Failed requesting chunk from network, index: %d" , index ), ex );
67+ }
68+ }
6469 }
6570
66- private boolean tryCacheChunk (int index ) throws IOException {
71+ private boolean tryCacheChunk (int index ) {
6772 try {
6873 if (!cacheHandler .hasChunk (index )) return false ;
6974 cacheHandler .readChunk (index , this );
7075 return true ;
71- } catch (SQLException ex ) {
72- throw new IOException (ex );
76+ } catch (SQLException | IOException ex ) {
77+ LOGGER .fatal (String .format ("Failed requesting chunk from cache, index: %d" , index ), ex );
78+ return false ;
7379 }
7480 }
7581
@@ -92,7 +98,7 @@ private boolean tryCacheHeaders(@NotNull AudioFileFetch fetch) throws IOExceptio
9298 private AudioFileFetch requestHeaders () throws IOException {
9399 AudioFileFetch fetch = new AudioFileFetch (cacheHandler );
94100 if (cacheHandler == null || !tryCacheHeaders (fetch ))
95- requestChunk (fileId , 0 , fetch );
101+ requestChunk (file . getFileId () , 0 , fetch );
96102
97103 fetch .waitChunk ();
98104 return fetch ;
@@ -112,8 +118,8 @@ public void open() throws IOException {
112118 chunksBuffer .internalStream .waitFor (0 );
113119 }
114120
115- private void requestChunk (int index ) throws IOException {
116- requestChunk (fileId , index , this );
121+ private void requestChunk (int index ) {
122+ requestChunk (file . getFileId () , index , this );
117123 chunksBuffer .requested [index ] = true ; // Just to be sure
118124 }
119125
@@ -128,7 +134,7 @@ public void writeChunk(byte[] buffer, int chunkIndex, boolean cached) throws IOE
128134 }
129135
130136 chunksBuffer .writeChunk (buffer , chunkIndex );
131- LOGGER .trace (String .format ("Chunk %d/%d completed, cached: %b, fileId: %s" , chunkIndex , chunks , cached , Utils .bytesToHex (fileId )));
137+ LOGGER .trace (String .format ("Chunk %d/%d completed, cached: %b, fileId: %s" , chunkIndex , chunks , cached , Utils .bytesToHex (file . getFileId () )));
132138 }
133139
134140 @ Override
@@ -216,13 +222,7 @@ protected int chunks() {
216222
217223 @ Override
218224 protected void requestChunkFromStream (int index ) {
219- executorService .submit (() -> {
220- try {
221- requestChunk (index );
222- } catch (IOException ex ) {
223- LOGGER .fatal (String .format ("Failed requesting chunk, index: %d" , index ), ex );
224- }
225- });
225+ executorService .submit (() -> requestChunk (index ));
226226 }
227227 }
228228 }
0 commit comments