/** * @param source instance of String * @return null if source is empty or contains only whitespaces, source otherwise */ @Override public Object convert(String source) { return blank(source) ? null : source; } }
private String getAllColumns(String[] columns){ return columns == null ? " *" : " " + join(columns, ", "); }
/** * Reads contents of the input stream fully and returns it as String. Sets UTF-8 encoding internally. * * @param in InputStream to read from. * @return contents of the input stream fully as String. * @throws IOException in case of IO error */ public static String read(InputStream in) throws IOException { return read(in, "UTF-8"); }
/** * Reads file into a byte array. * * @param file file to read. * @return content of file. * @throws java.io.IOException */ public static byte[] read(File file) throws IOException { FileInputStream is = new FileInputStream(file); try { return bytes(is); } finally { closeQuietly(is); } }
/** * Saves content of byte array to file. * * @param path path to file - can be absolute or relative to current. * @param content bytes to save. */ public static void saveTo(String path, byte[] content) { InputStream is = null; try { is = new ByteArrayInputStream(content); saveTo(path, is); } finally { closeQuietly(is); } }
/** * Joins the items in array with a delimiter. * * @param array array of items to join. * @param delimiter delimiter to insert between elements of array. * @return string with array elements separated by delimiter. There is no trailing delimiter in the string. */ public static String join(String[] array, String delimiter) { if (empty(array)) { return ""; } StringBuilder sb = new StringBuilder(); join(sb, array, delimiter); return sb.toString(); }
/** * Splits a string into an array using provided delimiter. Empty (but not blank) split chunks are omitted. * The split chunks are trimmed. * * @param input string to split. * @param delimiter delimiter * @return a string split into an array using a provided delimiter */ public static String[] split(String input, char delimiter) { return split(input, String.valueOf(delimiter)); }
private static String exec(String command) { Runtime runtime = Runtime.getRuntime(); try { Process p = runtime.exec(command); String output = read(p.getInputStream()); String error = read(p.getErrorStream()); if (!blank(error)) { throw new ExecException(error); } return output; } catch (ExecException e) { throw e; } catch (Exception e) { throw new ExecException(e); } } }
/** * Reads contents of file fully and returns as string. * * @param fileName file name. * @param charset name of supported charset. * @return contents of entire file. */ public static String readFile(String fileName, String charset) { FileInputStream in = null; try { in = new FileInputStream(fileName); return read(in, charset); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(in); } }
private static synchronized Map<String, Set<String>> getModelMap() { if (modelMap == null) { try { modelMap = new HashMap<>(); Enumeration<URL> urls = Registry.instance().getClass().getClassLoader().getResources("activejdbc_models.properties"); while(urls.hasMoreElements()) { URL url = urls.nextElement(); LogFilter.log(LOGGER, LogLevel.INFO, "Loading models from: {}", url.toExternalForm()); String modelsFile = Util.read(url.openStream()); String[] lines = Util.split(modelsFile, System.getProperty("line.separator")); for(String line : lines) { String[] parts = Util.split(line, ':'); String modelName = parts[0]; String dbName = parts[1]; Set<String> modelNames = modelMap.computeIfAbsent(dbName, k -> new HashSet<>()); if (!modelNames.add(modelName)) { throw new InitException(String.format("Model '{}' already exists for database '{}'", modelName, dbName)); } } } } catch(IOException e) { throw new InitException(e); } } return modelMap; }
/** * Quietly closes the <code>java.sql.PreparedStatement</code> used in a batch execution. The advantage over calling * <code>java.sql.PreparedStatement.close()</code> directly is not having to explicitly handle a checked exception * (<code>java.sql.SQLException</code>). * This method should typically be called in a finally block. So as not to displace any exception (e.g. from a failed * batch execution) that might already be in flight, this method swallows any exception that might arise from * closing the statement. This is generally seen as a worthwhile trade-off, as it much less likely for a close to fail * without a prior failure. * * @param ps <code>java.sql.PreparedStatement</code> with which a batch has been executed. If null, this is a no-op. */ public void closePreparedStatement(PreparedStatement ps) { closeQuietly(ps); }
/** * Joins the items in array with a delimiter, and appends the result to StringBuilder. * * @param sb StringBuilder to append result to * @param array array of items to join. * @param delimiter delimiter to insert between elements of array. */ public static void join(StringBuilder sb, Object[] array, String delimiter) { if (empty(array)) { return; } sb.append(array[0]); for (int i = 1; i < array.length; i++) { sb.append(delimiter); sb.append(array[i]); } }
private String loadTemplate(String templateName){ String slash = templateName.startsWith("/") ? "" : "/"; //for tests, load from location if (templateLocation != null) { return readFile(templateLocation + slash + templateName, "UTF-8"); } //proceed to load from servlet context String fullPath = "/WEB-INF/views" + slash + templateName; // First try to open as plain file (to bypass servlet container resource caches). String realPath = servletContext.getRealPath(fullPath); try { if (realPath != null) { File file = new File(realPath); if (!file.isFile()) { throw new TemplateException(realPath + " is not a file"); } if (file.canRead()) { return readFile(realPath, "UTF-8"); } } } catch (SecurityException ignore) {} try { URL url = servletContext.getResource(fullPath); return Util.read(url.openStream(), "UTF-8"); } catch (Exception e) {throw new TemplateException(e);} }
/** * Reads contents of resource fully into a string. Sets UTF-8 encoding internally. * * @param resourceName resource name. * @return entire contents of resource as string. */ public static String readResource(String resourceName) { return readResource(resourceName, "UTF-8"); }
/** * Constructor to be used in tests, field name and file name are set to File name. * Content type set to "text/plain". * * @param file file to send. * @throws IOException */ public FileItem(File file) throws IOException { super(file.getName(), file.getName(), true, "text/plain", Util.bytes(new FileInputStream(file))); }
/** * Reads contents of file fully and returns as string. * * @param fileName file name. * @return contents of entire file. */ public static String readFile(String fileName) { return readFile(fileName, "UTF-8"); }
/** * Saves content of this item to a file. * * @param path to file * @throws IOException */ public void saveTo(String path) throws IOException { Util.saveTo(path, getInputStream()); } }
/** * Reads contents of resource fully into a byte array. * * @param resourceName resource name. * @return entire contents of resource as byte array. */ public static byte[] readResourceBytes(String resourceName) { InputStream is = Util.class.getResourceAsStream(resourceName); try { return bytes(is); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(is); } }
/** * Joins the items in array with a delimiter. * * @param array array of items to join. * @param delimiter delimiter to insert between elements of array. * @return string with array elements separated by delimiter. There is no trailing delimiter in the string. */ public static String join(String[] array, String delimiter) { if (empty(array)) { return ""; } StringBuilder sb = new StringBuilder(); join(sb, array, delimiter); return sb.toString(); }
/** * Convenience method, does the same as {@link #execute(String...)}, but will * automatically convert a full command string to tokens for convenience. * Here is how to call: * * <pre> * System.out.println(execute("ls -ls").out); * </pre> * * @param command - a single string representing a command and its arguments. * @return instance of {@link Response} with result of execution. */ public static Response execute(String command) { return execute(Util.split(command, " ")); }