I wrote Java code that will decode the SHOUTcast stream and return metadata. This code works as intended, but when I port it to Android, the same code does not work. In particular, I cannot parse the HTTP response header from the SHOUTcast server. Where I can parse it just fine outside of Android, I seem to get nothing but garbage when the request is made via Android.
The corresponding code follows.
URL u = new URL("http://scfire-mtc-aa01.stream.aol.com:80/stream/1074"); URLConnection uc = u.openConnection(); uc.addRequestProperty("Icy-MetaData", "1"); InputStream in = uc.getInputStream(); byte[] byteheader = new byte[512]; int c = 0; int i = 0; int metaint = 0; while ((c = in.read()) != -1){ byteheader[i] = (byte)c; if (i > 4){ if (byteheader[i - 3] == '\r' && byteheader[i - 2] == '\n' && byteheader[i - 1] == '\r' && byteheader[i] == '\n') break; } i++; }
When launched on Android, this code overflows the "byteheader" buffer. When working outside of Android, it works correctly. To make things weirder, I sniffed a conversation through Wireshark and repeated the header sent by Android to the file. When using netcat for a request with the same header, I get a corresponding response. When I look at the output of Logcat on Android, "byteheader" contains only garbage.
My only idea is something environmental that I miss. Or, I will miss something massive obvious.
Any ideas?
As editing, I additionally highlighted the problem by creating a dummy application and placing only the violation code in it. The problem persists when Android returns the garbage and my identical external code works as expected. I thought this might be somehow related to character encoding, but it seems that both environments do not have UTF8 by default.
source share