281 ImageLocation location) throws IOException, BadArgs;
282 }
283
284 private void extract(BasicImageReader reader, String name,
285 ImageLocation location) throws IOException, BadArgs {
286 File directory = new File(options.directory);
287 byte[] bytes = reader.getResource(location);
288 File resource = new File(directory, name);
289 File parent = resource.getParentFile();
290
291 if (parent.exists()) {
292 if (!parent.isDirectory()) {
293 throw TASK_HELPER.newBadArgs("err.cannot.create.dir",
294 parent.getAbsolutePath());
295 }
296 } else if (!parent.mkdirs()) {
297 throw TASK_HELPER.newBadArgs("err.cannot.create.dir",
298 parent.getAbsolutePath());
299 }
300
301 if (!ImageResourcesTree.isTreeInfoResource(name)) {
302 Files.write(resource.toPath(), bytes);
303 }
304 }
305
306 private static final int OFFSET_WIDTH = 12;
307 private static final int SIZE_WIDTH = 10;
308 private static final int COMPRESSEDSIZE_WIDTH = 10;
309
310 private String trimModule(String name) {
311 int offset = name.indexOf('/', 1);
312
313 if (offset != -1 && offset + 1 < name.length()) {
314 return name.substring(offset + 1);
315 }
316
317 return name;
318 }
319
320 private void print(String name, ImageLocation location) {
321 log.print(pad(location.getContentOffset(), OFFSET_WIDTH) + " ");
398 }
399
400 if (resourceAction != null) {
401 String[] entryNames = reader.getEntryNames();
402 String oldModule = "";
403
404 for (String name : entryNames) {
405 boolean match = includePredicates.isEmpty();
406
407 for (Predicate<String> predicate : includePredicates) {
408 if (predicate.test(name)) {
409 match = true;
410 break;
411 }
412 }
413
414 if (!match) {
415 continue;
416 }
417
418 if (!ImageResourcesTree.isTreeInfoResource(name)) {
419 if (moduleAction != null) {
420 int offset = name.indexOf('/', 1);
421
422 String newModule = offset != -1 ?
423 name.substring(1, offset) :
424 "<unknown>";
425
426 if (!oldModule.equals(newModule)) {
427 moduleAction.apply(reader, oldModule, newModule);
428 oldModule = newModule;
429 }
430 }
431
432 ImageLocation location = reader.findLocation(name);
433 resourceAction.apply(reader, name, location);
434 }
435 }
436 }
437 } catch (IOException ioe) {
438 throw TASK_HELPER.newBadArgs("err.invalid.jimage", file, ioe.getMessage());
439 }
440 }
441 }
442
443 private boolean run() throws Exception, BadArgs {
444 switch (options.task) {
445 case EXTRACT:
446 iterate(null, null, this::extract);
447 break;
448 case INFO:
449 iterate(this::info, null, null);
450 break;
451 case LIST:
452 iterate(this::listTitle, this::listModule, this::list);
|
281 ImageLocation location) throws IOException, BadArgs;
282 }
283
284 private void extract(BasicImageReader reader, String name,
285 ImageLocation location) throws IOException, BadArgs {
286 File directory = new File(options.directory);
287 byte[] bytes = reader.getResource(location);
288 File resource = new File(directory, name);
289 File parent = resource.getParentFile();
290
291 if (parent.exists()) {
292 if (!parent.isDirectory()) {
293 throw TASK_HELPER.newBadArgs("err.cannot.create.dir",
294 parent.getAbsolutePath());
295 }
296 } else if (!parent.mkdirs()) {
297 throw TASK_HELPER.newBadArgs("err.cannot.create.dir",
298 parent.getAbsolutePath());
299 }
300
301 if (location.getType() == ImageLocation.LocationType.RESOURCE) {
302 Files.write(resource.toPath(), bytes);
303 }
304 }
305
306 private static final int OFFSET_WIDTH = 12;
307 private static final int SIZE_WIDTH = 10;
308 private static final int COMPRESSEDSIZE_WIDTH = 10;
309
310 private String trimModule(String name) {
311 int offset = name.indexOf('/', 1);
312
313 if (offset != -1 && offset + 1 < name.length()) {
314 return name.substring(offset + 1);
315 }
316
317 return name;
318 }
319
320 private void print(String name, ImageLocation location) {
321 log.print(pad(location.getContentOffset(), OFFSET_WIDTH) + " ");
398 }
399
400 if (resourceAction != null) {
401 String[] entryNames = reader.getEntryNames();
402 String oldModule = "";
403
404 for (String name : entryNames) {
405 boolean match = includePredicates.isEmpty();
406
407 for (Predicate<String> predicate : includePredicates) {
408 if (predicate.test(name)) {
409 match = true;
410 break;
411 }
412 }
413
414 if (!match) {
415 continue;
416 }
417
418 ImageLocation location = reader.findLocation(name);
419 if (location.getType() == ImageLocation.LocationType.RESOURCE) {
420 if (moduleAction != null) {
421 String newModule = location.getModule();
422 if (newModule.isEmpty()) {
423 newModule = "<unknown>";
424 }
425 if (!oldModule.equals(newModule)) {
426 moduleAction.apply(reader, oldModule, newModule);
427 oldModule = newModule;
428 }
429 }
430 resourceAction.apply(reader, name, location);
431 }
432 }
433 }
434 } catch (IOException ioe) {
435 throw TASK_HELPER.newBadArgs("err.invalid.jimage", file, ioe.getMessage());
436 }
437 }
438 }
439
440 private boolean run() throws Exception, BadArgs {
441 switch (options.task) {
442 case EXTRACT:
443 iterate(null, null, this::extract);
444 break;
445 case INFO:
446 iterate(this::info, null, null);
447 break;
448 case LIST:
449 iterate(this::listTitle, this::listModule, this::list);
|