Created
July 13, 2015 18:06
-
-
Save wangyingang/6b81394c9c32bc6e5cc7 to your computer and use it in GitHub Desktop.
Convert InputStream to Byte Array
This file contains 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
public class Utils { | |
public static byte[] getBytes(InputStream is) throws IOException { | |
int len; | |
int size = 1024; | |
byte[] buf; | |
if (is instanceof ByteArrayInputStream) { | |
size = is.available(); | |
buf = new byte[size]; | |
len = is.read(buf, 0, size); | |
} else { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
buf = new byte[size]; | |
while ((len = is.read(buf, 0, size)) != -1) | |
bos.write(buf, 0, len); | |
buf = bos.toByteArray(); | |
} | |
return buf; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment