Chunked transfer encoding problem
-
Hi guys, I think I'm going to lose my mind with this, I cant make my chunked transfer manager to work right. The problem I'm having is that reading the amount of data specified in the chunk-size, I end up having the next chunk size inside the body, so, when I call the next getchunksize, it fails. The first call to getChunkSize() works great and I get the size value as expected. The BufferedReader is connected to the sock
s = new Socket(connectTo, portNum); sIn = new BufferedReader(new InputStreamReader(s.getInputStream()) );
Thanks a lot guys, I hope any of you understands what's going on...private String processChunked(BufferedReader sIn, String newData) throws IOException { newData += LINEBREAK; // Let's separate header form body int size = 0; String body = new String(); size = getChunkSize(sIn); while(size > 0) { body = getChunkData(sIn, size); size = getChunkSize(sIn); newData += body; body = ""; } return newData; } private String getChunkData(BufferedReader sIn, int lenght) throws IOException { int val = 0; String res = new String(); char chr = ' '; for(int i= 0; i val = sIn.read(); if(val == -1) break; chr = (char)val; res += Character.valueOf(chr).toString(); } return res; } private int getChunkSize(BufferedReader sIn) throws IOException { char chr = ' ', prvChr = ' '; int val = 0; String tmpSize = new String(); int size = 0; int readed = 0; do { //Read chunk size prvChr = chr; val = sIn.read(); if (val == -1) { break; } readed++; chr = (char) val; tmpSize += Character.valueOf(chr).toString(); } while (prvChr != '\r' && chr != '\n'); // calculate chunk size if(tmpSize.indexOf(";") != -1) { //remove chunk extension tmpSize = tmpSize.substring(0, tmpSi