读书人

今天看到一个方法觉得写得不错共享

发布时间: 2012-09-08 10:48:07 作者: rapoo

今天看到一个方法,觉得写得不错,共享一下

Java code
/**     * 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;    }


[解决办法]
谢谢楼主,学习!
[解决办法]
Get it....
[解决办法]
探讨
Java code

/**
* Reads this input stream and returns contents as a byte[]
* from:aspectjweaver.jar
*/
public static byte[] readAsByteArray(InputStream inStream) throws IOException ……

[解决办法]
apache的IOUtil就有类似的函数。。。
http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html

static byte[] toByteArray(InputStream input)




[解决办法]
谢谢楼主!
[解决办法]
good
[解决办法]
解释一下,没看懂
[解决办法]
难道没人见过 ByteArrayOutputStream 么?
[解决办法]
Java code
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 .......
[解决办法]
感谢分享哈
[解决办法]
看看,学习
[解决办法]
谢谢,虽然没看懂
[解决办法]
好方法

读书人网 >J2SE开发

热点推荐