FontFileProviderImpl.java
5.92 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.osgi.framework.BundleContext
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.image.internal.font;
import com.day.image.internal.font.FontFileProvider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={FontFileProvider.class})
public class FontFileProviderImpl
implements FontFileProvider {
private final Logger log;
private static final String DIGEST = "SHA-1";
private static final String FILENAME_PREFIX = FontFileProviderImpl.class.getSimpleName() + "_";
private static final String FILE_EXT = ".font";
private static final char[] HEX = "0123456789abcdef".toCharArray();
private BundleContext bundleContext;
private AtomicInteger tmpFileCounter;
public FontFileProviderImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
this.tmpFileCounter = new AtomicInteger();
}
@Activate
protected void activate(ComponentContext ctx) {
this.bundleContext = ctx.getBundleContext();
File f = ctx.getBundleContext().getDataFile("FOO");
this.deleteMatchingFiles(f.getParentFile(), FILENAME_PREFIX);
}
void deleteMatchingFiles(File folder, String namePrefix) {
if (!folder.isDirectory()) {
this.log.debug("deleteMatchingFiles: {} is not a folder", (Object)folder.getAbsolutePath());
return;
}
String[] names = folder.list();
int deleted = 0;
if (names != null) {
for (String name : names) {
if (!name.startsWith(namePrefix)) continue;
File f = new File(folder, name);
f.delete();
if (f.exists()) {
this.log.info("Failed to delete font file {}, might be in use. Will try again next time", (Object)f.getAbsolutePath());
continue;
}
this.log.debug("Deleted possibly unused font file {}", (Object)f.getAbsolutePath());
++deleted;
}
}
if (deleted > 0) {
this.log.info("{} possibly unused font files deleted from {}", (Object)deleted, (Object)folder.getAbsolutePath());
} else {
this.log.info("No unused font files found under {}", (Object)folder.getAbsolutePath());
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public File getFileForStream(InputStream is) throws IOException {
File result;
block14 : {
result = null;
File tmp = this.bundleContext.getDataFile("TMP_" + this.tmpFileCounter.incrementAndGet() + "_" + System.currentTimeMillis() + ".tmp");
try {
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-1");
}
catch (NoSuchAlgorithmException nse) {
throw (IOException)new IOException("NoSuchAlgorithmException with digest=SHA-1").initCause(nse);
}
DigestOutputStream os = new DigestOutputStream(new FileOutputStream(tmp), digest);
byte[] buffer = new byte[8192];
int n = 0;
while ((n = is.read(buffer, 0, buffer.length)) > 0) {
os.write(buffer, 0, n);
}
os.flush();
os.close();
result = this.bundleContext.getDataFile(this.digestToFilename(digest.digest()));
boolean fileExists = false;
FontFileProviderImpl fontFileProviderImpl = this;
synchronized (fontFileProviderImpl) {
fileExists = result.canRead();
if (!fileExists) {
tmp.renameTo(result);
if (!result.canRead()) {
throw new IOException("Error renaming " + tmp.getAbsolutePath() + " to " + result.getAbsolutePath());
}
tmp = null;
}
}
if (fileExists) {
this.log.debug("{} font file already provided, using it as is", (Object)result.getAbsolutePath());
break block14;
}
result.deleteOnExit();
this.log.info("Font file created for new content (will be deleted on exit): {}", (Object)result.getAbsolutePath());
}
finally {
if (tmp != null) {
this.log.debug("Deleting unused temp file {}", (Object)tmp.getAbsolutePath());
tmp.delete();
if (tmp.exists()) {
this.log.warn("Failed to delete temporary file {}", (Object)tmp.getAbsolutePath());
}
}
is.close();
}
}
return result;
}
private String digestToFilename(byte[] digest) {
StringBuilder sb = new StringBuilder(FILENAME_PREFIX);
for (byte b : digest) {
sb.append(HEX[b >> 4 & 15]);
sb.append(HEX[b & 15]);
}
sb.append(".font");
return sb.toString();
}
}