From 26e91c0b81f82da5eae7565d17135de07637cc6e Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 2 Dec 2019 10:30:49 +0100 Subject: [PATCH 1/2] Avoid too much nesting --- .../checkstyle/checkstyle-best-practice.xml | 9 + config/checkstyle/checkstyle.xml | 9 + .../syndication/namespace/atom/NSAtom.java | 14 +- .../core/util/id3reader/ID3Reader.java | 307 +++++++++--------- 4 files changed, 175 insertions(+), 164 deletions(-) diff --git a/config/checkstyle/checkstyle-best-practice.xml b/config/checkstyle/checkstyle-best-practice.xml index 3abe4b5e5..410251d6c 100644 --- a/config/checkstyle/checkstyle-best-practice.xml +++ b/config/checkstyle/checkstyle-best-practice.xml @@ -244,5 +244,14 @@ + + + + + + + + + diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 4f916ec3f..02a370b7e 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -91,5 +91,14 @@ + + + + + + + + + diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java index 83957456a..7aa5a90e7 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java +++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/atom/NSAtom.java @@ -97,11 +97,9 @@ public class NSAtom extends Namespace { type = SyndTypeUtils.getMimeTypeFromUrl(href); } - if(SyndTypeUtils.enclosureTypeValid(type)) { - FeedItem currItem = state.getCurrentItem(); - if(currItem != null && !currItem.hasMedia()) { - currItem.setMedia(new FeedMedia(currItem, href, size, type)); - } + FeedItem currItem = state.getCurrentItem(); + if (SyndTypeUtils.enclosureTypeValid(type) && currItem != null && !currItem.hasMedia()) { + currItem.setMedia(new FeedMedia(currItem, href, size, type)); } } else if (LINK_REL_PAYMENT.equals(rel)) { state.getCurrentItem().setPaymentLink(href); @@ -111,9 +109,9 @@ public class NSAtom extends Namespace { String type = attributes.getValue(LINK_TYPE); /* * Use as link if a) no type-attribute is given and - * feed-object has no link yet b) type of link is - * LINK_TYPE_HTML or LINK_TYPE_XHTML - */ + * feed-object has no link yet b) type of link is + * LINK_TYPE_HTML or LINK_TYPE_XHTML + */ if (state.getFeed() != null && ((type == null && state.getFeed().getLink() == null) || (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ID3Reader.java b/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ID3Reader.java index 8589e1283..3f5993700 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ID3Reader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/id3reader/ID3Reader.java @@ -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. */ public class ID3Reader { - private static final int HEADER_LENGTH = 10; - private static final int ID3_LENGTH = 3; - private static final int FRAME_ID_LENGTH = 4; + private static final int HEADER_LENGTH = 10; + private static final int ID3_LENGTH = 3; + private static final int FRAME_ID_LENGTH = 4; - private static final int ACTION_SKIP = 1; - static final int ACTION_DONT_SKIP = 2; + private static final int ACTION_SKIP = 1; + 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_UTF8 = 3; private TagHeader tagHeader; - ID3Reader() { - } + ID3Reader() { + } - public final void readInputStream(InputStream input) throws IOException, - ID3ReaderException { - int rc; - readerPosition = 0; - char[] tagHeaderSource = readBytes(input, HEADER_LENGTH); - tagHeader = createTagHeader(tagHeaderSource); - if (tagHeader == null) { - onNoTagHeaderFound(); - } else { - rc = onStartTagHeader(tagHeader); - if (rc == ACTION_SKIP) { - onEndTag(); - } else { - while (readerPosition < tagHeader.getSize()) { - FrameHeader frameHeader = createFrameHeader(readBytes( - input, HEADER_LENGTH)); - if (checkForNullString(frameHeader.getId())) { - break; - } else { - rc = onStartFrameHeader(frameHeader, input); - if (rc == ACTION_SKIP) { + public final void readInputStream(InputStream input) throws IOException, + ID3ReaderException { + int rc; + readerPosition = 0; + char[] tagHeaderSource = readBytes(input, HEADER_LENGTH); + tagHeader = createTagHeader(tagHeaderSource); + if (tagHeader == null) { + onNoTagHeaderFound(); + } else { + rc = onStartTagHeader(tagHeader); + if (rc == ACTION_SKIP) { + onEndTag(); + } else { + while (readerPosition < tagHeader.getSize()) { + FrameHeader frameHeader = createFrameHeader(readBytes(input, HEADER_LENGTH)); + if (checkForNullString(frameHeader.getId())) { + break; + } + rc = onStartFrameHeader(frameHeader, input); + if (rc == ACTION_SKIP) { + if (frameHeader.getSize() + readerPosition > tagHeader.getSize()) { + break; + } + skipBytes(input, frameHeader.getSize()); + } + } + onEndTag(); + } + } + } - if (frameHeader.getSize() + readerPosition > tagHeader - .getSize()) { - break; - } else { - skipBytes(input, frameHeader.getSize()); - } - } - } - } - onEndTag(); - } - } - } + /** 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; + } - /** 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) - throws IOException, ID3ReaderException { - char[] header = new char[number]; - for (int i = 0; i < number; i++) { - int b = input.read(); - readerPosition++; - if (b != -1) { - header[i] = (char) b; - } else { - throw new ID3ReaderException("Unexpected end of stream"); - } - } - return header; - } + throws IOException, ID3ReaderException { + char[] header = new char[number]; + for (int i = 0; i < number; i++) { + int b = input.read(); + readerPosition++; + if (b != -1) { + header[i] = (char) b; + } else { + throw new ID3ReaderException("Unexpected end of stream"); + } + } + return header; + } - /** - * Skip a certain number of bytes on the given input stream. This method - * changes the readerPosition-attribute. - */ + /** + * Skip a certain number of bytes on the given input stream. This method + * changes the readerPosition-attribute. + */ void skipBytes(InputStream input, int number) throws IOException { - if (number <= 0) { - number = 1; - } - IOUtils.skipFully(input, number); + if (number <= 0) { + number = 1; + } + IOUtils.skipFully(input, number); - readerPosition += number; - } + readerPosition += number; + } - private TagHeader createTagHeader(char[] source) throws ID3ReaderException { - boolean hasTag = (source[0] == 0x49) && (source[1] == 0x44) - && (source[2] == 0x33); - if (source.length != HEADER_LENGTH) { - throw new ID3ReaderException("Length of header must be " - + HEADER_LENGTH); - } - if (hasTag) { - String id = new String(source, 0, ID3_LENGTH); - char version = (char) ((source[3] << 8) | source[4]); - byte flags = (byte) source[5]; - int size = (source[6] << 24) | (source[7] << 16) | (source[8] << 8) - | source[9]; + private TagHeader createTagHeader(char[] source) throws ID3ReaderException { + boolean hasTag = (source[0] == 0x49) && (source[1] == 0x44) + && (source[2] == 0x33); + if (source.length != HEADER_LENGTH) { + throw new ID3ReaderException("Length of header must be " + + HEADER_LENGTH); + } + if (hasTag) { + String id = new String(source, 0, ID3_LENGTH); + char version = (char) ((source[3] << 8) | source[4]); + byte flags = (byte) source[5]; + int size = (source[6] << 24) | (source[7] << 16) | (source[8] << 8) + | source[9]; size = unsynchsafe(size); - return new TagHeader(id, size, version, flags); - } else { - return null; - } - } + return new TagHeader(id, size, version, flags); + } else { + return null; + } + } - private FrameHeader createFrameHeader(char[] source) - throws ID3ReaderException { - if (source.length != HEADER_LENGTH) { - throw new ID3ReaderException("Length of header must be " - + HEADER_LENGTH); - } - String id = new String(source, 0, FRAME_ID_LENGTH); + private FrameHeader createFrameHeader(char[] source) + throws ID3ReaderException { + if (source.length != HEADER_LENGTH) { + throw new ID3ReaderException("Length of header must be " + + HEADER_LENGTH); + } + String id = new String(source, 0, FRAME_ID_LENGTH); 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) { size = unsynchsafe(size); } - char flags = (char) ((source[8] << 8) | source[9]); - return new FrameHeader(id, size, flags); - } + char flags = (char) ((source[8] << 8) | source[9]); + return new FrameHeader(id, size, flags); + } private int unsynchsafe(int in) { int out = 0; @@ -170,42 +165,42 @@ public class ID3Reader { return out; } - protected int readString(StringBuilder buffer, InputStream input, int max) throws IOException, - ID3ReaderException { - if (max > 0) { - char[] encoding = readBytes(input, 1); - max--; - - if (encoding[0] == ENCODING_UTF16_WITH_BOM || encoding[0] == ENCODING_UTF16_WITHOUT_BOM) { + protected int readString(StringBuilder buffer, InputStream input, int max) throws IOException, + ID3ReaderException { + if (max > 0) { + char[] encoding = readBytes(input, 1); + max--; + + 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 - } 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 } else { - return readISOString(buffer, input, max) + 1; // take encoding byte into account - } - } else { + return readISOString(buffer, input, max) + 1; // take encoding byte into account + } + } else { if (buffer != null) { buffer.append(""); } - return 0; - } - } + return 0; + } + } - protected int readISOString(StringBuilder buffer, InputStream input, int max) - throws IOException, ID3ReaderException { - int bytesRead = 0; - char c; - while (++bytesRead <= max && (c = (char) input.read()) > 0) { + protected int readISOString(StringBuilder buffer, InputStream input, int max) + throws IOException, ID3ReaderException { + int bytesRead = 0; + char c; + while (++bytesRead <= max && (c = (char) input.read()) > 0) { if (buffer != null) { - buffer.append(c); + buffer.append(c); } - } - return bytesRead; - } + } + return bytesRead; + } - private int readUnicodeString(StringBuilder strBuffer, InputStream input, int max, Charset charset) - throws IOException, ID3ReaderException { - byte[] buffer = new byte[max]; + private int readUnicodeString(StringBuilder strBuffer, InputStream input, int max, Charset charset) + throws IOException, ID3ReaderException { + byte[] buffer = new byte[max]; int c; int cZero = -1; int i = 0; @@ -226,26 +221,26 @@ public class ID3Reader { } } if (strBuffer != null) { - strBuffer.append(charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString()); + strBuffer.append(charset.newDecoder().decode(ByteBuffer.wrap(buffer)).toString()); } return i; - } + } - int onStartTagHeader(TagHeader header) { - return ACTION_SKIP; - } + int onStartTagHeader(TagHeader header) { + return ACTION_SKIP; + } - int onStartFrameHeader(FrameHeader header, InputStream input) - throws IOException, ID3ReaderException { - return ACTION_SKIP; - } + int onStartFrameHeader(FrameHeader header, InputStream input) + throws IOException, ID3ReaderException { + return ACTION_SKIP; + } - void onEndTag() { + void onEndTag() { - } + } - void onNoTagHeaderFound() { + void onNoTagHeaderFound() { - } + } } From 5789704c8fba9793c36973d507d5f1af96f17d59 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 2 Dec 2019 10:37:19 +0100 Subject: [PATCH 2/2] Removed intendation monster --- .../util/service/download/HTTPBin.java | 64 +++++++++++-------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java b/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java index 4158fd31c..49befd775 100644 --- a/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java +++ b/app/src/androidTest/java/de/test/antennapod/util/service/download/HTTPBin.java @@ -98,7 +98,7 @@ public class HTTPBin extends NanoHTTPD { if (func.equalsIgnoreCase("status")) { try { int code = Integer.parseInt(param); - return getStatus(code); + return new Response(getStatus(code), MIME_HTML, ""); } catch (NumberFormatException e) { e.printStackTrace(); return getInternalError(); @@ -284,31 +284,45 @@ public class HTTPBin extends NanoHTTPD { return response; } - private Response getStatus(final int code) { - Response.IStatus status = (code == 200) ? Response.Status.OK : - (code == 201) ? Response.Status.CREATED : - (code == 206) ? Response.Status.PARTIAL_CONTENT : - (code == 301) ? Response.Status.REDIRECT : - (code == 304) ? Response.Status.NOT_MODIFIED : - (code == 400) ? Response.Status.BAD_REQUEST : - (code == 401) ? Response.Status.UNAUTHORIZED : - (code == 403) ? Response.Status.FORBIDDEN : - (code == 404) ? Response.Status.NOT_FOUND : - (code == 405) ? Response.Status.METHOD_NOT_ALLOWED : - (code == 416) ? Response.Status.RANGE_NOT_SATISFIABLE : - (code == 500) ? Response.Status.INTERNAL_ERROR : new Response.IStatus() { - @Override - public int getRequestStatus() { - return code; - } - - @Override - public String getDescription() { - return "Unknown"; - } - }; - return new Response(status, MIME_HTML, ""); + private Response.IStatus getStatus(final int code) { + switch (code) { + case 200: + return Response.Status.OK; + case 201: + return Response.Status.CREATED; + case 206: + return Response.Status.PARTIAL_CONTENT; + case 301: + return Response.Status.REDIRECT; + case 304: + return Response.Status.NOT_MODIFIED; + case 400: + return Response.Status.BAD_REQUEST; + case 401: + return Response.Status.UNAUTHORIZED; + case 403: + return Response.Status.FORBIDDEN; + case 404: + return Response.Status.NOT_FOUND; + case 405: + return Response.Status.METHOD_NOT_ALLOWED; + case 416: + return Response.Status.RANGE_NOT_SATISFIABLE; + case 500: + return Response.Status.INTERNAL_ERROR; + default: + return new Response.IStatus() { + @Override + public int getRequestStatus() { + return code; + } + @Override + public String getDescription() { + return "Unknown"; + } + }; + } } private Response getRedirectResponse(int times) {