MemoryUtil.java
7.48 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* org.apache.commons.io.FileUtils
* org.apache.commons.lang.StringUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.util;
import com.day.cq.dam.api.Asset;
import java.io.IOException;
import java.util.Map;
import javax.imageio.IIOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MemoryUtil {
private static final String TIFF_BITS_PER_SAMPLE = "tiff:BitsPerSample";
private static final String DAM_BITSPERPIXEL = "dam:Bitsperpixel";
private static final String PHOTOSHOP_COLOR_MODE = "photoshop:ColorMode";
private static final String TIFF_SAMPLES_PER_PIXEL = "tiff:SamplesPerPixel";
private static final Logger log = LoggerFactory.getLogger(MemoryUtil.class);
public static long MIN_CQ_MEMORY_REQUIREMENT = 104857600;
private static final int DEFAULT_MAX_TRIALS = 100;
public static <T> T tryUntilEnoughMemory(Asset asset, Callback<T> callback) {
return MemoryUtil.tryUntilEnoughMemory(asset, 100, callback);
}
public static <T> T tryUntilEnoughMemory(Asset asset, int maxTrials, Callback<T> callback) {
int trials;
if (!MemoryUtil.hasEnoughSystemMemory(asset)) {
log.warn("Failed loading image, insufficient memory. Increase heap size up to {} for asset: {}.", (Object)FileUtils.byteCountToDisplaySize((long)MemoryUtil.suggestMaxHeapSize(asset)), (Object)asset.getPath());
return null;
}
int n = trials = maxTrials <= 0 ? 1 : maxTrials;
while (trials > 0) {
try {
return callback.execute();
}
catch (IOException e) {
if (e instanceof IIOException && e.getMessage().contains("Not enough memory")) {
--trials;
log.info("Insufficient memory, reloading image. Free memory: {}, asset: {}", (Object)FileUtils.byteCountToDisplaySize((long)Runtime.getRuntime().freeMemory()), (Object)asset.getPath());
try {
Thread.sleep((long)(2500.0 * (Math.random() + 0.5)));
continue;
}
catch (InterruptedException ie) {
return null;
}
}
log.warn("Error while loading image for asset {}: ", (Object)asset.getPath(), (Object)e);
return null;
}
}
log.warn("Failed loading image, insufficient memory even after {} trials for asset: {}", (Object)maxTrials, (Object)asset.getPath());
return null;
}
public static boolean hasEnoughSystemMemory(Asset asset) {
if (MemoryUtil.canCalculate(asset)) {
long expectedImageMem = MemoryUtil.getExpectedImageMemory(asset);
return Runtime.getRuntime().maxMemory() > MIN_CQ_MEMORY_REQUIREMENT + expectedImageMem;
}
log.debug("Cannot calculate memory requirements for " + asset.getPath());
return true;
}
public static long getExpectedImageMemory(Asset asset) {
if (MemoryUtil.canCalculate(asset)) {
Long length = MemoryUtil.getAsLong(asset, "exif:PixelXDimension");
Long width = MemoryUtil.getAsLong(asset, "exif:PixelYDimension");
if (length == null && width == null) {
length = MemoryUtil.getAsLong(asset, "tiff:ImageLength");
width = MemoryUtil.getAsLong(asset, "tiff:ImageWidth");
}
if (length == null && width == null) {
return 4 * width * length;
}
}
return -1;
}
private static int getPixelSize(Asset asset, Map<String, Object> metadata) {
int pixelSize = 4;
int oneByte = 8;
if (metadata.containsKey("dam:Bitsperpixel")) {
pixelSize = (int)MemoryUtil.getAsLong(asset, metadata, "dam:Bitsperpixel");
return pixelSize /= oneByte;
}
int bitsPerChannel = (int)MemoryUtil.getBitsPerSample(asset, metadata);
int bytesPerChannel = bitsPerChannel / oneByte;
if (metadata.containsKey("photoshop:ColorMode")) {
int colorMode = (int)MemoryUtil.getAsLong(asset, metadata, "photoshop:ColorMode");
switch (colorMode) {
case 0:
case 1:
case 2: {
pixelSize = 1;
break;
}
case 3: {
pixelSize = 3;
break;
}
default: {
pixelSize = 4;
}
}
return pixelSize * bytesPerChannel;
}
if (metadata.containsKey("tiff:SamplesPerPixel")) {
pixelSize = (int)MemoryUtil.getAsLong(asset, metadata, "tiff:SamplesPerPixel");
return pixelSize * bytesPerChannel;
}
return pixelSize;
}
private static long getBitsPerSample(Asset asset, Map<String, Object> metadata) {
Object[] values;
Object value = metadata.get("tiff:BitsPerSample");
if (value instanceof Long) {
return (Long)value;
}
if (value instanceof Object[] && (values = (Object[])value).length > 0) {
return MemoryUtil.getAsLong(asset, values[0]);
}
return 8;
}
private static long getAsLong(Asset asset, Map<String, Object> metadata, String name) {
String value = asset.getMetadataValue(name);
Long retValue = null;
try {
retValue = Long.parseLong(value);
}
catch (NumberFormatException ignore) {
// empty catch block
}
return retValue;
}
private static long getAsLong(Asset asset, Object value) {
if (value instanceof Long) {
return (Long)value;
}
if (value instanceof Double) {
return ((Double)value).longValue();
}
if (value instanceof String) {
String strVal = (String)value;
try {
return Long.valueOf(strVal);
}
catch (NumberFormatException nme) {
log.debug("Cannot convert {} to number for asset {}", value, (Object)asset.getPath());
}
}
return 0;
}
public static boolean hasEnoughMemory(Asset asset) {
if (MemoryUtil.canCalculate(asset)) {
long expectedImageMem = MemoryUtil.getExpectedImageMemory(asset);
Runtime rt = Runtime.getRuntime();
long maxFreeMem = rt.maxMemory() - (rt.totalMemory() - rt.freeMemory());
return maxFreeMem >= expectedImageMem;
}
log.debug("Cannot calculate memory requirements for " + asset.getPath());
return true;
}
public static long suggestMaxHeapSize(Asset asset) {
return MemoryUtil.getExpectedImageMemory(asset) + MIN_CQ_MEMORY_REQUIREMENT;
}
private static boolean canCalculate(Asset asset) {
return StringUtils.isNotEmpty((String)asset.getMetadataValue("tiff:ImageLength")) && StringUtils.isNotEmpty((String)asset.getMetadataValue("tiff:ImageWidth")) || StringUtils.isNotEmpty((String)asset.getMetadataValue("exif:PixelXDimension")) && StringUtils.isNotEmpty((String)asset.getMetadataValue("exif:PixelYDimension"));
}
public static interface Callback<T> {
public T execute() throws IOException;
}
}