Last active
April 19, 2020 15:26
-
-
Save otokunaga2/9bb0344efceb4f2f06d0b00392d7b83f to your computer and use it in GitHub Desktop.
Convert InputStream to byte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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