Skip to content

Commit 0fb0b73

Browse files
Move AssetExtractor logic into a separate class
1 parent 0211dd4 commit 0fb0b73

2 files changed

Lines changed: 74 additions & 72 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.viliussutkus89.android.pdf2htmlex;
2+
3+
import android.content.res.AssetManager;
4+
import android.util.Log;
5+
6+
import androidx.annotation.NonNull;
7+
8+
import java.io.BufferedInputStream;
9+
import java.io.BufferedOutputStream;
10+
import java.io.File;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.io.OutputStream;
15+
16+
final class AssetExtractor {
17+
private static final String TAG = "AssetExtractor";
18+
19+
// ExtractAssets adapted from
20+
// https://gist.github.com/tylerchesley/6198074
21+
static Boolean extract(@NonNull AssetManager assetManager, @NonNull File outputDir, @NonNull String name) {
22+
File output_name = new File(outputDir, name);
23+
try {
24+
String[] assets = assetManager.list(name);
25+
if (0 == assets.length) { // Processing a file
26+
InputStream in = assetManager.open(name);
27+
OutputStream out = new FileOutputStream(output_name);
28+
if (!copyFile(in, out)) {
29+
return false;
30+
}
31+
} else { // Processing a folder
32+
if (!output_name.exists() && !output_name.mkdirs()) {
33+
return false;
34+
}
35+
for (String asset: assets) {
36+
if (!extract(assetManager, outputDir, name + File.separator + asset)) {
37+
return false;
38+
}
39+
}
40+
}
41+
} catch (IOException e) {
42+
Log.e(TAG, e.getMessage());
43+
return false;
44+
}
45+
return true;
46+
}
47+
48+
private static Boolean copyFile(InputStream input, OutputStream output) {
49+
final int buffer_size = 1024;
50+
byte[] buffer = new byte[buffer_size];
51+
52+
BufferedInputStream in = new BufferedInputStream(input, buffer_size);
53+
BufferedOutputStream out = new BufferedOutputStream(output, buffer_size);
54+
55+
try {
56+
int read;
57+
while (-1 != (read = in.read(buffer))) {
58+
out.write(buffer, 0, read);
59+
}
60+
out.flush();
61+
out.close();
62+
output.close();
63+
in.close();
64+
input.close();
65+
} catch (IOException e) {
66+
Log.e(TAG, e.getMessage());
67+
return false;
68+
}
69+
return true;
70+
}
71+
}

pdf2htmlEX/src/main/java/com/viliussutkus89/android/pdf2htmlex/pdf2htmlEX.java

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,12 @@
2020
package com.viliussutkus89.android.pdf2htmlex;
2121

2222
import android.content.Context;
23-
import android.content.res.AssetManager;
2423
import android.os.Build;
25-
import android.util.Log;
2624

2725
import androidx.annotation.NonNull;
2826

29-
import java.io.BufferedInputStream;
30-
import java.io.BufferedOutputStream;
3127
import java.io.File;
32-
import java.io.FileOutputStream;
3328
import java.io.IOException;
34-
import java.io.InputStream;
35-
import java.io.OutputStream;
3629

3730
public final class pdf2htmlEX {
3831
static {
@@ -41,59 +34,6 @@ public final class pdf2htmlEX {
4134

4235
private static final String TAG = "pdf2htmlEX";
4336

44-
// ExtractAssets adapted from
45-
// https://gist.github.com/tylerchesley/6198074
46-
private Boolean ExtractAssets(@NonNull AssetManager assetManager, @NonNull File outputDir, @NonNull String name) {
47-
File output_name = new File(outputDir, name);
48-
try {
49-
String[] assets = assetManager.list(name);
50-
if (0 == assets.length) { // Processing a file
51-
InputStream in = assetManager.open(name);
52-
OutputStream out = new FileOutputStream(output_name);
53-
if (!copyFile(in, out)) {
54-
return false;
55-
}
56-
} else { // Processing a folder
57-
if (!output_name.exists() && !output_name.mkdirs()) {
58-
return false;
59-
}
60-
for (String asset: assets) {
61-
if (!ExtractAssets(assetManager, outputDir, name + File.separator + asset)) {
62-
return false;
63-
}
64-
}
65-
}
66-
} catch (IOException e) {
67-
Log.e(TAG, e.getMessage());
68-
return false;
69-
}
70-
return true;
71-
}
72-
73-
private Boolean copyFile(InputStream input, OutputStream output) {
74-
final int buffer_size = 1024;
75-
byte[] buffer = new byte[buffer_size];
76-
77-
BufferedInputStream in = new BufferedInputStream(input, buffer_size);
78-
BufferedOutputStream out = new BufferedOutputStream(output, buffer_size);
79-
80-
try {
81-
int read;
82-
while (-1 != (read = in.read(buffer))) {
83-
out.write(buffer, 0, read);
84-
}
85-
out.flush();
86-
out.close();
87-
output.close();
88-
in.close();
89-
input.close();
90-
} catch (IOException e) {
91-
Log.e(TAG, e.getMessage());
92-
return false;
93-
}
94-
return true;
95-
}
96-
9737
private void prepareEnvironmentForFontforge(Context ctx) {
9838
File homeDir = new File(ctx.getCacheDir(), "homeForFontforge");
9939
homeDir.mkdir();
@@ -117,12 +57,12 @@ public pdf2htmlEX(@NonNull Context ctx) {
11757
// @TODO: https://github.com/ViliusSutkus89/pdf2htmlEX-Android/issues/9
11858
// pdf2htmlEX_dataDir is where pdf2htmlEX's share folder contents are
11959
m_pdf2htmlEX_dataDir = new File(filesDir, "pdf2htmlEX");
120-
ExtractAssets(ctx.getAssets(), filesDir, "pdf2htmlEX");
60+
AssetExtractor.extract(ctx.getAssets(), filesDir, "pdf2htmlEX");
12161

12262
// @TODO: https://github.com/ViliusSutkus89/pdf2htmlEX-Android/issues/10
12363
// Poppler requires encoding data
12464
m_poppler_dataDir = new File(filesDir, "poppler");
125-
ExtractAssets(ctx.getAssets(), filesDir, "poppler");
65+
AssetExtractor.extract(ctx.getAssets(), filesDir, "poppler");
12666

12767
// tmpDir is where pdf2htmlEX does it's work
12868
m_pdf2htmlEX_tmpDir = new File(cacheDir, "pdf2htmlEX-tmp");
@@ -133,15 +73,6 @@ public pdf2htmlEX(@NonNull Context ctx) {
13373

13474
prepareEnvironmentForFontforge(ctx);
13575

136-
// xdg-cache Used by fontconfig
137-
File xdgCache = new File(cacheDir, "xdg-cache");
138-
xdgCache.mkdir();
139-
set_environment_value("XDG_CACHE_HOME", xdgCache.getAbsolutePath());
140-
141-
// /etc/fonts is provided by fontconfig
142-
File fontsConfigDir = new File(filesDir, "etc/fonts");
143-
ExtractAssets(ctx.getAssets(), filesDir, "etc/fonts");
144-
set_environment_value("FONTCONFIG_PATH", fontsConfigDir.getAbsolutePath());
14576
}
14677

14778
public class ConversionFailedException extends Exception {
@@ -176,7 +107,7 @@ public File convert(@NonNull File inputPDF) throws IOException, ConversionFailed
176107
private native int call_pdf2htmlEX(String dataDir, String popplerDir, String tmpDir, String inputFile, String outputFile);
177108

178109
// Because Java cannot setenv for the current process
179-
private native void set_environment_value(String key, String value);
110+
static native void set_environment_value(String key, String value);
180111

181112
// Because Java can't set env vars for the current process...
182113
private native void set_env_values_for_fontforge(String homeDir, String tmpDir, String username);

0 commit comments

Comments
 (0)