Skip to content

Commit

Permalink
Let the id3 reader accept a file, not an input stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Jul 30, 2017
1 parent a60e5e5 commit a6d1c27
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/de/ph1b/audiobook/features/BookAdder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ import javax.inject.Singleton
.blockingGet()
if (result is MediaAnalyzer.Result.Success) {
val marks = when (f.extension) {
"mp3" -> f.inputStream().use { iD3ChapterReader.readInputStream(it) }
"mp3" -> iD3ChapterReader.read(f)
"mp4", "m4a", "m4b", "aac" -> mp4ChapterReader.readChapters(f)
"opus", "ogg", "oga" -> f.inputStream().use { oggChapterReader.read(it) }
"mka", "mkv", "webm" -> matroskaChapterReader.read(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package de.ph1b.audiobook.chapterreader.id3
import dagger.Reusable
import de.ph1b.audiobook.common.Logger
import de.ph1b.audiobook.common.skipBytes
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.util.ArrayList
import javax.inject.Inject
import kotlin.collections.HashMap

private const val HEADER_LENGTH = 10
private const val ID3_LENGTH = 3
Expand All @@ -31,7 +33,11 @@ private const val FRAME_ID_TITLE = "TIT2"
private var readerPosition: Int = 0
private var currentChapter: ChapterMetaData? = null

@Synchronized fun readInputStream(input: InputStream): Map<Int, String> {
fun read(file: File): Map<Int, String> = file.inputStream().use {
readInputStream(it)
}

@Synchronized private fun readInputStream(input: InputStream): Map<Int, String> {
chapters.clear()

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package de.ph1b.audiobook.chapterreader.id3
import de.ph1b.audiobook.chapterreader.matroska.NoOpLogger
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.io.File


/**
* Test for the id3 chapter reader
*/
class ID3ChapterReaderTest {

private val id3ChapterReader = ID3ChapterReader(NoOpLogger)

@Test
fun readInputStream() {
val file = javaClass.classLoader.getResource("id3/simple.mp3")
fun testRead() {
val file = File(javaClass.classLoader.getResource("id3/simple.mp3").file)

val chapters = id3ChapterReader.readInputStream(file.openStream())
val chapters = id3ChapterReader.read(file)

assertThat(chapters).isEqualTo(mapOf(
0 to "Intro",
Expand Down

0 comments on commit a6d1c27

Please sign in to comment.