HoppeJ / crap4j

Automatically exported from code.google.com/p/crap4j
0 stars 0 forks source link

CPStringReader throws too many files open error #17

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
With crap4j ant task v 1.1.6. The test runner will eventually fail after
processing a certain number of files since CPStringReader.readClass()
doesn't close the inputstream

Wrapping the method in a try/finally seems to have solved the problem.

    private static byte[] readClass(final InputStream is) throws IOException {
    try {
        if (is == null) {
        throw new IOException("Class not found");
        }
        byte[] b = new byte[is.available()];
        int len = 0;
        while (true) {
        int n = is.read(b, len, b.length - len);
        if (n == -1) {
            if (len < b.length) {
            byte[] c = new byte[len];
            System.arraycopy(b, 0, c, 0, len);
            b = c;
            }
            return b;
        }
        len += n;
        if (len == b.length) {
            byte[] c = new byte[b.length + 1000];
            System.arraycopy(b, 0, c, 0, len);
            b = c;
        }
        }
    } finally {
        try {
        is.close();
        } catch (IOException e) {

        }
    }
    }

Original issue reported on code.google.com by case.nel...@gmail.com on 25 May 2009 at 8:06