Avoid too much nesting

This commit is contained in:
ByteHamster 2019-12-02 10:30:49 +01:00
parent cbcd97f6c9
commit 26e91c0b81
4 changed files with 175 additions and 164 deletions

View File

@ -244,5 +244,14 @@
<property name="exceptionVariableName" value="expected"/> <property name="exceptionVariableName" value="expected"/>
</module> </module>
<module name="CommentsIndentation"/> <module name="CommentsIndentation"/>
<module name="NestedIfDepth">
<property name="max" value="4"/>
</module>
<module name="NestedTryDepth">
<property name="max" value="2"/>
</module>
<module name="NestedForDepth">
<property name="max" value="2"/>
</module>
</module> </module>
</module> </module>

View File

@ -91,5 +91,14 @@
<property name="tokens" value="VARIABLE_DEF"/> <property name="tokens" value="VARIABLE_DEF"/>
<property name="allowSamelineMultipleAnnotations" value="true"/> <property name="allowSamelineMultipleAnnotations" value="true"/>
</module> </module>
<module name="NestedIfDepth">
<property name="max" value="4"/>
</module>
<module name="NestedTryDepth">
<property name="max" value="2"/>
</module>
<module name="NestedForDepth">
<property name="max" value="2"/>
</module>
</module> </module>
</module> </module>

View File

@ -97,11 +97,9 @@ public class NSAtom extends Namespace {
type = SyndTypeUtils.getMimeTypeFromUrl(href); type = SyndTypeUtils.getMimeTypeFromUrl(href);
} }
if(SyndTypeUtils.enclosureTypeValid(type)) { FeedItem currItem = state.getCurrentItem();
FeedItem currItem = state.getCurrentItem(); if (SyndTypeUtils.enclosureTypeValid(type) && currItem != null && !currItem.hasMedia()) {
if(currItem != null && !currItem.hasMedia()) { currItem.setMedia(new FeedMedia(currItem, href, size, type));
currItem.setMedia(new FeedMedia(currItem, href, size, type));
}
} }
} else if (LINK_REL_PAYMENT.equals(rel)) { } else if (LINK_REL_PAYMENT.equals(rel)) {
state.getCurrentItem().setPaymentLink(href); state.getCurrentItem().setPaymentLink(href);
@ -111,9 +109,9 @@ public class NSAtom extends Namespace {
String type = attributes.getValue(LINK_TYPE); String type = attributes.getValue(LINK_TYPE);
/* /*
* Use as link if a) no type-attribute is given and * Use as link if a) no type-attribute is given and
* feed-object has no link yet b) type of link is * feed-object has no link yet b) type of link is
* LINK_TYPE_HTML or LINK_TYPE_XHTML * LINK_TYPE_HTML or LINK_TYPE_XHTML
*/ */
if (state.getFeed() != null && if (state.getFeed() != null &&
((type == null && state.getFeed().getLink() == null) || ((type == null && state.getFeed().getLink() == null) ||
(LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) { (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) {

View File

@ -15,147 +15,142 @@ import de.danoeh.antennapod.core.util.id3reader.model.TagHeader;
* create a subclass of it and overwrite the onStart* - or onEnd* - methods. * create a subclass of it and overwrite the onStart* - or onEnd* - methods.
*/ */
public class ID3Reader { public class ID3Reader {
private static final int HEADER_LENGTH = 10; private static final int HEADER_LENGTH = 10;
private static final int ID3_LENGTH = 3; private static final int ID3_LENGTH = 3;
private static final int FRAME_ID_LENGTH = 4; private static final int FRAME_ID_LENGTH = 4;
private static final int ACTION_SKIP = 1; private static final int ACTION_SKIP = 1;
static final int ACTION_DONT_SKIP = 2; static final int ACTION_DONT_SKIP = 2;
private int readerPosition; private int readerPosition;
private static final byte ENCODING_UTF16_WITH_BOM = 1; private static final byte ENCODING_UTF16_WITH_BOM = 1;
private static final byte ENCODING_UTF16_WITHOUT_BOM = 2; private static final byte ENCODING_UTF16_WITHOUT_BOM = 2;
private static final byte ENCODING_UTF8 = 3; private static final byte ENCODING_UTF8 = 3;
private TagHeader tagHeader; private TagHeader tagHeader;
ID3Reader() { ID3Reader() {
} }
public final void readInputStream(InputStream input) throws IOException, public final void readInputStream(InputStream input) throws IOException,
ID3ReaderException { ID3ReaderException {
int rc; int rc;
readerPosition = 0; readerPosition = 0;
char[] tagHeaderSource = readBytes(input, HEADER_LENGTH); char[] tagHeaderSource = readBytes(input, HEADER_LENGTH);
tagHeader = createTagHeader(tagHeaderSource); tagHeader = createTagHeader(tagHeaderSource);
if (tagHeader == null) { if (tagHeader == null) {
onNoTagHeaderFound(); onNoTagHeaderFound();
} else { } else {
rc = onStartTagHeader(tagHeader); rc = onStartTagHeader(tagHeader);
if (rc == ACTION_SKIP) { if (rc == ACTION_SKIP) {
onEndTag(); onEndTag();
} else { } else {
while (readerPosition < tagHeader.getSize()) { while (readerPosition < tagHeader.getSize()) {
FrameHeader frameHeader = createFrameHeader(readBytes( FrameHeader frameHeader = createFrameHeader(readBytes(input, HEADER_LENGTH));
input, HEADER_LENGTH)); if (checkForNullString(frameHeader.getId())) {
if (checkForNullString(frameHeader.getId())) { break;
break; }
} else { rc = onStartFrameHeader(frameHeader, input);
rc = onStartFrameHeader(frameHeader, input); if (rc == ACTION_SKIP) {
if (rc == ACTION_SKIP) { if (frameHeader.getSize() + readerPosition > tagHeader.getSize()) {
break;
}
skipBytes(input, frameHeader.getSize());
}
}
onEndTag();
}
}
}
if (frameHeader.getSize() + readerPosition > tagHeader /** Returns true if string only contains null-bytes. */
.getSize()) { private boolean checkForNullString(String s) {
break; if (!s.isEmpty()) {
} else { int i = 0;
skipBytes(input, frameHeader.getSize()); if (s.charAt(i) == 0) {
} for (i = 1; i < s.length(); i++) {
} if (s.charAt(i) != 0) {
} return false;
} }
onEndTag(); }
} return true;
} }
} return false;
} else {
return true;
}
/** Returns true if string only contains null-bytes. */ }
private boolean checkForNullString(String s) {
if (!s.isEmpty()) {
int i = 0;
if (s.charAt(i) == 0) {
for (i = 1; i < s.length(); i++) {
if (s.charAt(i) != 0) {
return false;
}
}
return true;
}
return false;
} else {
return true;
}
} /**
* Read a certain number of bytes from the given input stream. This method
/** * changes the readerPosition-attribute.
* Read a certain number of bytes from the given input stream. This method */
* changes the readerPosition-attribute.
*/
char[] readBytes(InputStream input, int number) char[] readBytes(InputStream input, int number)
throws IOException, ID3ReaderException { throws IOException, ID3ReaderException {
char[] header = new char[number]; char[] header = new char[number];
for (int i = 0; i < number; i++) { for (int i = 0; i < number; i++) {
int b = input.read(); int b = input.read();
readerPosition++; readerPosition++;
if (b != -1) { if (b != -1) {
header[i] = (char) b; header[i] = (char) b;
} else { } else {
throw new ID3ReaderException("Unexpected end of stream"); throw new ID3ReaderException("Unexpected end of stream");
} }
} }
return header; return header;
} }
/** /**
* Skip a certain number of bytes on the given input stream. This method * Skip a certain number of bytes on the given input stream. This method
* changes the readerPosition-attribute. * changes the readerPosition-attribute.
*/ */
void skipBytes(InputStream input, int number) throws IOException { void skipBytes(InputStream input, int number) throws IOException {
if (number <= 0) { if (number <= 0) {
number = 1; number = 1;
} }
IOUtils.skipFully(input, number); IOUtils.skipFully(input, number);
readerPosition += number; readerPosition += number;
} }
private TagHeader createTagHeader(char[] source) throws ID3ReaderException { private TagHeader createTagHeader(char[] source) throws ID3ReaderException {
boolean hasTag = (source[0] == 0x49) && (source[1] == 0x44) boolean hasTag = (source[0] == 0x49) && (source[1] == 0x44)
&& (source[2] == 0x33); && (source[2] == 0x33);
if (source.length != HEADER_LENGTH) { if (source.length != HEADER_LENGTH) {
throw new ID3ReaderException("Length of header must be " throw new ID3ReaderException("Length of header must be "
+ HEADER_LENGTH); + HEADER_LENGTH);
} }
if (hasTag) { if (hasTag) {
String id = new String(source, 0, ID3_LENGTH); String id = new String(source, 0, ID3_LENGTH);
char version = (char) ((source[3] << 8) | source[4]); char version = (char) ((source[3] << 8) | source[4]);
byte flags = (byte) source[5]; byte flags = (byte) source[5];
int size = (source[6] << 24) | (source[7] << 16) | (source[8] << 8) int size = (source[6] << 24) | (source[7] << 16) | (source[8] << 8)
| source[9]; | source[9];
size = unsynchsafe(size); size = unsynchsafe(size);
return new TagHeader(id, size, version, flags); return new TagHeader(id, size, version, flags);
} else { } else {
return null; return null;
} }
} }
private FrameHeader createFrameHeader(char[] source) private FrameHeader createFrameHeader(char[] source)
throws ID3ReaderException { throws ID3ReaderException {
if (source.length != HEADER_LENGTH) { if (source.length != HEADER_LENGTH) {
throw new ID3ReaderException("Length of header must be " throw new ID3ReaderException("Length of header must be "
+ HEADER_LENGTH); + HEADER_LENGTH);
} }
String id = new String(source, 0, FRAME_ID_LENGTH); String id = new String(source, 0, FRAME_ID_LENGTH);
int size = (((int) source[4]) << 24) | (((int) source[5]) << 16) int size = (((int) source[4]) << 24) | (((int) source[5]) << 16)
| (((int) source[6]) << 8) | source[7]; | (((int) source[6]) << 8) | source[7];
if (tagHeader != null && tagHeader.getVersion() >= 0x0400) { if (tagHeader != null && tagHeader.getVersion() >= 0x0400) {
size = unsynchsafe(size); size = unsynchsafe(size);
} }
char flags = (char) ((source[8] << 8) | source[9]); char flags = (char) ((source[8] << 8) | source[9]);
return new FrameHeader(id, size, flags); return new FrameHeader(id, size, flags);
} }
private int unsynchsafe(int in) { private int unsynchsafe(int in) {
int out = 0; int out = 0;
@ -170,42 +165,42 @@ public class ID3Reader {
return out; return out;
} }
protected int readString(StringBuilder buffer, InputStream input, int max) throws IOException, protected int readString(StringBuilder buffer, InputStream input, int max) throws IOException,
ID3ReaderException { ID3ReaderException {
if (max > 0) { if (max > 0) {
char[] encoding = readBytes(input, 1); char[] encoding = readBytes(input, 1);
max--; max--;
if (encoding[0] == ENCODING_UTF16_WITH_BOM || encoding[0] == ENCODING_UTF16_WITHOUT_BOM) { if (encoding[0] == ENCODING_UTF16_WITH_BOM || encoding[0] == ENCODING_UTF16_WITHOUT_BOM) {
return readUnicodeString(buffer, input, max, Charset.forName("UTF-16")) + 1; // take encoding byte into account return readUnicodeString(buffer, input, max, Charset.forName("UTF-16")) + 1; // take encoding byte into account
} else if (encoding[0] == ENCODING_UTF8) { } else if (encoding[0] == ENCODING_UTF8) {
return readUnicodeString(buffer, input, max, Charset.forName("UTF-8")) + 1; // take encoding byte into account return readUnicodeString(buffer, input, max, Charset.forName("UTF-8")) + 1; // take encoding byte into account
} else { } else {
return readISOString(buffer, input, max) + 1; // take encoding byte into account return readISOString(buffer, input, max) + 1; // take encoding byte into account
} }
} else { } else {
if (buffer != null) { if (buffer != null) {
buffer.append(""); buffer.append("");
} }
return 0; return 0;
} }
} }
protected int readISOString(StringBuilder buffer, InputStream input, int max) protected int readISOString(StringBuilder buffer, InputStream input, int max)
throws IOException, ID3ReaderException { throws IOException, ID3ReaderException {
int bytesRead = 0; int bytesRead = 0;
char c; char c;
while (++bytesRead <= max && (c = (char) input.read()) > 0) { while (++bytesRead <= max && (c = (char) input.read()) > 0) {
if (buffer != null) { if (buffer != null) {
buffer.append(c); buffer.append(c);
} }
} }
return bytesRead; return bytesRead;
} }
private int readUnicodeString(StringBuilder strBuffer, InputStream input, int max, Charset charset) private int readUnicodeString(StringBuilder strBuffer, InputStream input, int max, Charset charset)
throws IOException, ID3ReaderException { throws IOException, ID3ReaderException {
byte[] buffer = new byte[max]; byte[] buffer = new byte[max];
int c; int c;
int cZero = -1; int cZero = -1;
int i = 0; int i = 0;
@ -226,26 +221,26 @@ public class ID3Reader {
} }
} }
if (strBuffer != null) { if (strBuffer != null) {
strBuffer.append(charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString()); strBuffer.append(charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString());
} }
return i; return i;
} }
int onStartTagHeader(TagHeader header) { int onStartTagHeader(TagHeader header) {
return ACTION_SKIP; return ACTION_SKIP;
} }
int onStartFrameHeader(FrameHeader header, InputStream input) int onStartFrameHeader(FrameHeader header, InputStream input)
throws IOException, ID3ReaderException { throws IOException, ID3ReaderException {
return ACTION_SKIP; return ACTION_SKIP;
} }
void onEndTag() { void onEndTag() {
} }
void onNoTagHeaderFound() { void onNoTagHeaderFound() {
} }
} }