Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

What do I need to close to free memory ?

$
0
0
Hi.

I use poi to read Excel files in a task in Bonita BPM.
When I run this task more than 3 times, it won't work again and I see this error in the log :
org.bonitasoft.engine.connector.exception.SConnect orException: java.util.concurrent.ExecutionException: java.lang.ExceptionInInitializerError

I think it happens because I try to read multiple time in the same file.
I probably need to free memory by closing something in my program but I don't know what.

Here is my code :

Code:

public static Workbook openWorkbook(String nomFichier, String password) {
        try {
            FileInputStream file = new FileInputStream(new File(nomFichier));
            BufferedInputStream bufferInput = new BufferedInputStream(file);
            file.close();
            if (nomFichier.endsWith("xls")) {
                POIFSFileSystem pfs = new POIFSFileSystem(bufferInput);
                Biff8EncryptionKey.setCurrentUserPassword(password);
                bufferInput.close();
                return new HSSFWorkbook(pfs);
            }
            if (nomFichier.endsWith("xlsx")) {
                POIFSFileSystem pfs = new POIFSFileSystem(bufferInput);
                EncryptionInfo encInfo = new EncryptionInfo(pfs);
                Decryptor decryptor = Decryptor.getInstance(encInfo);
                decryptor.verifyPassword(password);
                bufferInput.close();
                return new XSSFWorkbook(decryptor.getDataStream(pfs));
            }
            if (nomFichier.endsWith("xlsm")) {
                OPCPackage pkg = OPCPackage.open(new File(nomFichier));
                bufferInput.close();
                XSSFWorkbook wb = new XSSFWorkbook(pkg);
                pkg.close();
                return wb;
            }
            bufferInput.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Le fichier doit être au format .xls, .xlsm ou .xlsx !");
        return null;
    }

public static ArrayList<String> getListeIdRelease(String nomFichier, String password, String nomOnglet){
        Workbook workbook = openWorkbook(nomFichier, password);
        Sheet sheet = workbook.getSheet(nomOnglet);
        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        Iterator<Row> rowIterator = sheet.iterator();
        TreeSet<String> listeIdRelease = new TreeSet<String>(Collator.getInstance());
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Cell cellRelease = row.getCell(releaseIndex);
            Cell cellEtat = row.getCell(etatIndex);
            if (cellRelease != null && cellEtat.getStringCellValue().equals("")) {
                listeIdRelease.add(getCellValueToString(cellRelease, evaluator));
            }
        }
        return new ArrayList<String>(listeIdRelease);
    }

I already close the file, bufferInput and OPCPackage.
Did you have and idea of what can cause this problem ?

Thanks in advance.

Viewing all articles
Browse latest Browse all 120

Trending Articles