Skip to content

Instantly share code, notes, and snippets.

@abeykoon
Last active August 5, 2020 12:20
Show Gist options
  • Save abeykoon/b6067b5cfe58b2967b291ef584b37b40 to your computer and use it in GitHub Desktop.
Save abeykoon/b6067b5cfe58b2967b291ef584b37b40 to your computer and use it in GitHub Desktop.
How to stream bytes received from socket to a file
InputStream requestStream = httpExchange.getRequestBody();
File targetFile = new File("/Users/hasitha/temp/file-streaming/targetFile/targetFile.mkv");
OutputStream outStream = null;
try {
outStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = requestStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
outStream.flush();
}
} finally {
if(outStream != null) {
outStream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment