#vulnerable code 
                                public String parse(InputStream stream, Metadata metadata)
            throws IOException, TikaException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));

        int charAsInt;

        while ((charAsInt = br.read()) != -1) {
            sb.append((char) charAsInt);
        }

        return sb.toString();
    } 
                            #location 12 
                            #vulnerability type RESOURCE_LEAK
#fixed code
                                public String parse(InputStream stream, Metadata metadata)
            throws IOException, TikaException {
        CharsetDetector detector = new CharsetDetector();

        // Use the declared character encoding, if available
        String encoding = metadata.get(Metadata.CONTENT_ENCODING);
        if (encoding != null) {
            detector.setDeclaredEncoding(encoding);
        }

        // CharsetDetector expects a stream to support marks
        if (!stream.markSupported()) {
            stream = new BufferedInputStream(stream);
        }

        detector.setText(stream);

        CharsetMatch match = detector.detect();
        if (match == null) {
            throw new TikaException("Unable to detect character encoding");
        }

        metadata.set(Metadata.CONTENT_TYPE, "text/plain");
        metadata.set(Metadata.CONTENT_ENCODING, match.getName());
        String language = match.getLanguage();
        if (language != null) {
            metadata.set(Metadata.CONTENT_LANGUAGE, match.getLanguage());
            metadata.set(Metadata.LANGUAGE, match.getLanguage());
        }

        return match.getString();
    }