List of usage examples for java.io InputStreamReader read
public int read(java.nio.CharBuffer target) throws IOException
From source file:org.apache.roller.weblogger.config.WebloggerRuntimeConfig.java
/** * Get the runtime configuration definitions XML file as a string. * * This is basically a convenience method for accessing this file. * The file itself contains meta-data about what configuration * properties we change at runtime via the UI and how to setup * the display for editing those properties. *//* ww w. java 2s . c o m*/ public static String getRuntimeConfigDefsAsString() { log.debug("Trying to load runtime config defs file"); try { InputStreamReader reader = new InputStreamReader( WebloggerConfig.class.getResourceAsStream(runtime_config)); StringWriter configString = new StringWriter(); char[] buf = new char[8196]; int length = 0; while ((length = reader.read(buf)) > 0) configString.write(buf, 0, length); reader.close(); return configString.toString(); } catch (Exception e) { log.error("Error loading runtime config defs file", e); } return ""; }
From source file:io.fabric8.insight.log.support.LogQuerySupport.java
protected static String loadString(URL url) throws IOException { InputStream is = url.openStream(); if (is == null) { return null; }// w ww.j a v a 2s .c om try { InputStreamReader reader = new InputStreamReader(is); StringWriter writer = new StringWriter(); final char[] buffer = new char[4096]; int n; while (-1 != (n = reader.read(buffer))) { writer.write(buffer, 0, n); } writer.flush(); return writer.toString(); } finally { is.close(); } }
From source file:ch.entwine.weblounge.common.impl.util.TemplateUtils.java
/** * Loads the resource identified by concatenating the package name from * <code>clazz</code> and <code>path</code> from the classpath. * /* w w w. ja v a 2 s . co m*/ * @param path * the path relative to the package name of <code>clazz</code> * @param clazz * the class * @param language * the requested language * @param site * the associated site * @return the resource */ public static String load(String path, Class<?> clazz, Language language, Site site) { if (path == null) throw new IllegalArgumentException("path cannot be null"); if (clazz == null) throw new IllegalArgumentException("clazz cannot be null"); String pkg = null; if (!path.startsWith("/")) pkg = "/" + clazz.getPackage().getName().replace('.', '/'); // Try to find the template in any of the usual languages InputStream is = null; String[] templates = null; if (site != null) templates = LanguageUtils.getLanguageVariants(path, language, site.getDefaultLanguage()); else templates = LanguageUtils.getLanguageVariants(path, language); for (String template : templates) { String pathToTemplate = pkg != null ? UrlUtils.concat(pkg, template) : template; is = clazz.getResourceAsStream(pathToTemplate); if (is != null) { path = template; break; } } // If is is still null, then the template doesn't exist. if (is == null) { logger.error("Template " + path + " not found in any language"); return null; } // Load the template InputStreamReader isr = null; StringBuffer buf = new StringBuffer(); try { logger.debug("Loading " + path); isr = new InputStreamReader(is, Charset.forName("UTF-8")); char[] chars = new char[1024]; int count = 0; while ((count = isr.read(chars)) > 0) { for (int i = 0; i < count; i++) buf.append(chars[i]); } return buf.toString(); } catch (Throwable t) { logger.warn("Error reading " + path + ": " + t.getMessage()); } finally { IOUtils.closeQuietly(isr); IOUtils.closeQuietly(is); } logger.debug("Template " + path + " loaded"); return null; }
From source file:ch.entwine.weblounge.common.impl.util.TemplateUtils.java
/** * Loads the resource from the classpath. The <code>path</code> denotes the * path to the resource to load, e. g.//from www.j a v a2 s .co m * <code>/ch/o2it/weblounge/test.txt</code>. * * @param path * the resource path * @return the resource */ public static String load(String path) { InputStream is = TemplateUtils.class.getResourceAsStream(path); InputStreamReader isr = null; StringBuffer buf = new StringBuffer(); if (is != null) { try { logger.debug("Loading " + path); isr = new InputStreamReader(is, Charset.forName("UTF-8")); char[] chars = new char[1024]; int count = 0; while ((count = isr.read(chars)) > 0) { for (int i = 0; i < count; i++) buf.append(chars[i]); } return buf.toString(); } catch (Throwable t) { logger.warn("Error reading " + path + ": " + t.getMessage()); } finally { IOUtils.closeQuietly(isr); IOUtils.closeQuietly(is); } logger.debug("Editor support (javascript) loaded"); } else { logger.error("Repository item not found: " + path); } return null; }
From source file:ch.entwine.weblounge.common.impl.util.TemplateUtils.java
/** * Loads the resource identified by concatenating the package name from * <code>clazz</code> and <code>path</code> from the classpath. * /*from w w w.j a va 2 s. c o m*/ * @param path * the path relative to the package name of <code>clazz</code> * @param clazz * the class * @return the resource */ public static String load(String path, Class<?> clazz) { if (path == null) throw new IllegalArgumentException("path cannot be null"); if (clazz == null) throw new IllegalArgumentException("clazz cannot be null"); String pkg = "/" + clazz.getPackage().getName().replace('.', '/'); InputStream is = clazz.getResourceAsStream(UrlUtils.concat(pkg, path)); InputStreamReader isr = null; StringBuffer buf = new StringBuffer(); if (is != null) { try { logger.debug("Loading " + path); isr = new InputStreamReader(is, Charset.forName("UTF-8")); char[] chars = new char[1024]; int count = 0; while ((count = isr.read(chars)) > 0) { for (int i = 0; i < count; i++) buf.append(chars[i]); } return buf.toString(); } catch (Throwable t) { logger.warn("Error reading " + path + ": " + t.getMessage()); } finally { IOUtils.closeQuietly(isr); IOUtils.closeQuietly(is); } logger.debug("Editor support (javascript) loaded"); } else { logger.error("Repository item not found: " + path); } return null; }
From source file:org.gcaldaemon.core.notifier.GmailNotifier.java
private static final String[] getActiveUsers() { HashSet users = new HashSet(); try {//from ww w .ja v a 2 s. com String me = System.getProperty("user.name"); if (me != null) { users.add(me); } } catch (Exception ignored) { } try { String os = System.getProperty("os.name", "unknown"); if (commandExecutable && os.toLowerCase().indexOf("windows") != -1) { // Execute script ProcessBuilder builder = new ProcessBuilder(TASK_COMMAND); Process tasklist = builder.start(); // Read command output InputStream in = tasklist.getInputStream(); QuickWriter buffer = new QuickWriter(); BufferedInputStream bis = new BufferedInputStream(in); InputStreamReader isr = new InputStreamReader(bis); char[] chars = new char[1024]; int len; while ((len = isr.read(chars)) != -1) { buffer.write(chars, 0, len); } // Parse output String token, out = buffer.toString(); StringTokenizer lines = new StringTokenizer(out, "\r\n"); StringTokenizer tokens; int i; while (lines.hasMoreTokens()) { tokens = new StringTokenizer(lines.nextToken(), "\"", false); while (tokens.hasMoreTokens()) { token = tokens.nextToken(); i = token.indexOf('\\'); if (i != -1) { token = token.substring(i + 1); if (token.length() != 0) { users.add(token); break; } } } } } } catch (Exception invalidSyntax) { commandExecutable = false; log.debug(invalidSyntax); } String[] array = new String[users.size()]; if (array.length > 0) { users.toArray(array); if (log.isDebugEnabled()) { QuickWriter writer = new QuickWriter(100); for (int i = 0; i < array.length; i++) { writer.write(array[i]); if (i < array.length - 1) { writer.write(", "); } } log.debug("Active users: " + writer.toString()); } } return array; }
From source file:com.cubusmail.mail.text.MessageTextUtil.java
/** * Reads a string from given input stream using direct buffering * /* w w w.j a v a 2 s . c o m*/ * @param inStream * - the input stream * @param charset * - the charset * @return the <code>String</code> read from input stream * @throws IOException * - if an I/O error occurs */ public static String readStream(final InputStream inStream, final String charset) throws IOException { InputStreamReader isr = null; try { int count = 0; final char[] c = new char[BUFSIZE]; isr = new InputStreamReader(inStream, charset); if ((count = isr.read(c)) > 0) { final StringBuilder sb = new StringBuilder(STRBLD_SIZE); do { sb.append(c, 0, count); } while ((count = isr.read(c)) > 0); return sb.toString(); } return ""; } catch (final UnsupportedEncodingException e) { log.error("Unsupported encoding in a message detected and monitored.", e); return ""; } finally { if (null != isr) { try { isr.close(); } catch (final IOException e) { log.error(e.getLocalizedMessage(), e); } } } }
From source file:edu.stanford.epad.epadws.xnat.XNATSessionOperations.java
private static XNATSessionResponse getXNATSessionID(String username, String password) { String xnatSessionURL = buildXNATSessionURL(); HttpClient client = new HttpClient(); PostMethod method = new PostMethod(xnatSessionURL); String authString = buildAuthorizationString(username, password); XNATSessionResponse xnatSessionResponse; int xnatStatusCode; try {/*from w ww . j av a 2 s. co m*/ log.info("Invoking XNAT session service for user " + username + " at " + xnatSessionURL); method.setRequestHeader("Authorization", "Basic " + authString); xnatStatusCode = client.executeMethod(method); log.info("Successfully invoked XNAT session service for user " + username + "; status code = " + xnatStatusCode); } catch (IOException e) { log.warning("Error calling XNAT session service for user " + username, e); xnatStatusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } try { if (xnatStatusCode == HttpServletResponse.SC_OK) { try { StringBuilder sb = new StringBuilder(); InputStreamReader isr = null; try { isr = new InputStreamReader(method.getResponseBodyAsStream()); int read = 0; char[] chars = new char[128]; while ((read = isr.read(chars)) > 0) { sb.append(chars, 0, read); } } finally { IOUtils.closeQuietly(isr); } String jsessionID = sb.toString(); xnatSessionResponse = new XNATSessionResponse(HttpServletResponse.SC_OK, jsessionID); log.debug("Session ID " + jsessionID + " generated for user " + username); // TODO temp } catch (IOException e) { log.warning(LOGIN_EXCEPTION_MESSAGE, e); xnatSessionResponse = new XNATSessionResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, LOGIN_EXCEPTION_MESSAGE + ": " + e.getMessage()); } } else if (xnatStatusCode == HttpServletResponse.SC_UNAUTHORIZED) { log.warning(XNAT_UNAUTHORIZED_MESSAGE); xnatSessionResponse = new XNATSessionResponse(xnatStatusCode, XNAT_UNAUTHORIZED_MESSAGE); } else { log.warning(XNAT_LOGIN_ERROR_MESSAGE + "; XNAT status code = " + xnatStatusCode); xnatSessionResponse = new XNATSessionResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, XNAT_LOGIN_ERROR_MESSAGE + "; XNAT status code = " + xnatStatusCode); } } finally { method.releaseConnection(); } return xnatSessionResponse; }
From source file:org.opencastproject.util.IoSupport.java
/** * Extracts the content from the given input stream. This method is intended to faciliate handling of processes that * have error, input and output streams. * * @param is/*from w w w .j a v a2 s . com*/ * the input stream * @return the stream content */ public static String getOutput(InputStream is) { InputStreamReader bis = new InputStreamReader(is); StringBuffer outputMsg = new StringBuffer(); char[] chars = new char[1024]; try { int len = 0; try { while ((len = bis.read(chars)) > 0) { outputMsg.append(chars, 0, len); } } catch (IOException e) { e.printStackTrace(); } } finally { if (bis != null) try { bis.close(); } catch (IOException e) { } } return outputMsg.toString(); }
From source file:och.util.FileUtil.java
public static String readFile(FileInputStream fis, String charset) throws UnsupportedEncodingException, IOException { InputStreamReader r = null; OutputStreamWriter w = null;/*from w w w . j ava2 s . c om*/ try { r = new InputStreamReader(fis, charset); ByteArrayOutputStream out = new ByteArrayOutputStream(); w = new OutputStreamWriter(out, charset); char[] buff = new char[1024 * 4]; int i; while ((i = r.read(buff)) > 0) { w.write(buff, 0, i); } w.flush(); return out.toString(charset); } finally { if (r != null) try { r.close(); } catch (Exception e) { e.printStackTrace(); } if (w != null) try { w.close(); } catch (Exception e) { e.printStackTrace(); } } }