Repository: expo/expo
File: packages/expo-audio/android/src/main/java/expo/modules/audio/AudioPreloadManager.kt (Kotlin)

Code before:
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
object AudioPreloadManager {
  private val store = ConcurrentHashMap<String, ByteArray>()

  suspend fun preload(uri: String, factory: DataSource.Factory) =
    withContext(Dispatchers.IO) {
      val dataSource = factory.createDataSource()
      val dataSpec = DataSpec.Builder().setUri(uri.toUri()).build()

      val outputStream = ByteArrayOutputStream()
      try {
        dataSource.open(dataSpec)
        val buffer = ByteArray(8 * 1024)
        generateSequence {
          dataSource.read(buffer, 0, buffer.size).takeIf {
            it != -1
          }
        }.forEach {
          outputStream.write(buffer, 0, it)
        }
      } finally {
        dataSource.close()
      }

      store[uri] = outputStream.toByteArray()
    }

  fun get(uri: String): ByteArray? = store[uri]

  fun clearSource(uri: String) = store.remove(uri)

  fun clearAll() = store.clear()

  fun getPreloadedSources(): List<String> = store.keys().toList()
}

@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class InMemoryDataSourceFactory(private val data: ByteArray) : DataSource.Factory {
  override fun createDataSource(): DataSource = ByteArrayDataSource(data)
}


Review comment (suggestion):
```suggestion
  inline fun <T : DataSource, R> T.use(block: (T) -> R): R {                                                                                                                                                       
    try {                                                                                                                                                                                                          
      return block(this)                                                                                                                                                                                           
    } finally {                                                                                                                                                                                                    
      close()                                                                                                                                                                                                      
    }                                                                                                                                                                                                              
  }

  suspend fun preload(uri: String, factory: DataSource.Factory) =                                                                                                                                                  
    withContext(Dispatchers.IO) {                                                                                                                                                                                  
      val dataSpec = DataSpec.Builder().setUri(uri.toUri()).build()                                                                                                                                                
                                                                                                                                                                                                                   
      factory.createDataSource().use { dataSource ->                                                                                                                                                               
        ByteArrayOutputStream().use { outputStream ->                                                                                                                                                              
          dataSource.open(dataSpec)                                                                                                                                                                                
          val buffer = ByteArray(8 * 1024)                                                                                                                                                                         
          generateSequence {                                                                                                                                                                                       
            dataSource
              .read(buffer, 0, buffer.size)
              .takeIf { it != -1 }                                                                                                                                            
          }.forEach {                                                                                                                                                                                              
            outputStream.write(buffer, 0, it)                                                                                                                                                                      
          }                                                                                                                                                                                                        
          store[uri] = outputStream.toByteArray()                                                                                                                                                                  
        }                                                                                                                                                                                                          
      }                                                                                                                                                                                                            
    } 
```
I think that is cleaner

Code after:
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
object AudioPreloadManager {
  private val store = ConcurrentHashMap<String, ByteArray>()

  private inline fun <T : DataSource, R> T.use(block: (T) -> R): R {
    try {
      return block(this)
    } finally {
      close()
    }
  }

  suspend fun preload(uri: String, factory: DataSource.Factory) = withContext(Dispatchers.IO) {
    val dataSpec = DataSpec.Builder().setUri(uri.toUri()).build()

    factory.createDataSource().use { dataSource ->
      ByteArrayOutputStream().use { outputStream ->
        dataSource.open(dataSpec)
        val buffer = ByteArray(8 * 1024)
        generateSequence {
          dataSource
            .read(buffer, 0, buffer.size)
            .takeIf { it != -1 }
        }.forEach {
          outputStream.write(buffer, 0, it)
        }
        store[uri] = outputStream.toByteArray()
      }
    }
  }

  fun get(uri: String): ByteArray? = store[uri]

  fun clearSource(uri: String) = store.remove(uri)

  fun clearAll() = store.clear()

  fun getPreloadedSources(): List<String> = store.keys().toList()
}

@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class InMemoryDataSourceFactory(private val data: ByteArray) : DataSource.Factory {
  override fun createDataSource(): DataSource = ByteArrayDataSource(data)
}