Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for the Issue #186 - #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/com/monitorjbl/xlsx/sst/FileBackedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@
*/
public class FileBackedList implements AutoCloseable {

private static final String EMPTY_STRING = "";
private final List<Long> pointers = new ArrayList<>();
private final RandomAccessFile raf;
private final FileChannel channel;
private final Map<Integer, String> cache;
private final int cacheSize;

private long filesize;

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<Integer, String>(cacheSize, 0.75f, true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > cacheSize;
Expand All @@ -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)) {
Comment on lines -52 to +55
Copy link

@jamesblewitt jamesblewitt Nov 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that empty (but not null) strings shouldn't be added to the shared string table?
Not adding it (when it is expected) could mess up all the subsequent indexes. Reading values in an Excel row could then receive the wrong values from the shared string table.

writeToFile(str);
}

} catch(IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -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);

Expand Down
19 changes: 17 additions & 2 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public void testSpecialStyles() throws Exception {
@Test
public void testBlankNumerics() throws Exception {
File f = new File("src/test/resources/blank_cells.xlsx");
try(Workbook wb = StreamingReader.builder().open(f)) {
try(Workbook wb = StreamingReader.builder().sstCacheSize(2).open(f)) {
Row row = wb.getSheetAt(0).iterator().next();
assertThat(row.getCell(1).getStringCellValue(), equalTo(""));
assertThat(row.getCell(1).getRichStringCellValue().getString(), equalTo(""));
Expand All @@ -510,10 +510,25 @@ 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<Integer, List<Cell>> 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");
try(Workbook wb = StreamingReader.builder().open(f)) {
try(Workbook wb = StreamingReader.builder().sstCacheSize(4).open(f)) {
Row row = wb.getSheetAt(0).iterator().next();
assertThat(row.getRowNum(), equalTo(0));
}
Expand Down
Binary file not shown.