I have gotten HttpURLConnection inputStream:
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
Now I want to produce two copy of this inputStream, one is used to store to a file, and the other one is used to parse, but after I stored, the inputStream is invalid to parse:
BufferedInputStream bis = new BufferedInputStream(inStream);
try {
byte[] buffer = new byte[1024];
if (inStream.markSupported()) {
inStream.mark(1);
}
int bytesRead = 0;
while ((bytesRead = bis.read(buffer)) != -1) {
Log.d(TAG, "buffer: "+new String(buffer));
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
inStream.reset();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now how to get this inputStream entirely again?