| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
* Unlike createTempFile(), there is no minimum size for prefix. If * prefix is less than 3 characters, the remaining characters will be * filled with underscores */ static public File createTempFolder(String prefix, String suffix, File directory) throws IOException { int fillChars = 3 - prefix.length(); if (fillChars > 0) { prefix += "_".repeat(fillChars); } if (directory == null) { directory = getProcessingTemp(); } File folder = File.createTempFile(prefix, suffix, directory); // Now delete that file and create a folder in its place if (!folder.delete()) { throw new IOException("Could not remove " + folder + " to create a temporary folder"); } if (!folder.mkdirs()) { throw new IOException("Unable to create " + folder + ", please check permissions for " + folder.getParentFile()); } // And send the folder back to your friends return folder; } static public File getProcessingTemp() throws IOException { String tmpDir = System.getProperty("java.io.tmpdir"); File directory = new File(tmpDir, "processing"); if (!directory.exists()) { if (directory.mkdirs()) { // Set the parent directory writable for multi-user machines. // https://github.com/processing/processing4/issues/666 if (Platform.isLinux()) { Path path = directory.toPath(); Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxrwxrwx")); } else { if (!directory.setReadable(true, false)) { System.err.println("Could not set readable for all: " + directory); } if (!directory.setWritable(true, false)) { System.err.println("Could not set writable for all: " + directory); } if (!directory.setExecutable(true, false)) { System.err.println("Could not set writable for all: " + directory); } } } else { throw new IOException("Could not create temp directory. " + "Check that you have permissions to write to " + tmpDir); } } return directory; } /** * Copy a folder from one place to another. This ignores all dot files and * folders found in the source directory, to avoid copying silly .DS_Store * files and potentially troublesome .svn folders. */ static public void copyDir(File sourceDir, File targetDir) throws IOException { if (sourceDir.equals(targetDir)) { final String urDum = "source and target directories are identical"; throw new IllegalArgumentException(urDum); } if (!targetDir.exists() && !targetDir.mkdirs()) { throw new IOException("Could not create " + targetDir); } String[] filenames = sourceDir.list(); if (filenames != null) { for (String filename : filenames) { // Ignore dot files (.DS_Store), dot folders (.svn) while copying if (filename.charAt(0) != '.') { File source = new File(sourceDir, filename); File target = new File(targetDir, filename); if (source.isDirectory()) { //target.mkdirs(); copyDir(source, target); //noinspection ResultOfMethodCallIgnored target.setLastModified(source.lastModified()); } else { copyFile(source, target); } } } } else { throw new IOException("Could not read " + sourceDir); } } static public void copyDirNative(File sourceDir, File targetDir) throws IOException { Process process; if (Platform.isMacOS() || Platform.isLinux()) { process = Runtime.getRuntime().exec(new String[] { "cp", "-a", sourceDir.getAbsolutePath(), targetDir.getAbsolutePath() }); } else { // TODO implement version that uses XCOPY here on Windows throw new RuntimeException("Not yet implemented on Windows"); } try { int result = process.waitFor(); if (result != 0) { throw new IOException("Error while copying (result " + result + ")"); } } catch (InterruptedException e) { e.printStackTrace(); } } /** * Remove all files in a directory and the directory itself. * Prints error messages with failed filenames. Does not follow symlinks. * Use Platform.deleteFile() instead, which first attempts to use * the Trash or Recycle Bin, out of an abundance of caution. */ static public boolean removeDir(File dir) { return removeDir(dir, true); } /** * Remove all files in a directory and the directory itself. * Optionally, prints error messages with failed filenames. * Does not follow symlinks. */ static public boolean removeDir(File dir, boolean printErrorMessages) { if (!dir.exists()) return true; boolean result = true; if (!Files.isSymbolicLink(dir.toPath())) { File[] files = dir.listFiles(); if (files != null) { for (File child : files) { if (child.isFile()) { boolean deleted = child.delete(); if (!deleted && printErrorMessages) { System.err.println("Could not delete " + child.getAbsolutePath()); } result &= deleted; } else if (child.isDirectory()) { result &= removeDir(child, printErrorMessages); } } } } boolean deleted = dir.delete(); if (!deleted && printErrorMessages) { System.err.println("Could not delete " + dir.getAbsolutePath()); } result &= deleted; return result; } /** * Function to return the length of the file, or entire directory, including * the component files and sub-folders if passed. * @param file The file or folder to calculate */ static public long calcSize(File file) { return file.isFile() ? file.length() : Util.calcFolderSize(file); } /** * Calculate the size of the contents of a folder. * Used to determine whether sketches are empty or not. * Note that the function calls itself recursively. */ static public long calcFolderSize(File folder) { long size = 0; String[] filenames = folder.list(); // null if folder doesn't exist, happens when deleting sketch if (filenames == null) return -1; for (String file : filenames) { if (!file.equals(".") && !file.equals("..") && !file.equals(".DS_Store")) { File fella = new File(folder, file); if (fella.isDirectory()) { long subfolderSize = calcFolderSize(fella); if (subfolderSize == -1) { return -1; } else { size += subfolderSize; } } else { size += fella.length(); } } } return size; } /** * Recursively creates a list of all files within the specified folder, * and returns a list of their relative paths. * Ignores any files/folders prefixed with a dot. * @param relative true return relative paths instead of absolute paths */ static public String[] listFiles(File folder, boolean relative) { return listFiles(folder, relative, null); } static public String[] listFiles(File folder, boolean relative, String extension) { if (extension != null) { if (!extension.startsWith(".")) { extension = "." + extension; } } StringList list = new StringList(); listFilesImpl(folder, relative, extension, list); if (relative) { String[] outgoing = new String[list.size()]; // remove the slash (or backslash) as well int prefixLength = folder.getAbsolutePath().length() + 1; for (int i = 0; i < outgoing.length; i++) { outgoing[i] = list.get(i).substring(prefixLength); } return outgoing; } return list.toArray(); } static void listFilesImpl(File folder, boolean relative, String extension, StringList list) { File[] items = folder.listFiles(); if (items != null) { for (File item : items) { String name = item.getName(); if (name.charAt(0) != '.') { if (item.isDirectory()) { listFilesImpl(item, relative, extension, list); } else { // a file if (extension == null || name.endsWith(extension)) { list.append(item.getAbsolutePath()); } } } } } } /** * @param folder source folder to search * @return an array of .jar and .zip files in that folder */ static public File[] listJarFiles(File folder) { return folder.listFiles((dir, name) -> (!name.startsWith(".") && (name.toLowerCase().endsWith(".jar") || name.toLowerCase().endsWith(".zip")))); } ///////////////////////////////////////////////////////////////////////////// /** * Given a folder, return a list of absolute paths to all jar or zip files * inside that folder, separated by pathSeparatorChar. *
* This will prepend a colon (or whatever the path separator is) * so that it can be directly appended to another path string. *
* As of 0136, this will no longer add the root folder as well. *
* This function doesn't bother checking to see if there are any .class * files in the folder or within a subfolder. */ static public String contentsToClassPath(File folder) { if (folder == null) return ""; StringBuilder sb = new StringBuilder(); String sep = System.getProperty("path.separator"); try { String path = folder.getCanonicalPath(); // When getting the name of this folder, make sure it has a slash // after it, so that the names of sub-items can be added. if (!path.endsWith(File.separator)) { path += File.separator; } String[] list = folder.list(); if (list != null) { for (String item : list) { // Skip . and ._ files. Prior to 0125p3, .jar files that had // OS X AppleDouble files associated would cause trouble. if (!item.startsWith(".")) { if (item.toLowerCase().endsWith(".jar") || item.toLowerCase().endsWith(".zip")) { sb.append(sep); sb.append(path); sb.append(item); } } } } } catch (IOException e) { e.printStackTrace(); // this would be odd } return sb.toString(); } /** * A classpath, separated by the path separator, will contain * a series of .jar/.zip files or directories containing .class * files, or containing subdirectories that have .class files. * * @param path the input classpath * @return array of possible package names */ static public StringList packageListFromClassPath(String path) { // Map map = new HashMap(); StringList list = new StringList(); String[] pieces = PApplet.split(path, File.pathSeparatorChar); for (String piece : pieces) { //System.out.println("checking piece '" + pieces[i] + "'"); if (!piece.isEmpty()) { if (piece.toLowerCase().endsWith(".jar") || piece.toLowerCase().endsWith(".zip")) { //System.out.println("checking " + pieces[i]); packageListFromZip(piece, list); } else { // it's another type of file or directory File dir = new File(piece); if (dir.exists() && dir.isDirectory()) { packageListFromFolder(dir, null, list); //importCount = magicImportsRecursive(dir, null, // map); //imports, importCount); } } } } // int mapCount = map.size(); // String output[] = new String[mapCount]; // int index = 0; // Set set = map.keySet(); // for (String s : set) { // output[index++] = s.replace('/', '.'); // } // return output; StringList outgoing = new StringList(list.size()); for (String item : list) { outgoing.append(item.replace('/', '.')); } return outgoing; } static private void packageListFromZip(String filename, StringList list) { try (ZipFile file = new ZipFile(filename);) { Enumeration entries = file.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (!entry.isDirectory()) { String name = entry.getName(); // Avoid META-INF because some jokers put .class files in there // https://github.com/processing/processing/issues/5778 if (name.endsWith(".class") && !name.contains("META-INF/")) { int slash = name.lastIndexOf('/'); if (slash != -1) { String packageName = name.substring(0, slash); list.appendUnique(packageName); } } } } } catch (IOException e) { System.err.println("Ignoring " + filename + " (" + e.getMessage() + ")"); //e.printStackTrace(); } } /** * Make list of package names by traversing a directory hierarchy. * Each time a class is found in a folder, add its containing set * of folders to the package list. If another folder is found, * walk down into that folder and continue. */ static private void packageListFromFolder(File dir, String sofar, StringList list) { boolean foundClass = false; String[] filenames = dir.list(); if (filenames != null) { for (String filename : filenames) { if (filename.equals(".") || filename.equals("..")) continue; File sub = new File(dir, filename); if (sub.isDirectory()) { String nowfar = (sofar == null) ? filename : (sofar + "." + filename); packageListFromFolder(sub, nowfar, list); } else if (!foundClass) { // if no classes found in this folder yet if (filename.endsWith(".class")) { list.appendUnique(sofar); foundClass = true; } } } } else { System.err.println("Could not read " + dir); } } /** * Extract the contents of a .zip archive into a folder. * Ignores (does not extract) any __MACOSX files from macOS archives. */ static public void unzip(File zipFile, File dest) throws IOException { try (ZipInputStream zis = new ZipInputStream( new BufferedInputStream( new CheckedInputStream( new FileInputStream(zipFile), new Adler32())))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { final String name = entry.getName(); if (!name.startsWith(("__MACOSX"))) { File currentFile = new File(dest, name); if (entry.isDirectory()) { currentFile.mkdirs(); } else { File parentDir = currentFile.getParentFile(); // Sometimes the directory entries aren't already created if (!parentDir.exists()) { parentDir.mkdirs(); } currentFile.createNewFile(); unzipEntry(zis, currentFile); } } } } } static protected void unzipEntry(ZipInputStream zin, File f) throws IOException { try (FileOutputStream out = new FileOutputStream(f)) { byte[] b = new byte[512]; int len; while ((len = zin.read(b)) != -1) { out.write(b, 0, len); } out.flush(); } } static public byte[] gzipEncode(byte[] what) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (GZIPOutputStream output = new GZIPOutputStream(baos);) { PApplet.saveStream(output, new ByteArrayInputStream(what)); } return baos.toByteArray(); } static public boolean containsNonASCII(String what) { for (char c : what.toCharArray()) { if (c < 32 || c > 127) return true; } return false; } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . static public String sanitizeHtmlTags(String str) { return str.replaceAll("", ">"); } /** * This has a [link](http://example.com/) in [it](http://example.org/). *
* Becomes... *
* This has a link in it. */ static public String markDownLinksToHtml(String str) { Pattern p = Pattern.compile("\\[(.*?)]\\((.*?)\\)"); Matcher m = p.matcher(str); StringBuilder sb = new StringBuilder(); int start = 0; while (m.find(start)) { sb.append(str, start, m.start()); String text = m.group(1); String url = m.group(2); sb.append(""); sb.append(text); sb.append(""); start = m.end(); } sb.append(str.substring(start)); return sb.toString(); } static public String removeMarkDownLinks(String str) { StringBuilder name = new StringBuilder(); if (str != null) { int parentheses = 0; for (char c : str.toCharArray()) { if (c == '[' || c == ']') { // pass } else if (c == '(') { parentheses++; } else if (c == ')') { parentheses--; } else if (parentheses == 0) { name.append(c); } } } return name.toString(); } }
| Back | FazBrowse Home | New Git URL |