diff --git a/src/main/java/com/monitorjbl/xlsx/sst/FileBackedList.java b/src/main/java/com/monitorjbl/xlsx/sst/FileBackedList.java index 423fac0a..d4d61b56 100644 --- a/src/main/java/com/monitorjbl/xlsx/sst/FileBackedList.java +++ b/src/main/java/com/monitorjbl/xlsx/sst/FileBackedList.java @@ -29,10 +29,12 @@ */ public class FileBackedList implements AutoCloseable { + private static final String EMPTY_STRING = ""; private final List pointers = new ArrayList<>(); private final RandomAccessFile raf; private final FileChannel channel; private final Map cache; + private final int cacheSize; private long filesize; @@ -40,6 +42,7 @@ public FileBackedList(File file, final int cacheSize) throws IOException { this.raf = new RandomAccessFile(file, "rw"); this.channel = raf.getChannel(); this.filesize = raf.length(); + this.cacheSize = cacheSize; this.cache = new LinkedHashMap(cacheSize, 0.75f, true) { public boolean removeEldestEntry(Map.Entry eldest) { return size() > cacheSize; @@ -49,9 +52,10 @@ public boolean removeEldestEntry(Map.Entry eldest) { public void add(String str) { try { - if (str!=null && str.length()>0) { + if (cacheSize > 0 || (str!=null && str.length()>0)) { writeToFile(str); } + } catch(IOException e) { throw new RuntimeException(e); } @@ -73,6 +77,9 @@ public String getAt(int index) { private void writeToFile(String str) throws IOException { synchronized (channel) { + if (str == null) { + str = EMPTY_STRING; + } ByteBuffer bytes = ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8)); ByteBuffer length = ByteBuffer.allocate(4).putInt(bytes.array().length); diff --git a/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java b/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java index d0420794..b9b3b645 100644 --- a/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java +++ b/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java @@ -510,6 +510,21 @@ public void testBlankNumerics() throws Exception { } } + @Test + public void testBlankCellWithSstCacheSize() throws Exception { + File f = new File("src/test/resources/blank_cell_to_test_sst_size.xlsx"); + Map> contents = new HashMap<>(); + try(Workbook wb = StreamingReader.builder().sstCacheSize(8).open(f)) { + for(Row row : wb.getSheetAt(0)) { + contents.put(row.getRowNum(), new ArrayList<>()); + for(Cell c : row) { + contents.get(row.getRowNum()).add(c); + } + } + } + assertThat(contents.get(1).get(2).getStringCellValue(), equalTo("")); + } + @Test public void testFirstRowNumIs0() throws Exception { File f = new File("src/test/resources/data_types.xlsx"); diff --git a/src/test/resources/blank_cell_to_test_sst_size.xlsx b/src/test/resources/blank_cell_to_test_sst_size.xlsx new file mode 100644 index 00000000..3d5906a7 Binary files /dev/null and b/src/test/resources/blank_cell_to_test_sst_size.xlsx differ