/** * 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 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); } }
/** * Reads contents of resource fully into a string. * * @param resourceName resource name. * @param charset name of supported charset * @return entire contents of resource as string. */ public static String readResource(String resourceName, String charset) { InputStream is = Util.class.getResourceAsStream(resourceName); try { return read(is, charset); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(is); } }
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); } } }
private boolean loadStaticMetadata() { try { Enumeration<URL> urls = Registry.instance().getClass().getClassLoader().getResources("activejdbc_metadata.json"); staticMetadataStatus = urls.hasMoreElements() ? STATIC_METADATA_LOADED : STATIC_METADATA_CHECKED; while(urls.hasMoreElements()) { URL url = urls.nextElement(); LogFilter.log(LOGGER, LogLevel.INFO, "Loading metadata from: {}", url.toExternalForm()); metaModels.fromJSON(Util.read(url.openStream())); } return staticMetadataStatus == STATIC_METADATA_LOADED; } catch(IOException e) { throw new InitException(e); } }
/** * Fetches response content from server as String. * * @return response content from server as String. */ public String text() { try { connect(); return responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream()); } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); }finally { dispose(); } }
/** * Fetches response content from server as String. * * @param encoding - name of supported charset to apply when reading data. * * @return response content from server as String. */ public String text(String encoding) { try { connect(); return responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream(), encoding); } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); }finally { dispose(); } }
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; }
/** * 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 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"); }
public String lessc(File lessFile) throws IOException, InterruptedException { logInfo("Executing: " + "lessc " + lessFile.getPath()); String exec = "lessc"; if (System.getProperty("os.name").toLowerCase().contains("windows")) { exec += ".cmd"; } Process process = getRuntime().exec(new String[]{exec, lessFile.getPath()}); String css = read(process.getInputStream(), "UTF-8"); String error = read(process.getErrorStream(), "UTF-8"); if (process.waitFor() != 0) { throw new RuntimeException(error); } return css; }
/** * Reads entire request data as String. Do not use for large data sets to avoid * memory issues, instead use {@link #getRequestInputStream()}. * * @return data sent by client as string. * @throws IOException */ protected String getRequestString() throws IOException { return Util.read(RequestContext.getHttpRequest().getInputStream()); }
/** * Reads contents of resource fully into a string. * * @param resourceName resource name. * @param charset name of supported charset * @return entire contents of resource as string. */ public static String readResource(String resourceName, String charset) { InputStream is = Util.class.getResourceAsStream(resourceName); try { return read(is, charset); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(is); } }
/** * Reads contents of resource fully into a string. * * @param resourceName resource name. * @param charset name of supported charset * @return entire contents of resource as string. */ public static String readResource(String resourceName, String charset) { InputStream is = Util.class.getResourceAsStream(resourceName); try { return read(is, charset); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(is); } }
/** * 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); } }
/** * Converts entire content of this item to String. * * @return content streamed from this field as string. */ public String getStreamAsString(){ try { return Util.read(fileItemStream.openStream()); } catch (Exception e) { throw new ControllerException(e); } }
/** * Fetches response content from server as String. * * @return response content from server as String. */ public String text() { try { connect(); return responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream()); } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); }finally { dispose(); } }
/** * Fetches response content from server as String. * * @param encoding - name of supported charset to apply when reading data. * * @return response content from server as String. */ public String text(String encoding) { try { connect(); return responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream(), encoding); } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); }finally { dispose(); } }
/** * Fetches response content from server as String. * * @return response content from server as String. */ public String text() { try { connect(); String result = responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream()); dispose(); return result; } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); } }
/** * Fetches response content from server as String. * * @param encoding - name of supported charset to apply when reading data. * * @return response content from server as String. */ public String text(String encoding) { try { connect(); String result = responseCode() >= 400 ? read(connection.getErrorStream()) : read(connection.getInputStream(), encoding); dispose(); return result; } catch (IOException e) { throw new HttpException("Failed URL: " + url, e); } }