-
Notifications
You must be signed in to change notification settings - Fork 188
/
TestResourceUtils.java
111 lines (87 loc) · 3.33 KB
/
TestResourceUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Created on Dec 21, 2009
* (c) 2009 Trumpet, Inc.
*
*/
package com.itextpdf.testutils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.itextpdf.text.pdf.PdfReader;
/**
* @author kevin
*/
public final class TestResourceUtils {
private static final String TESTPREFIX = "itexttest_";
private TestResourceUtils() {
}
public static String getFullyQualifiedResourceName(Class context, String resourceName){
return context.getName().replace('.', '/') + "/" + resourceName;
}
public static InputStream getResourceAsStream(Object context, String resourceName){
Class contextClass;
if (context instanceof Class){
contextClass = (Class)context;
}else{
contextClass = context.getClass();
}
return contextClass.getClassLoader().getResourceAsStream(getFullyQualifiedResourceName(contextClass, resourceName));
}
public static void purgeTempFiles(){
File tempFolder = new File(System.getProperty("java.io.tmpdir"));
File[] itextTempFiles = tempFolder.listFiles(new FileFilter(){
public boolean accept(File pathname) {
return pathname.getName().startsWith(TESTPREFIX);
}
});
for (File file : itextTempFiles) {
if (!file.delete()){
System.err.println("Unable to delete iText temporary test file " + file);
}
}
}
public static File getResourceAsTempFile(Object context, String resourceName) throws IOException{
File f = File.createTempFile(TESTPREFIX, ".pdf");
f.deleteOnExit();
InputStream is = getResourceAsStream(context, resourceName);
final OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
try{
writeInputToOutput(is, os);
} finally {
is.close();
os.close();
}
return f;
}
public static PdfReader getResourceAsPdfReader(Object context, String resourceName) throws IOException{
return new PdfReader(new BufferedInputStream(getResourceAsStream(context, resourceName)));
}
public static byte[] getResourceAsByteArray(Object context, String resourceName) throws IOException{
InputStream inputStream = getResourceAsStream(context, resourceName);
final ByteArrayOutputStream fileBytes = new ByteArrayOutputStream();
try{
writeInputToOutput(inputStream, fileBytes);
} finally {
inputStream.close();
}
return fileBytes.toByteArray();
}
private static void writeInputToOutput(InputStream is, OutputStream os) throws IOException{
final byte[] buffer = new byte[8192];
while (true)
{
final int bytesRead = is.read(buffer);
if (bytesRead == -1)
{
break;
}
os.write(buffer, 0, bytesRead);
}
}
}