Skip to content

Commit

Permalink
Use the binary-stream extension readBytes and rename it to readAmount…
Browse files Browse the repository at this point in the history
…OfBytes to prevent the clash-bug with the kotlin extensions.
  • Loading branch information
PaulWoitaschek committed Jul 27, 2017
1 parent 69bd8f7 commit 2700275
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.ph1b.audiobook.features.chapterReader.ogg

import android.util.SparseArray
import de.ph1b.audiobook.common.readAmountOfBytes
import de.ph1b.audiobook.common.startsWith
import de.ph1b.audiobook.features.chapterReader.ogg.oggReading.OggPageParseException
import de.ph1b.audiobook.features.chapterReader.ogg.oggReading.OggStream
Expand Down Expand Up @@ -55,7 +56,7 @@ private fun readVorbisCommentFromOpusStream(stream: OggStream): VorbisComment {
throw OpusStreamParseException("Opus tags packet not present")
val tagsPacket = stream.next()
val packetStream = ByteArrayInputStream(tagsPacket)
val capturePattern = packetStream.readBytes(OPUS_TAGS_MAGIC.size)
val capturePattern = packetStream.readAmountOfBytes(OPUS_TAGS_MAGIC.size)
if (!(capturePattern contentEquals OPUS_TAGS_MAGIC))
throw OpusStreamParseException("Invalid opus tags capture pattern")
return VorbisCommentReader.readComment(packetStream)
Expand All @@ -69,7 +70,7 @@ private fun readVorbisCommentFromVorbisStream(stream: OggStream): VorbisComment
throw VorbisStreamParseException("Vorbis comment header packet not present")
val tagsPacket = stream.next()
val packetStream = ByteArrayInputStream(tagsPacket)
val capturePattern = packetStream.readBytes(VORBIS_TAGS_MAGIC.size)
val capturePattern = packetStream.readAmountOfBytes(VORBIS_TAGS_MAGIC.size)
if (!(capturePattern contentEquals VORBIS_TAGS_MAGIC))
throw VorbisStreamParseException("Invalid vorbis comment header capture pattern")
return VorbisCommentReader.readComment(packetStream)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.ph1b.audiobook.features.chapterReader.ogg.oggReading

import android.util.SparseArray
import de.ph1b.audiobook.common.readAmountOfBytes
import de.ph1b.audiobook.common.readLeInt32
import de.ph1b.audiobook.common.readLeInt64
import de.ph1b.audiobook.common.readLeUInt32
Expand All @@ -16,7 +17,7 @@ fun readOggPages(stream: InputStream): Sequence<OggPage> {
return generateSequence gen@ {
// https://www.ietf.org/rfc/rfc3533.txt
val capturePattern = try {
stream.readBytes(4)
stream.readAmountOfBytes(4)
} catch (_: EOFException) {
return@gen null
}
Expand All @@ -31,9 +32,9 @@ fun readOggPages(stream: InputStream): Sequence<OggPage> {
val pageSequenceNumber = stream.readLeUInt32()
stream.skipBytes(4) // checksum
val numberPageSegments = stream.readUInt8()
val segmentTable = stream.readBytes(numberPageSegments)
val segmentTable = stream.readAmountOfBytes(numberPageSegments)
val packets = PackageSizeParser.fromSegmentTable(segmentTable).map {
stream.readBytes(it)
stream.readAmountOfBytes(it)
}

OggPage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.ph1b.audiobook.features.chapterReader.ogg.vorbisComment

import de.ph1b.audiobook.common.readAmountOfBytes
import de.ph1b.audiobook.common.readLeUInt32
import java.io.InputStream

Expand All @@ -12,11 +13,11 @@ object VorbisCommentReader {
*/
fun readComment(stream: InputStream): VorbisComment {
val vendorLength = stream.readLeUInt32()
val vendor = stream.readBytes(vendorLength.toInt()).toString(Charsets.UTF_8)
val vendor = stream.readAmountOfBytes(vendorLength.toInt()).toString(Charsets.UTF_8)
val numberComments = stream.readLeUInt32()
val comments = (1..numberComments).map {
val length = stream.readLeUInt32()
val comment = stream.readBytes(length.toInt()).toString(Charsets.UTF_8)
val comment = stream.readAmountOfBytes(length.toInt()).toString(Charsets.UTF_8)
val parts = comment.split("=", limit = 2)
if (parts.size != 2) throw VorbisCommentParseException("Expected TAG=value comment format")
Pair(parts[0].toUpperCase(), parts[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.io.InputStream

fun InputStream.skipBytes(number: Int) = IOUtils.skipFully(this, number.toLong())

fun InputStream.readBytes(number: Int): ByteArray {
fun InputStream.readAmountOfBytes(number: Int): ByteArray {
val buf = ByteArray(number)
var index = 0
while (index < number) {
Expand All @@ -27,23 +27,23 @@ fun InputStream.readUInt8(): Int {
}

fun InputStream.readLeUInt32(): Long {
val buf = readBytes(4)
val buf = readAmountOfBytes(4)
return buf[0].toULong() or
(buf[1].toULong() shl 8) or
(buf[2].toULong() shl 16) or
(buf[3].toULong() shl 24)
}

fun InputStream.readLeInt32(): Int {
val buf = readBytes(4)
val buf = readAmountOfBytes(4)
return buf[0].toUInt() or
(buf[1].toUInt() shl 8) or
(buf[2].toUInt() shl 16) or
(buf[3].toUInt() shl 24)
}

fun InputStream.readLeInt64(): Long {
val buf = readBytes(8)
val buf = readAmountOfBytes(8)
return buf[0].toULong() or
(buf[1].toULong() shl 8) or
(buf[2].toULong() shl 16) or
Expand All @@ -60,5 +60,4 @@ fun ByteArray.startsWith(prefix: ByteArray): Boolean {
}

fun Byte.toUInt(): Int = toInt() and 0xFF

fun Byte.toULong() = toLong() and 0xFFL
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class BinaryStreamExtensionsTest {

@Test
fun readBytes() {
assertThat(stream.readBytes(4)).isEqualTo(DatatypeConverter.parseHexBinary("6c6f6cff"))
assertThat(stream.readBytes(3)).isEqualTo(DatatypeConverter.parseHexBinary("d7904d"))
assertThat(stream.readAmountOfBytes(4)).isEqualTo(DatatypeConverter.parseHexBinary("6c6f6cff"))
assertThat(stream.readAmountOfBytes(3)).isEqualTo(DatatypeConverter.parseHexBinary("d7904d"))
}

@Test
Expand All @@ -78,6 +78,6 @@ class BinaryStreamExtensionsTest {

@Test(expected = EOFException::class)
fun endOfBinaryStreamException() {
stream.readBytes(100)
stream.readAmountOfBytes(100)
}
}

0 comments on commit 2700275

Please sign in to comment.