Skip to content

Commit 9babf5a

Browse files
committed
Prepare io module for iTextSharp
1 parent 4980a7e commit 9babf5a

File tree

12 files changed

+102
-159
lines changed

12 files changed

+102
-159
lines changed

io/src/main/java/com/itextpdf/io/font/FontProgram.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.itextpdf.io.IOException;
44
import com.itextpdf.io.font.otf.Glyph;
5+
import com.itextpdf.io.util.FileUtils;
56

6-
import java.io.File;
77
import java.io.Serializable;
88
import java.util.HashMap;
99
import java.util.Map;
@@ -234,11 +234,8 @@ protected void setFontFamily(String fontFamily) {
234234
}
235235

236236
protected void checkFilePath(String path) {
237-
if (path != null) {
238-
File f = new File(path);
239-
if (!FontConstants.BUILTIN_FONTS_14.contains(path) && (!f.exists() || !f.isFile())) {
240-
throw new IOException(IOException.FontFile1NotFound).setMessageParams(path);
241-
}
237+
if (path != null && !FontConstants.BUILTIN_FONTS_14.contains(path) && !FileUtils.fileExists(path)) {
238+
throw new IOException(IOException.FontFile1NotFound).setMessageParams(path);
242239
}
243240
}
244241

io/src/main/java/com/itextpdf/io/font/FontRegisterProvider.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.itextpdf.io.font;
22

33
import com.itextpdf.io.IOException;
4+
import com.itextpdf.io.util.FileUtils;
45
import com.itextpdf.io.util.Utilities;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

8-
import java.io.File;
99
import java.util.ArrayList;
1010
import java.util.HashMap;
1111
import java.util.List;
1212
import java.util.Map;
1313
import java.util.Set;
1414

15-
1615
/**
1716
* If you are using True Type fonts, you can declare the paths of the different ttf- and ttc-files
1817
* to this class first and then create fonts in your code using one of the getFont method
@@ -340,31 +339,26 @@ public int registerDirectory(final String dir, final boolean scanSubdirectories)
340339
}
341340
int count = 0;
342341
try {
343-
File file = new File(dir);
344-
if (!file.exists() || !file.isDirectory())
345-
return 0;
346-
String[] files = file.list();
342+
String[] files = FileUtils.getDirectoryList(dir);
347343
if (files == null)
348344
return 0;
349-
for (int k = 0; k < files.length; ++k) {
345+
for (String file : files) {
350346
try {
351-
file = new File(dir, files[k]);
352-
if (file.isDirectory()) {
347+
if (FileUtils.isDirectory(file)) {
353348
if (scanSubdirectories) {
354-
count += registerDirectory(file.getAbsolutePath(), true);
349+
count += registerDirectory(file, true);
355350
}
356351
} else {
357-
String name = file.getPath();
358-
String suffix = name.length() < 4 ? null : name.substring(name.length() - 4).toLowerCase();
352+
String suffix = file.length() < 4 ? null : file.substring(file.length() - 4).toLowerCase();
359353
if (".afm".equals(suffix) || ".pfm".equals(suffix)) {
360354
/* Only register Type 1 fonts with matching .pfb files */
361-
File pfb = new File(name.substring(0, name.length() - 4) + ".pfb");
362-
if (pfb.exists()) {
363-
register(name, null);
355+
String pfb = file.substring(0, file.length() - 4) + ".pfb";
356+
if (FileUtils.fileExists(pfb)) {
357+
register(file, null);
364358
++count;
365359
}
366360
} else if (".ttf".equals(suffix) || ".otf".equals(suffix) || ".ttc".equals(suffix)) {
367-
register(name, null);
361+
register(file, null);
368362
++count;
369363
}
370364
}
@@ -387,7 +381,7 @@ public int registerDirectory(final String dir, final boolean scanSubdirectories)
387381
public int registerSystemDirectories() {
388382
int count = 0;
389383
String[] withSubDirs = {
390-
Utilities.getFontsDir(),
384+
FileUtils.getFontsDir(),
391385
"/usr/share/X11/fonts",
392386
"/usr/X/lib/X11/fonts",
393387
"/usr/openwin/lib/X11/fonts",

io/src/main/java/com/itextpdf/io/font/OpenTypeParser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.itextpdf.io.IOException;
44
import com.itextpdf.io.source.RandomAccessFileOrArray;
55
import com.itextpdf.io.source.RandomAccessSourceFactory;
6+
import com.itextpdf.io.util.FileUtils;
67
import com.itextpdf.io.util.IntHashtable;
78

8-
import java.io.File;
99
import java.util.ArrayList;
1010
import java.util.LinkedHashMap;
1111
import java.util.List;
@@ -204,8 +204,7 @@ public String getPsFontName() {
204204
if (names != null && names.size() > 0) {
205205
fontName = names.get(0)[3];
206206
} else {
207-
File file = new File(fileName);
208-
fontName = file.getName().replace(' ', '-');
207+
fontName = FileUtils.getFileName(fileName).replace(' ', '-');
209208
}
210209
}
211210
return fontName;

io/src/main/java/com/itextpdf/io/font/TrueTypeCollection.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import com.itextpdf.io.IOException;
44
import com.itextpdf.io.source.RandomAccessFileOrArray;
55
import com.itextpdf.io.source.RandomAccessSourceFactory;
6-
7-
import java.io.File;
6+
import com.itextpdf.io.util.FileUtils;
87

98
/**
109
* Use this class for working with true type collection font (*.ttc)
@@ -26,7 +25,9 @@ public TrueTypeCollection(byte[] ttc, String encoding) throws java.io.IOExceptio
2625
}
2726

2827
public TrueTypeCollection(String ttcPath, String encoding) throws java.io.IOException {
29-
checkFilePath(ttcPath);
28+
if (!FileUtils.fileExists(ttcPath)) {
29+
throw new IOException(IOException.FontFile1NotFound).setMessageParams(ttcPath);
30+
}
3031
raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().createBestSource(ttcPath));
3132
this.ttcPath = ttcPath;
3233
this.encoding = encoding;
@@ -76,13 +77,4 @@ private void initFontSize() throws java.io.IOException {
7677
raf.skipBytes(4);
7778
TTCSize = raf.readInt();
7879
}
79-
80-
protected void checkFilePath(String path){
81-
if(path != null) {
82-
File f = new File(path);
83-
if ((!f.exists() || !f.isFile())) {
84-
throw new IOException(IOException.FontFile1NotFound).setMessageParams(path);
85-
}
86-
}
87-
}
8880
}

io/src/main/java/com/itextpdf/io/font/cmap/CMapLocationResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.itextpdf.io.source.PdfTokenizer;
66
import com.itextpdf.io.source.RandomAccessFileOrArray;
77
import com.itextpdf.io.source.RandomAccessSourceFactory;
8-
import com.itextpdf.io.source.StreamUtil;
8+
import com.itextpdf.io.util.Utilities;
99

1010
import java.io.InputStream;
1111

@@ -16,7 +16,7 @@ public class CMapLocationResource implements CMapLocation{
1616

1717
public PdfTokenizer getLocation(String location) throws java.io.IOException {
1818
String fullName = FontConstants.RESOURCE_PATH + "cmap/" + location;
19-
InputStream inp = StreamUtil.getResourceStream(fullName);
19+
InputStream inp = Utilities.getResourceStream(fullName);
2020
if (inp == null) {
2121
throw new IOException("the.cmap.1.was.not.found").setMessageParams(fullName);
2222
}

io/src/main/java/com/itextpdf/io/source/GetBufferedRandomAccessSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class GetBufferedRandomAccessSource implements RandomAccessSource {
1414
*/
1515
public GetBufferedRandomAccessSource(RandomAccessSource source) {
1616
this.source = source;
17-
this.getBuffer = new byte[(int)Math.min(Math.max(source.length()/4, 1), (long)4096)];
17+
this.getBuffer = new byte[(int)Math.min(Math.max(source.length()/4, 1), 4096)];
1818
this.getBufferStart = -1;
1919
this.getBufferEnd = -1;
2020
}

io/src/main/java/com/itextpdf/io/source/RandomAccessSourceFactory.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.itextpdf.io.source;
22

33
import com.itextpdf.io.IOException;
4+
import com.itextpdf.io.util.Utilities;
45

56
import java.io.File;
67
import java.io.FileInputStream;
@@ -103,7 +104,7 @@ public RandomAccessSource createSource(URL url) throws java.io.IOException{
103104
*/
104105
public RandomAccessSource createSource(InputStream inputStream) throws java.io.IOException{
105106
try {
106-
return createSource(StreamUtil.inputStreamToArray(inputStream));
107+
return createSource(Utilities.inputStreamToArray(inputStream));
107108
}
108109
finally {
109110
try {
@@ -124,7 +125,7 @@ public RandomAccessSource createSource(InputStream inputStream) throws java.io.I
124125
*/
125126
public RandomAccessSource createBestSource(String filename) throws java.io.IOException{
126127
File file = new File(filename);
127-
if (!file.canRead()){
128+
if (!file.canRead()) {
128129
if (filename.startsWith("file:/")
129130
|| filename.startsWith("http://")
130131
|| filename.startsWith("https://")
@@ -202,7 +203,7 @@ public RandomAccessSource createRanged(RandomAccessSource source, long[] ranges)
202203
* @throws java.io.IOException if reading the underling file or stream fails
203204
*/
204205
private RandomAccessSource createByReadingToMemory(String filename) throws java.io.IOException {
205-
InputStream stream = StreamUtil.getResourceStream(filename);
206+
InputStream stream = Utilities.getResourceStream(filename);
206207
if (stream == null) {
207208
throw new java.io.IOException(MessageFormat.format(IOException._1NotFoundAsFileOrResource, filename));
208209
}
@@ -217,7 +218,7 @@ private RandomAccessSource createByReadingToMemory(String filename) throws java.
217218
*/
218219
private RandomAccessSource createByReadingToMemory(InputStream stream) throws java.io.IOException {
219220
try {
220-
return new ArrayRandomAccessSource(StreamUtil.inputStreamToArray(stream));
221+
return new ArrayRandomAccessSource(Utilities.inputStreamToArray(stream));
221222
}
222223
finally {
223224
try {

io/src/main/java/com/itextpdf/io/source/StreamUtil.java

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.itextpdf.io.util;
2+
3+
import java.io.File;
4+
5+
public class FileUtils {
6+
7+
public static String getFontsDir() {
8+
String winDir = System.getenv("windir");
9+
String fileSeparator = System.getProperty("file.separator");
10+
return winDir + fileSeparator + "fonts";
11+
}
12+
13+
public static String getFileName(String file) {
14+
return new File(file).getName();
15+
}
16+
17+
public static boolean fileExists(String path) {
18+
if (path != null) {
19+
File f = new File(path);
20+
return f.exists() && f.isFile();
21+
}
22+
return false;
23+
}
24+
25+
public static boolean directoryExists(String path) {
26+
if (path != null) {
27+
File f = new File(path);
28+
return f.exists() && f.isDirectory();
29+
}
30+
return false;
31+
}
32+
33+
public static boolean isDirectory(String path) {
34+
return new File(path).isDirectory();
35+
}
36+
37+
public static String[] getDirectoryList(String path) {
38+
if (path != null) {
39+
File f = new File(path);
40+
if (f.exists() && f.isDirectory()) {
41+
File[] files = f.listFiles();
42+
if (files == null || files.length == 0) {
43+
return null;
44+
}
45+
String[] list = new String[files.length];
46+
for (int i = 0; i < files.length; i++) {
47+
list[i] = files[i].getAbsolutePath();
48+
}
49+
return list;
50+
}
51+
}
52+
return null;
53+
}
54+
}

0 commit comments

Comments
 (0)