Skip to content

Commit dc95cfb

Browse files
Update Issue #94 workaround. Replace string in file in-place. Reading whole file consumes a lot of memory
com.viliussutkus89.android.pdf2htmlex.ConversionTests > convertPDF[/data/user/0/app.opendocument.android.pdf2htmlex.test/cache/testPDFs/issue_83.pdf][emulator-5554 - 10] FAILED java.lang.OutOfMemoryError: Failed to allocate a 14893560 byte allocation with 3895928 free bytes and 3804KB until OOM, target footprint 16777216, growth limit 16777216 at java.util.Arrays.copyOf(Arrays.java:3257)
1 parent 2a70a3a commit dc95cfb

1 file changed

Lines changed: 33 additions & 17 deletions

File tree

  • pdf2htmlEX/src/main/java/app/opendocument/android/pdf2htmlex

pdf2htmlEX/src/main/java/app/opendocument/android/pdf2htmlex/pdf2htmlEX.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* pdf2htmlEX.java
33
*
4-
* Copyright (C) 2019, 2020, 2022, 2023 ViliusSutkus89.com
4+
* Copyright (C) 2019, 2020, 2022, 2023, 2024 ViliusSutkus89.com
55
*
66
* pdf2htmlEX-Android is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
@@ -28,13 +28,16 @@
2828
import com.viliussutkus89.android.assetextractor.AssetExtractor;
2929
import com.viliussutkus89.android.tmpfile.Tmpfile;
3030

31+
import java.io.BufferedInputStream;
3132
import java.io.Closeable;
3233
import java.io.File;
3334
import java.io.FileInputStream;
3435
import java.io.FileNotFoundException;
35-
import java.io.FileOutputStream;
3636
import java.io.IOException;
37+
import java.io.RandomAccessFile;
3738
import java.nio.charset.StandardCharsets;
39+
import java.util.LinkedList;
40+
import java.util.List;
3841

3942

4043
@SuppressWarnings("unused")
@@ -181,24 +184,37 @@ public File convert() throws IOException, ConversionFailedException {
181184

182185
int retVal = NativeConverter.convert(nc.mConverter);
183186
if (0 == retVal) {
184-
// Workaround for https://github.com/opendocument-app/pdf2htmlEX-Android/issues/94
185-
String convertedDocumentText;
186-
try (FileInputStream fis = new FileInputStream(outputFile)) {
187-
byte[] data = new byte[(int) outputFile.length()];
188-
fis.read(data);
189-
convertedDocumentText = new String(data, StandardCharsets.UTF_8);
187+
// Workaround for https://github.com/opendocument-app/pdf2htmlEX-Android/issues/94
188+
189+
byte[] needle = "{font-family:sans-serif;visibility:hidden;}".getBytes(StandardCharsets.UTF_8);
190+
byte[] replacement = "{font-family:sans-serif;visibility:visible}".getBytes(StandardCharsets.UTF_8);
191+
List<Long> needlePositions = new LinkedList<>();
192+
try (FileInputStream fis = new FileInputStream(outputFile)) {
193+
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
194+
long totalRead = 0;
195+
int matchedNeedle = 0;
196+
byte[] buffer = new byte[1];
197+
while (1 == bis.read(buffer)) {
198+
if (buffer[0] == needle[matchedNeedle]) {
199+
if (++matchedNeedle == needle.length) {
200+
needlePositions.add(totalRead - needle.length + 1);
201+
matchedNeedle = 0;
202+
}
203+
} else {
204+
matchedNeedle = 0;
205+
}
206+
totalRead++;
207+
}
190208
}
191-
if (!convertedDocumentText.isEmpty()) {
192-
convertedDocumentText = convertedDocumentText.replace(
193-
"{font-family:sans-serif;visibility:hidden;}",
194-
"{font-family:sans-serif;visibility:visible;}"
195-
);
196-
File defaultFontVisibleFile = new File(outputFile + ".default-font-visible.html");
197-
try (FileOutputStream fos = new FileOutputStream(defaultFontVisibleFile)) {
198-
fos.write(convertedDocumentText.getBytes(StandardCharsets.UTF_8));
199-
defaultFontVisibleFile.renameTo(outputFile);
209+
}
210+
if (!needlePositions.isEmpty()) {
211+
try (RandomAccessFile raf = new RandomAccessFile(outputFile, "rw")) {
212+
for (long i : needlePositions) {
213+
raf.seek(i);
214+
raf.write(replacement, 0, replacement.length);
200215
}
201216
}
217+
}
202218
return outputFile;
203219
}
204220

0 commit comments

Comments
 (0)