Skip to content

Instantly share code, notes, and snippets.

@subinkrishna
Created January 24, 2015 00:11
Show Gist options
  • Save subinkrishna/a20a463cc965b05b1aa3 to your computer and use it in GitHub Desktop.
Save subinkrishna/a20a463cc965b05b1aa3 to your computer and use it in GitHub Desktop.
copyStream() - copies contents from one stream to other
public static void copy(final InputStream in,
final OutputStream out,
final boolean closeStreams) throws IOException {
final int kilobyte = 1024;
byte[] buffer = null;
int count = -1;
if ((null != in) && (null != out)) {
try {
buffer = new byte[kilobyte];
while (-1 != (count = in.read(buffer))) {
out.write(buffer, 0, count);
}
} finally {
buffer = null;
// Close streams
if (closeStreams) {
try {
in.close();
out.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment