Skip to content

Instantly share code, notes, and snippets.

@otokunaga2
Last active April 19, 2020 15:26
Show Gist options
  • Save otokunaga2/9bb0344efceb4f2f06d0b00392d7b83f to your computer and use it in GitHub Desktop.
Save otokunaga2/9bb0344efceb4f2f06d0b00392d7b83f to your computer and use it in GitHub Desktop.
Convert InputStream to byte
//https://www.techiedelight.com/convert-inputstream-byte-array-java/
private static byte[] toByteArray(InputStream in) throws IOException{
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
//Reads up to len bytes of data from the input stream into an array of bytes.
//Check: https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read-byte:A-
while((len = in.read(buffer)) != -1){
//指定されたバイト配列のオフセット位置offから始まるlenバイトをこのバイト配列出力ストリームに書き込みます。
os.write(buffer, 0, len);
}
return os.toByteArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment