|
27 | 27 | import java.io.*;
|
28 | 28 | import java.lang.reflect.*;
|
29 | 29 | import java.net.*;
|
| 30 | +import java.nio.charset.StandardCharsets; |
30 | 31 | import java.text.NumberFormat;
|
31 | 32 | import java.util.*;
|
32 | 33 | import java.util.regex.*;
|
@@ -4062,31 +4063,6 @@ public void run() {
|
4062 | 4063 |
|
4063 | 4064 |
|
4064 | 4065 |
|
4065 |
| - ////////////////////////////////////////////////////////////// |
4066 |
| - |
4067 |
| - // EXTENSIONS |
4068 |
| - |
4069 |
| - |
4070 |
| - /** |
4071 |
| - * Get the compression-free extension for this filename. |
4072 |
| - * @param filename The filename to check |
4073 |
| - * @return an extension, skipping past .gz if it's present |
4074 |
| - */ |
4075 |
| - static public String checkExtension(String filename) { |
4076 |
| - // Don't consider the .gz as part of the name, createInput() |
4077 |
| - // and createOuput() will take care of fixing that up. |
4078 |
| - if (filename.toLowerCase().endsWith(".gz")) { |
4079 |
| - filename = filename.substring(0, filename.length() - 3); |
4080 |
| - } |
4081 |
| - int dotIndex = filename.lastIndexOf('.'); |
4082 |
| - if (dotIndex != -1) { |
4083 |
| - return filename.substring(dotIndex + 1).toLowerCase(); |
4084 |
| - } |
4085 |
| - return null; |
4086 |
| - } |
4087 |
| - |
4088 |
| - |
4089 |
| - |
4090 | 4066 | //////////////////////////////////////////////////////////////
|
4091 | 4067 |
|
4092 | 4068 | // DATA I/O
|
@@ -4507,6 +4483,156 @@ public PFont createFont(String name, float size,
|
4507 | 4483 | // }
|
4508 | 4484 |
|
4509 | 4485 |
|
| 4486 | + ////////////////////////////////////////////////////////////// |
| 4487 | + |
| 4488 | + // LISTING DIRECTORIES |
| 4489 | + |
| 4490 | + |
| 4491 | + public String[] listPaths(String path, String... options) { |
| 4492 | + File[] list = listFiles(path, options); |
| 4493 | + |
| 4494 | + int offset = 0; |
| 4495 | + for (String opt : options) { |
| 4496 | + if (opt.equals("relative")) { |
| 4497 | + if (!path.endsWith(File.pathSeparator)) { |
| 4498 | + path += File.pathSeparator; |
| 4499 | + } |
| 4500 | + offset = path.length(); |
| 4501 | + break; |
| 4502 | + } |
| 4503 | + } |
| 4504 | + String[] outgoing = new String[list.length]; |
| 4505 | + for (int i = 0; i < list.length; i++) { |
| 4506 | + // as of Java 1.8, substring(0) returns the original object |
| 4507 | + outgoing[i] = list[i].getAbsolutePath().substring(offset); |
| 4508 | + } |
| 4509 | + return outgoing; |
| 4510 | + } |
| 4511 | + |
| 4512 | + |
| 4513 | + public File[] listFiles(String path, String... options) { |
| 4514 | + File file = new File(path); |
| 4515 | + // if not an absolute path, make it relative to the sketch folder |
| 4516 | + if (!file.isAbsolute()) { |
| 4517 | + file = sketchFile(path); |
| 4518 | + } |
| 4519 | + return listFiles(file, options); |
| 4520 | + } |
| 4521 | + |
| 4522 | + |
| 4523 | + // "relative" -> no effect with the Files version, but important for listPaths |
| 4524 | + // "recursive" |
| 4525 | + // "extension=js" or "extensions=js|csv|txt" (no dot) |
| 4526 | + // "directories" -> only directories |
| 4527 | + // "files" -> only files |
| 4528 | + // "hidden" -> include hidden files (prefixed with .) disabled by default |
| 4529 | + static public File[] listFiles(File base, String... options) { |
| 4530 | + boolean recursive = false; |
| 4531 | + String[] extensions = null; |
| 4532 | + boolean directories = true; |
| 4533 | + boolean files = true; |
| 4534 | + boolean hidden = false; |
| 4535 | + |
| 4536 | + for (String opt : options) { |
| 4537 | + if (opt.equals("recursive")) { |
| 4538 | + recursive = true; |
| 4539 | + } else if (opt.startsWith("extension=")) { |
| 4540 | + extensions = new String[] { opt.substring(10) }; |
| 4541 | + } else if (opt.startsWith("extensions=")) { |
| 4542 | + extensions = split(opt.substring(10), ','); |
| 4543 | + } else if (opt.equals("files")) { |
| 4544 | + directories = false; |
| 4545 | + } else if (opt.equals("directories")) { |
| 4546 | + files = false; |
| 4547 | + } else if (opt.equals("hidden")) { |
| 4548 | + hidden = true; |
| 4549 | + } else if (opt.equals("relative")) { |
| 4550 | + // ignored |
| 4551 | + } else { |
| 4552 | + throw new RuntimeException(opt + " is not a listFiles() option"); |
| 4553 | + } |
| 4554 | + } |
| 4555 | + |
| 4556 | + if (extensions != null) { |
| 4557 | + for (int i = 0; i < extensions.length; i++) { |
| 4558 | + extensions[i] = "." + extensions[i]; |
| 4559 | + } |
| 4560 | + } |
| 4561 | + |
| 4562 | + if (!files && !directories) { |
| 4563 | + // just make "only files" and "only directories" mean... both |
| 4564 | + files = true; |
| 4565 | + directories = true; |
| 4566 | + } |
| 4567 | + |
| 4568 | + if (!base.canRead()) { |
| 4569 | + return null; |
| 4570 | + } |
| 4571 | + |
| 4572 | + List<File> outgoing = new ArrayList<>(); |
| 4573 | + listFilesImpl(base, recursive, extensions, hidden, directories, files, outgoing); |
| 4574 | + return outgoing.toArray(new File[0]); |
| 4575 | + } |
| 4576 | + |
| 4577 | + |
| 4578 | + static void listFilesImpl(File folder, boolean recursive, |
| 4579 | + String[] extensions, boolean hidden, |
| 4580 | + boolean directories, boolean files, |
| 4581 | + List<File> list) { |
| 4582 | + File[] items = folder.listFiles(); |
| 4583 | + if (items != null) { |
| 4584 | + for (File item : items) { |
| 4585 | + String name = item.getName(); |
| 4586 | + if (!hidden && name.charAt(0) == '.') { |
| 4587 | + continue; |
| 4588 | + } |
| 4589 | + if (item.isDirectory()) { |
| 4590 | + if (recursive) { |
| 4591 | + listFilesImpl(item, recursive, extensions, hidden, directories, files, list); |
| 4592 | + } |
| 4593 | + if (directories) { |
| 4594 | + list.add(item); |
| 4595 | + } |
| 4596 | + } else if (files) { |
| 4597 | + if (extensions == null) { |
| 4598 | + list.add(item); |
| 4599 | + } else { |
| 4600 | + for (String ext : extensions) { |
| 4601 | + if (item.getName().toLowerCase().endsWith(ext)) { |
| 4602 | + list.add(item); |
| 4603 | + } |
| 4604 | + } |
| 4605 | + } |
| 4606 | + } |
| 4607 | + } |
| 4608 | + } |
| 4609 | + } |
| 4610 | + |
| 4611 | + |
| 4612 | + |
| 4613 | + ////////////////////////////////////////////////////////////// |
| 4614 | + |
| 4615 | + // EXTENSIONS |
| 4616 | + |
| 4617 | + |
| 4618 | + /** |
| 4619 | + * Get the compression-free extension for this filename. |
| 4620 | + * @param filename The filename to check |
| 4621 | + * @return an extension, skipping past .gz if it's present |
| 4622 | + */ |
| 4623 | + static public String checkExtension(String filename) { |
| 4624 | + // Don't consider the .gz as part of the name, createInput() |
| 4625 | + // and createOuput() will take care of fixing that up. |
| 4626 | + if (filename.toLowerCase().endsWith(".gz")) { |
| 4627 | + filename = filename.substring(0, filename.length() - 3); |
| 4628 | + } |
| 4629 | + int dotIndex = filename.lastIndexOf('.'); |
| 4630 | + if (dotIndex != -1) { |
| 4631 | + return filename.substring(dotIndex + 1).toLowerCase(); |
| 4632 | + } |
| 4633 | + return null; |
| 4634 | + } |
| 4635 | + |
4510 | 4636 |
|
4511 | 4637 | //////////////////////////////////////////////////////////////
|
4512 | 4638 |
|
@@ -4566,10 +4692,8 @@ static public BufferedReader createReader(File file) {
|
4566 | 4692 | * following lines any more I'm gonna send Sun my medical bills.
|
4567 | 4693 | */
|
4568 | 4694 | static public BufferedReader createReader(InputStream input) {
|
4569 |
| - InputStreamReader isr = null; |
4570 |
| - try { |
4571 |
| - isr = new InputStreamReader(input, "UTF-8"); |
4572 |
| - } catch (UnsupportedEncodingException e) { } // not gonna happen |
| 4695 | + InputStreamReader isr = |
| 4696 | + new InputStreamReader(input, StandardCharsets.UTF_8); |
4573 | 4697 | return new BufferedReader(isr);
|
4574 | 4698 | }
|
4575 | 4699 |
|
@@ -4612,12 +4736,10 @@ static public PrintWriter createWriter(File file) {
|
4612 | 4736 | * It's the JavaSoft API engineers who need to explain themselves.
|
4613 | 4737 | */
|
4614 | 4738 | static public PrintWriter createWriter(OutputStream output) {
|
4615 |
| - try { |
4616 |
| - BufferedOutputStream bos = new BufferedOutputStream(output, 8192); |
4617 |
| - OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8"); |
4618 |
| - return new PrintWriter(osw); |
4619 |
| - } catch (UnsupportedEncodingException e) { } // not gonna happen |
4620 |
| - return null; |
| 4739 | + BufferedOutputStream bos = new BufferedOutputStream(output, 8192); |
| 4740 | + OutputStreamWriter osw = |
| 4741 | + new OutputStreamWriter(bos, StandardCharsets.UTF_8); |
| 4742 | + return new PrintWriter(osw); |
4621 | 4743 | }
|
4622 | 4744 |
|
4623 | 4745 |
|
|
0 commit comments