|
59 | 59 | public class VarCache { |
60 | 60 | private static final Map<String, Var<?>> vars = new ConcurrentHashMap<>(); |
61 | 61 | private static final Map<String, Object> fileAttributes = new HashMap<>(); |
62 | | - private static final Map<String, InputStream> fileStreams = new HashMap<>(); |
| 62 | + private static final Map<String, StreamProvider> fileStreams = new HashMap<>(); |
63 | 63 |
|
64 | 64 | /** |
65 | 65 | * The default values set by the client. This is not thread-safe so traversals should be |
@@ -122,42 +122,77 @@ private static Object traverse(Object collection, Object key, boolean autoInsert |
122 | 122 | return null; |
123 | 123 | } |
124 | 124 |
|
125 | | - public static boolean registerFile( |
126 | | - String stringValue, String defaultValue, |
127 | | - InputStream defaultStream, boolean isResource, String resourceHash, int resourceSize) { |
128 | | - if (Constants.isDevelopmentModeEnabled) { |
129 | | - if (!Constants.isNoop()) { |
130 | | - if (defaultStream == null) { |
131 | | - return false; |
132 | | - } |
133 | | - Map<String, Object> variationAttributes = new HashMap<>(); |
134 | | - Map<String, Object> attributes = new HashMap<>(); |
135 | | - if (isResource) { |
136 | | - attributes.put(Constants.Keys.HASH, resourceHash); |
137 | | - attributes.put(Constants.Keys.SIZE, resourceSize); |
138 | | - } else { |
139 | | - if (Constants.hashFilesToDetermineModifications && Util.isSimulator()) { |
140 | | - HashResults result = FileManager.fileMD5HashCreateWithPath(defaultStream); |
141 | | - if (result != null) { |
142 | | - attributes.put(Constants.Keys.HASH, result.hash); |
143 | | - attributes.put(Constants.Keys.SIZE, result.size); |
144 | | - } |
145 | | - } else { |
146 | | - int size = FileManager.getFileSize( |
147 | | - FileManager.fileValue(stringValue, defaultValue, null)); |
148 | | - attributes.put(Constants.Keys.SIZE, size); |
149 | | - } |
150 | | - } |
151 | | - variationAttributes.put("", attributes); |
152 | | - fileAttributes.put(stringValue, variationAttributes); |
153 | | - fileStreams.put(stringValue, defaultStream); |
154 | | - maybeUploadNewFiles(); |
| 125 | + @FunctionalInterface |
| 126 | + public interface StreamProvider { |
| 127 | + InputStream openStream(); |
| 128 | + } |
| 129 | + |
| 130 | + private static boolean isStreamAvailable(StreamProvider stream) { |
| 131 | + if (stream == null) |
| 132 | + return false; |
| 133 | + |
| 134 | + try { |
| 135 | + InputStream is = stream.openStream(); |
| 136 | + if (is != null) { |
| 137 | + is.close(); |
| 138 | + return true; |
155 | 139 | } |
156 | | - return true; |
| 140 | + } catch (Throwable ignore) { |
157 | 141 | } |
158 | 142 | return false; |
159 | 143 | } |
160 | 144 |
|
| 145 | + public static void registerFile( |
| 146 | + String stringValue, StreamProvider defaultStream, String hash, int size) { |
| 147 | + |
| 148 | + if (!isStreamAvailable(defaultStream) |
| 149 | + || !Constants.isDevelopmentModeEnabled |
| 150 | + || Constants.isNoop()) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + Map<String, Object> attributes = new HashMap<>(); |
| 155 | + attributes.put(Constants.Keys.HASH, hash); |
| 156 | + attributes.put(Constants.Keys.SIZE, size); |
| 157 | + |
| 158 | + Map<String, Object> variationAttributes = new HashMap<>(); |
| 159 | + variationAttributes.put("", attributes); |
| 160 | + |
| 161 | + fileStreams.put(stringValue, defaultStream); |
| 162 | + fileAttributes.put(stringValue, variationAttributes); |
| 163 | + maybeUploadNewFiles(); |
| 164 | + } |
| 165 | + |
| 166 | + public static void registerFile( |
| 167 | + String stringValue, String defaultValue, StreamProvider defaultStream) { |
| 168 | + |
| 169 | + if (!isStreamAvailable(defaultStream) |
| 170 | + || !Constants.isDevelopmentModeEnabled |
| 171 | + || Constants.isNoop()) { |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + Map<String, Object> variationAttributes = new HashMap<>(); |
| 176 | + Map<String, Object> attributes = new HashMap<>(); |
| 177 | + |
| 178 | + if (Constants.hashFilesToDetermineModifications && Util.isSimulator()) { |
| 179 | + HashResults result = FileManager.fileMD5HashCreateWithPath(defaultStream.openStream()); |
| 180 | + if (result != null) { |
| 181 | + attributes.put(Constants.Keys.HASH, result.hash); |
| 182 | + attributes.put(Constants.Keys.SIZE, result.size); |
| 183 | + } |
| 184 | + } else { |
| 185 | + int size = FileManager.getFileSize( |
| 186 | + FileManager.fileValue(stringValue, defaultValue, null)); |
| 187 | + attributes.put(Constants.Keys.SIZE, size); |
| 188 | + } |
| 189 | + |
| 190 | + variationAttributes.put("", attributes); |
| 191 | + fileStreams.put(stringValue, defaultStream); |
| 192 | + fileAttributes.put(stringValue, variationAttributes); |
| 193 | + maybeUploadNewFiles(); |
| 194 | + } |
| 195 | + |
161 | 196 | private static void updateValues(String name, String[] nameComponents, Object value, String kind, |
162 | 197 | Map<String, Object> values, Map<String, String> kinds) { |
163 | 198 | Object valuesPtr = values; |
@@ -461,8 +496,8 @@ private static void fileVariableFinish() { |
461 | 496 | !overrideFile.equals(var.defaultValue())) { |
462 | 497 | Map<String, Object> variationAttributes = CollectionUtil.uncheckedCast(fileAttributes.get |
463 | 498 | (overrideFile)); |
464 | | - InputStream stream = fileStreams.get(overrideFile); |
465 | | - if (variationAttributes != null && stream != null) { |
| 499 | + StreamProvider streamProvider = fileStreams.get(overrideFile); |
| 500 | + if (variationAttributes != null && streamProvider != null) { |
466 | 501 | var.setOverrideResId(getResIdFromPath(var.stringValue())); |
467 | 502 | } |
468 | 503 | } |
@@ -692,7 +727,12 @@ static void maybeUploadNewFiles() { |
692 | 727 | Log.e("Unable to upload files.\n" + Log.getStackTraceString(e)); |
693 | 728 | fileData.add(new JSONObject()); |
694 | 729 | } |
695 | | - streams.add(fileStreams.get(name)); |
| 730 | + InputStream is = null; |
| 731 | + StreamProvider streamProvider = fileStreams.get(name); |
| 732 | + if (streamProvider != null) { |
| 733 | + is = streamProvider.openStream(); |
| 734 | + } |
| 735 | + streams.add(is); |
696 | 736 | } |
697 | 737 | } |
698 | 738 |
|
|
0 commit comments