Reformatted vorbis reader for readability

This commit is contained in:
ByteHamster 2020-02-12 23:26:30 +01:00
parent 9db5450243
commit 246a2e650c
2 changed files with 221 additions and 242 deletions

View File

@ -38,10 +38,10 @@ public class VorbisCommentChapterReader extends VorbisCommentReader {
}
@Override
public void onContentVectorValue(String key, String value)
throws VorbisCommentReaderException {
if (BuildConfig.DEBUG)
public void onContentVectorValue(String key, String value) throws VorbisCommentReaderException {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Key: " + key + ", value: " + value);
}
String attribute = VorbisCommentChapter.getAttributeTypeFromKey(key);
int id = VorbisCommentChapter.getIDFromKey(key);
Chapter chapter = getChapterById(id);
@ -53,9 +53,7 @@ public class VorbisCommentChapterReader extends VorbisCommentReader {
chapter.setStart(start);
chapters.add(chapter);
} else {
throw new VorbisCommentReaderException(
"Found chapter with duplicate ID (" + key + ", "
+ value + ")");
throw new VorbisCommentReaderException("Found chapter with duplicate ID (" + key + ", " + value + ")");
}
} else if (attribute.equals(CHAPTER_ATTRIBUTE_TITLE)) {
if (chapter != null) {

View File

@ -1,5 +1,6 @@
package de.danoeh.antennapod.core.util.vorbiscommentreader;
import androidx.annotation.NonNull;
import org.apache.commons.io.EndianUtils;
import org.apache.commons.io.IOUtils;
@ -10,7 +11,6 @@ import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
public abstract class VorbisCommentReader {
/** Length of first page in an ogg file in bytes. */
private static final int FIRST_PAGE_LENGTH = 58;
@ -31,11 +31,8 @@ public abstract class VorbisCommentReader {
/**
* Is called if onContentVectorKey returned true for the key.
*
* @throws VorbisCommentReaderException
*/
protected abstract void onContentVectorValue(String key, String value)
throws VorbisCommentReaderException;
protected abstract void onContentVectorValue(String key, String value) throws VorbisCommentReaderException;
protected abstract void onNoVorbisCommentFound();
@ -43,45 +40,21 @@ public abstract class VorbisCommentReader {
protected abstract void onError(VorbisCommentReaderException exception);
public void readInputStream(InputStream input)
throws VorbisCommentReaderException {
public void readInputStream(InputStream input) throws VorbisCommentReaderException {
try {
// look for identification header
if (findIdentificationHeader(input)) {
onVorbisCommentFound();
input = new OggInputStream(input);
if (findCommentHeader(input)) {
VorbisCommentHeader commentHeader = readCommentHeader(input);
if (commentHeader != null) {
onVorbisCommentHeaderFound(commentHeader);
for (int i = 0; i < commentHeader
.getUserCommentLength(); i++) {
try {
long vectorLength = EndianUtils
.readSwappedUnsignedInteger(input);
String key = readContentVectorKey(input,
vectorLength).toLowerCase();
boolean readValue = onContentVectorKey(key);
if (readValue) {
String value = readUTF8String(
input,
(int) (vectorLength - key.length() - 1));
onContentVectorValue(key, value);
} else {
IOUtils.skipFully(input,
vectorLength - key.length() - 1);
}
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < commentHeader.getUserCommentLength(); i++) {
readUserComment(input);
}
onEndOfComment();
}
} else {
onError(new VorbisCommentReaderException(
"No comment header found"));
onError(new VorbisCommentReaderException("No comment header found"));
}
} else {
onNoVorbisCommentFound();
@ -91,10 +64,24 @@ public abstract class VorbisCommentReader {
}
}
private String readUTF8String(InputStream input, long length)
throws IOException {
byte[] buffer = new byte[(int) length];
private void readUserComment(InputStream input) throws VorbisCommentReaderException {
try {
long vectorLength = EndianUtils.readSwappedUnsignedInteger(input);
String key = readContentVectorKey(input, vectorLength).toLowerCase();
boolean readValue = onContentVectorKey(key);
if (readValue) {
String value = readUtf8String(input, (int) (vectorLength - key.length() - 1));
onContentVectorValue(key, value);
} else {
IOUtils.skipFully(input, vectorLength - key.length() - 1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String readUtf8String(InputStream input, long length) throws IOException {
byte[] buffer = new byte[(int) length];
IOUtils.readFully(input, buffer);
Charset charset = Charset.forName("UTF-8");
return charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString();
@ -104,15 +91,11 @@ public abstract class VorbisCommentReader {
* Looks for an identification header in the first page of the file. If an
* identification header is found, it will be skipped completely and the
* method will return true, otherwise false.
*
* @throws IOException
*/
private boolean findIdentificationHeader(InputStream input)
throws IOException {
private boolean findIdentificationHeader(InputStream input) throws IOException {
byte[] buffer = new byte[FIRST_PAGE_LENGTH];
IOUtils.readFully(input, buffer);
int i;
for (i = 6; i < buffer.length; i++) {
for (int i = 6; i < buffer.length; i++) {
if (buffer[i - 5] == 'v' && buffer[i - 4] == 'o'
&& buffer[i - 3] == 'r' && buffer[i - 2] == 'b'
&& buffer[i - 1] == 'i' && buffer[i] == 's'
@ -165,21 +148,19 @@ public abstract class VorbisCommentReader {
return false;
}
private VorbisCommentHeader readCommentHeader(InputStream input)
throws IOException, VorbisCommentReaderException {
@NonNull
private VorbisCommentHeader readCommentHeader(InputStream input) throws IOException, VorbisCommentReaderException {
try {
long vendorLength = EndianUtils.readSwappedUnsignedInteger(input);
String vendorName = readUTF8String(input, vendorLength);
long userCommentLength = EndianUtils
.readSwappedUnsignedInteger(input);
String vendorName = readUtf8String(input, vendorLength);
long userCommentLength = EndianUtils.readSwappedUnsignedInteger(input);
return new VorbisCommentHeader(vendorName, userCommentLength);
} catch (UnsupportedEncodingException e) {
throw new VorbisCommentReaderException(e);
}
}
private String readContentVectorKey(InputStream input, long vectorLength)
throws IOException {
private String readContentVectorKey(InputStream input, long vectorLength) throws IOException {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < vectorLength; i++) {
char c = (char) input.read();