今天看到一个方法,觉得写得不错,共享一下
/** * Reads this input stream and returns contents as a byte[] * from:aspectjweaver.jar */ public static byte[] readAsByteArray(InputStream inStream) throws IOException { int size = 1024; byte[] buff = new byte[size]; int readSoFar = 0; while (true) { int nRead = inStream.read(buff, readSoFar, size - readSoFar); if (nRead == -1) { break; } readSoFar += nRead; if (readSoFar == size) { int newSize = size * 2; byte[] newBuff = new byte[newSize]; System.arraycopy(buff, 0, newBuff, 0, size); buff = newBuff; size = newSize; } } byte[] newBuff = new byte[readSoFar]; System.arraycopy(buff, 0, newBuff, 0, readSoFar); return newBuff; }public static byte[] readStreamAsBytes(InputStream in) throws IOException { if (in == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] bys = new byte[4096]; for (int p = -1; (p = in.read(bys)) != -1; ) { out.write(bys, 0, p); } return out.toByteArray();}
[解决办法]
good good .......
[解决办法]
感谢分享哈
[解决办法]
看看,学习
[解决办法]
谢谢,虽然没看懂
[解决办法]
好方法