public static byte[] readChunked(InputStream inputStream) throws IOException {
		ByteArrayOutputStream tmpbuf = new ByteArrayOutputStream();
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		int ch;
		long bytesRead = 0;
		try{
		//while not closed:
			while(true){
				//read out the size.
				int lastCh =0;
				 while ((ch = inputStream.read()) >= 0) {
					 	tmpbuf.write(ch);
			            if (ch == '\n' && lastCh=='\r') {
			                break;
			            }
			            lastCh=ch;
			     }
				 String s = new String(tmpbuf.toByteArray());
				 s = s.replaceAll(("[\\n\\r]"), "");
				 long size = Long.parseLong(s, 16);
				 tmpbuf.reset();
				 //The last-chunk is a regular chunk, with the exception that its length is zero.
				 if(size==0){
					 break;
				 }
				//read content ends with \r\n
				while (bytesRead<size && (ch = inputStream.read()) >= 0) {//errr off by 1?
					buf.write(ch);
					bytesRead++;
				}
				//"read content ends with \r\n"
				ch = inputStream.read();
				ch = inputStream.read();
			}
		}catch(SocketTimeoutException STE){
	        	//It might be ok. We'll Try to recover from this.
	    }
		
		if (buf.size() == 0) {
			return new byte[0];
		}
		return buf.toByteArray();
	 }