Speed up chapter parsing

This commit is contained in:
ByteHamster 2022-10-19 20:29:29 +02:00
parent 25dd4902ba
commit 9624d8ce9e
4 changed files with 14 additions and 23 deletions

View File

@ -9,6 +9,7 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.ParseException; import java.text.ParseException;
@ -218,7 +219,8 @@ public class LocalFeedUpdater {
mediaMetadataRetriever.close(); mediaMetadataRetriever.close();
try (InputStream inputStream = context.getContentResolver().openInputStream(file.getUri())) { try (InputStream inputStream = context.getContentResolver().openInputStream(file.getUri())) {
Id3MetadataReader reader = new Id3MetadataReader(new CountingInputStream(inputStream)); Id3MetadataReader reader = new Id3MetadataReader(
new CountingInputStream(new BufferedInputStream(inputStream)));
reader.readInputStream(); reader.readInputStream();
item.setDescriptionIfLonger(reader.getComment()); item.setDescriptionIfLonger(reader.getComment());
} catch (IOException | ID3ReaderException e) { } catch (IOException | ID3ReaderException e) {

View File

@ -23,6 +23,7 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import org.apache.commons.io.input.CountingInputStream; import org.apache.commons.io.input.CountingInputStream;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@ -120,17 +121,17 @@ public class ChapterUtils {
if (!source.exists()) { if (!source.exists()) {
throw new IOException("Local file does not exist"); throw new IOException("Local file does not exist");
} }
return new CountingInputStream(new FileInputStream(source)); return new CountingInputStream(new BufferedInputStream(new FileInputStream(source)));
} else if (playable.getStreamUrl().startsWith(ContentResolver.SCHEME_CONTENT)) { } else if (playable.getStreamUrl().startsWith(ContentResolver.SCHEME_CONTENT)) {
Uri uri = Uri.parse(playable.getStreamUrl()); Uri uri = Uri.parse(playable.getStreamUrl());
return new CountingInputStream(context.getContentResolver().openInputStream(uri)); return new CountingInputStream(new BufferedInputStream(context.getContentResolver().openInputStream(uri)));
} else { } else {
Request request = new Request.Builder().url(playable.getStreamUrl()).build(); Request request = new Request.Builder().url(playable.getStreamUrl()).build();
Response response = AntennapodHttpClient.getHttpClient().newCall(request).execute(); Response response = AntennapodHttpClient.getHttpClient().newCall(request).execute();
if (response.body() == null) { if (response.body() == null) {
throw new IOException("Body is null"); throw new IOException("Body is null");
} }
return new CountingInputStream(response.body().byteStream()); return new CountingInputStream(new BufferedInputStream(response.body().byteStream()));
} }
} }
@ -171,7 +172,7 @@ public class ChapterUtils {
@NonNull @NonNull
private static List<Chapter> readOggChaptersFromInputStream(InputStream input) throws VorbisCommentReaderException { private static List<Chapter> readOggChaptersFromInputStream(InputStream input) throws VorbisCommentReaderException {
VorbisCommentChapterReader reader = new VorbisCommentChapterReader(input); VorbisCommentChapterReader reader = new VorbisCommentChapterReader(new BufferedInputStream(input));
reader.readInputStream(); reader.readInputStream();
List<Chapter> chapters = reader.getChapters(); List<Chapter> chapters = reader.getChapters();
if (chapters == null) { if (chapters == null) {

View File

@ -46,7 +46,7 @@ public class ID3Reader {
} }
protected void readFrame(@NonNull FrameHeader frameHeader) throws IOException, ID3ReaderException { protected void readFrame(@NonNull FrameHeader frameHeader) throws IOException, ID3ReaderException {
Log.d(TAG, "Skipping frame: " + frameHeader.toString()); Log.d(TAG, "Skipping frame: " + frameHeader.getId() + ", size: " + frameHeader.getSize());
skipBytes(frameHeader.getSize()); skipBytes(frameHeader.getSize());
} }
@ -106,7 +106,7 @@ public class ID3Reader {
@NonNull @NonNull
FrameHeader readFrameHeader() throws IOException { FrameHeader readFrameHeader() throws IOException {
String id = readIsoStringFixed(FRAME_ID_LENGTH); String id = readPlainBytesToString(FRAME_ID_LENGTH);
int size = readInt(); int size = readInt();
if (tagHeader != null && tagHeader.getVersion() >= 0x0400) { if (tagHeader != null && tagHeader.getVersion() >= 0x0400) {
size = unsynchsafe(size); size = unsynchsafe(size);
@ -136,15 +136,14 @@ public class ID3Reader {
return readEncodedString(encoding, max - 1); return readEncodedString(encoding, max - 1);
} }
@SuppressWarnings("CharsetObjectCanBeUsed") protected String readPlainBytesToString(int length) throws IOException {
protected String readIsoStringFixed(int length) throws IOException { StringBuilder stringBuilder = new StringBuilder();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
int bytesRead = 0; int bytesRead = 0;
while (bytesRead < length) { while (bytesRead < length) {
bytes.write(readByte()); stringBuilder.append((char) readByte());
bytesRead++; bytesRead++;
} }
return Charset.forName("ISO-8859-1").newDecoder().decode(ByteBuffer.wrap(bytes.toByteArray())).toString(); return stringBuilder.toString();
} }
protected String readIsoStringNullTerminated(int max) throws IOException { protected String readIsoStringNullTerminated(int max) throws IOException {

View File

@ -1,18 +1,7 @@
package de.danoeh.antennapod.parser.media.id3.model; package de.danoeh.antennapod.parser.media.id3.model;
import androidx.annotation.NonNull;
public class FrameHeader extends Header { public class FrameHeader extends Header {
private final short flags;
public FrameHeader(String id, int size, short flags) { public FrameHeader(String id, int size, short flags) {
super(id, size); super(id, size);
this.flags = flags;
}
@Override
@NonNull
public String toString() {
return String.format("FrameHeader [flags=%s, id=%s, size=%s]", Integer.toBinaryString(flags), id, size);
} }
} }