Unicode strings weren't read correctly
This commit is contained in:
parent
9676ffae34
commit
50c983cccd
@ -33,11 +33,11 @@ public class ChapterReader extends ID3Reader {
|
|||||||
if (currentChapter != null) {
|
if (currentChapter != null) {
|
||||||
if (!hasId3Chapter(currentChapter)) {
|
if (!hasId3Chapter(currentChapter)) {
|
||||||
chapters.add(currentChapter);
|
chapters.add(currentChapter);
|
||||||
|
System.out.println("Found chapter: " + currentChapter);
|
||||||
currentChapter = null;
|
currentChapter = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Found chapter");
|
String elementId = readISOString(input, Integer.MAX_VALUE);
|
||||||
String elementId = readString(input, Integer.MAX_VALUE);
|
|
||||||
char[] startTimeSource = readBytes(input, 4);
|
char[] startTimeSource = readBytes(input, 4);
|
||||||
long startTime = ((int) startTimeSource[0] << 24)
|
long startTime = ((int) startTimeSource[0] << 24)
|
||||||
| ((int) startTimeSource[1] << 16)
|
| ((int) startTimeSource[1] << 16)
|
||||||
@ -47,10 +47,10 @@ public class ChapterReader extends ID3Reader {
|
|||||||
return ID3Reader.ACTION_DONT_SKIP;
|
return ID3Reader.ACTION_DONT_SKIP;
|
||||||
} else if (header.getId().equals(FRAME_ID_TITLE)) {
|
} else if (header.getId().equals(FRAME_ID_TITLE)) {
|
||||||
if (currentChapter != null && currentChapter.getTitle() == null) {
|
if (currentChapter != null && currentChapter.getTitle() == null) {
|
||||||
System.out.println("Found title");
|
|
||||||
skipBytes(input, 1);
|
|
||||||
currentChapter
|
currentChapter
|
||||||
.setTitle(readString(input, header.getSize() - 1));
|
.setTitle(readString(input, header.getSize()));
|
||||||
|
System.out.println("Found title: " + currentChapter.getTitle());
|
||||||
|
|
||||||
return ID3Reader.ACTION_DONT_SKIP;
|
return ID3Reader.ACTION_DONT_SKIP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package de.danoeh.antennapod.util.id3reader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PushbackInputStream;
|
import java.io.PushbackInputStream;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
@ -23,8 +25,7 @@ public class ID3Reader {
|
|||||||
|
|
||||||
protected int readerPosition;
|
protected int readerPosition;
|
||||||
|
|
||||||
private static final char[] LITTLE_ENDIAN_BOM = { 0xff, 0xfe };
|
private static final byte ENCODING_UNICODE = 1;
|
||||||
private static final char[] BIG_ENDIAN_BOM = { 0xfe, 0xff };
|
|
||||||
|
|
||||||
public ID3Reader() {
|
public ID3Reader() {
|
||||||
}
|
}
|
||||||
@ -152,19 +153,23 @@ public class ID3Reader {
|
|||||||
|
|
||||||
protected String readString(InputStream input, int max) throws IOException,
|
protected String readString(InputStream input, int max) throws IOException,
|
||||||
ID3ReaderException {
|
ID3ReaderException {
|
||||||
char[] bom = readBytes(input, 2);
|
if (max > 0) {
|
||||||
if (bom == LITTLE_ENDIAN_BOM || bom == BIG_ENDIAN_BOM) {
|
char[] encoding = readBytes(input, 1);
|
||||||
return readUnicodeString(input, bom, max);
|
max--;
|
||||||
|
|
||||||
|
if (encoding[0] == ENCODING_UNICODE) {
|
||||||
|
return readUnicodeString(input, max);
|
||||||
|
} else {
|
||||||
|
return readISOString(input, max);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
PushbackInputStream pi = new PushbackInputStream(input, 2);
|
return "";
|
||||||
pi.unread(bom[1]);
|
|
||||||
pi.unread(bom[0]);
|
|
||||||
return readISOString(pi, max);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readISOString(InputStream input, int max)
|
protected String readISOString(InputStream input, int max)
|
||||||
throws IOException, ID3ReaderException {
|
throws IOException, ID3ReaderException {
|
||||||
|
|
||||||
int bytesRead = 0;
|
int bytesRead = 0;
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
char c;
|
char c;
|
||||||
@ -174,22 +179,12 @@ public class ID3Reader {
|
|||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readUnicodeString(InputStream input, char[] bom, int max)
|
private String readUnicodeString(InputStream input, int max)
|
||||||
throws IOException, ID3ReaderException {
|
throws IOException, ID3ReaderException {
|
||||||
StringBuffer builder = new StringBuffer();
|
byte[] buffer = new byte[max];
|
||||||
char c1 = (char) input.read();
|
IOUtils.readFully(input, buffer);
|
||||||
char c2 = (char) input.read();
|
Charset charset = Charset.forName("UTF-16");
|
||||||
int bytesRead = 2;
|
return charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString();
|
||||||
while ((c1 > 0 && c2 > 0) && ++bytesRead <= max) {
|
|
||||||
|
|
||||||
builder.append(c1);
|
|
||||||
c1 = c2;
|
|
||||||
c2 = (char) input.read();
|
|
||||||
}
|
|
||||||
if (bom == LITTLE_ENDIAN_BOM) {
|
|
||||||
builder.reverse();
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int onStartTagHeader(TagHeader header) {
|
public int onStartTagHeader(TagHeader header) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user