0

I made a job that needs to read a Excel file with Apache POI 3.14, it works fine locally on my Ubuntu machine, but fails in production, Windows Server 2008 R2.

With the following exception: Potential loop detected - Block 0 was already claimed but was just requested again

[java] Exception in thread "main" java.lang.IllegalStateException: Potential loop detected - Block 0 was already claimed but was just requested again
 [java]     at org.apache.poi.poifs.filesystem.BlockStore$ChainLoopDetector.claim(BlockStore.java:100)
 [java]     at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:168)
 [java]     at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:142)
 [java]     at org.apache.poi.poifs.property.NPropertyTable.buildProperties(NPropertyTable.java:87)
 [java]     at org.apache.poi.poifs.property.NPropertyTable.<init>(NPropertyTable.java:66)
 [java]     at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.readCoreContents(NPOIFSFileSystem.java:440)
 [java]     at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:235)
 [java]     at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:168)
 [java]     at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:149)

Before i start reading excel file and process it (sync) i move the file from ftp location to tmp folder.

Example code:

@Service
public class UsersExcelSyncHelper {

private File usersExcelFileSource;
private File usersExcelFileDestination;

private static String source;
private static String destination;

@Autowired
public UsersExcelSyncHelper(@Value("${usersSyncFtpFileLocation:@null}") String source,
                            @Value("${usersSyncTempFolder:@null}") String destination){
    this.source = source;
    this.destination = destination;
}

public List<UserRow> getUsersRowFromExcel() {

    try {

        prepareFiles();
        moveFile(usersExcelFileSource, usersExcelFileDestination);
        List<UserRow> usersRows = getUserRows();
        forceDelete(usersExcelFileDestination);
        return usersRows;

    } catch (IOException e) {
       ...
    }

}

private List<UserRow> getUserRows() throws IOException, InvalidFormatException {

    //FileInputStream excelInputStream = openInputStream(usersExcelFileDestination);
    //Workbook workbook = WorkbookFactory.create(excelInputStream);
//Workbook workbook = WorkbookFactory.create(usersExcelFileDestination);

    NPOIFSFileSystem fs = new NPOIFSFileSystem(usersExcelFileDestination);
    HSSFWorkbook workbook = new HSSFWorkbook(fs.getRoot(), true);  <-------- Fails here
    fs.close();

    Stream<Row> rowStream = asStream(workbook.getSheetAt(0).iterator());

    List<UserRow> usersRows = getUserRows(rowStream);

    return usersRows;
}

private void prepareFiles() {
    log.info("source: " + source);
    this.usersExcelFileSource = getUsersExcelFile(source);
    final String destinationFile = destination + System.getProperty("file.separator") + usersExcelFileSource.getName();
    log.info("destination: " + destinationFile);
    this.usersExcelFileDestination = new File(destinationFile);
}

private File getUsersExcelFile(String excelFileLocation) {

    File dir = new File(excelFileLocation);
    if(!dir.isDirectory()){
        throw new HomemadeException(excelFileLocation + " folder must exist!");
    }
    File[] files = dir.listFiles();
    if(files.length != 1){
        throw new HomemadeException("only one file for synchronization!");
    }
    return files[0];
}

...

}

Any ideas? I already updated library 3.14, changed from Inputstream to File and lastly NPOIFSFileSystem. Greatly appreciated any help

3
  • 1
    Sounds like the file is corrupted while transferred via FTP, I would verify this by manually copying the file onto the two machines and try to process it there without FTP being involved.
    – centic
    Jun 6, 2016 at 10:49
  • Thanks centic, i verified with md5sum both files and in fact they where different, so i add the 'binary mode' while transfering the file with ftp and it fixed the issue! Thanks! - add an answer so i can rate it Jun 6, 2016 at 13:49
  • Ok, good that we found it!
    – centic
    Jun 6, 2016 at 15:11

1 Answer 1

3

Sounds like the file is corrupted while transferred via FTP, verify this by manually copying the file onto the two machines and try to process it there without FTP being involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.