Merge pull request #5345 from AntennaPod/example-file

Added chapter marks test file with extended header
This commit is contained in:
ByteHamster 2021-08-17 20:28:54 +02:00 committed by GitHub
commit 98cfbcafa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
/**
* Reads the ID3 Tag of a given file.
@ -96,6 +97,10 @@ public class ID3Reader {
short version = readShort();
byte flags = readByte();
int size = unsynchsafe(readInt());
if ((flags & 0b01000000) != 0) {
int extendedHeaderSize = readInt();
skipBytes(extendedHeaderSize - 4);
}
return new TagHeader("ID3", size, version, flags);
}
@ -180,16 +185,29 @@ public class ID3Reader {
private String readEncodedString2(Charset charset, int max) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
int bytesRead = 0;
boolean foundEnd = false;
while (bytesRead + 1 < max) {
byte c1 = readByte();
byte c2 = readByte();
if (c1 == 0 && c2 == 0) {
foundEnd = true;
break;
}
bytesRead += 2;
bytes.write(c1);
bytes.write(c2);
}
return charset.newDecoder().decode(ByteBuffer.wrap(bytes.toByteArray())).toString();
if (!foundEnd && bytesRead < max) {
// Last character
byte c = readByte();
if (c != 0) {
bytes.write(c);
}
}
try {
return charset.newDecoder().decode(ByteBuffer.wrap(bytes.toByteArray())).toString();
} catch (MalformedInputException e) {
return "";
}
}
}

View File

@ -185,4 +185,25 @@ public class ChapterReaderTest {
assertEquals(EmbeddedChapterImage.makeUrl(5330, 4015), chapters.get(0).getImageUrl());
assertEquals(EmbeddedChapterImage.makeUrl(9498, 4364), chapters.get(1).getImageUrl());
}
@Test
public void testRealFileMp3chapsPy() throws IOException, ID3ReaderException {
CountingInputStream inputStream = new CountingInputStream(getClass().getClassLoader()
.getResource("media-parser/mp3chaps-py.mp3").openStream());
ChapterReader reader = new ChapterReader(inputStream);
reader.readInputStream();
List<Chapter> chapters = reader.getChapters();
assertEquals(4, chapters.size());
assertEquals(0, chapters.get(0).getStart());
assertEquals(7000, chapters.get(1).getStart());
assertEquals(9000, chapters.get(2).getStart());
assertEquals(11000, chapters.get(3).getStart());
assertEquals("Start", chapters.get(0).getTitle());
assertEquals("Chapter 1", chapters.get(1).getTitle());
assertEquals("Chapter 2", chapters.get(2).getTitle());
assertEquals("Chapter 3", chapters.get(3).getTitle());
}
}

Binary file not shown.