protected void findLocations(Set<String> locations, ClassLoader classLoader) throws IOException { Enumeration<URL> resources = classLoader.getResources(LOCATION_PROPERTIES); while (resources.hasMoreElements()) { URL url = resources.nextElement(); BufferedReader reader = IOHelper.buffered(new InputStreamReader(url.openStream(), UTF8)); try { while (true) { String line = reader.readLine(); if (line == null) { break; } line = line.trim(); if (line.startsWith("#") || line.length() == 0) { continue; } locations.add(line); } } finally { IOHelper.close(reader, null, LOG); } } }
@Override public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { try (final InputStreamReader isr = new InputStreamReader(stream, IOHelper.getCharsetName(exchange)); final BufferedReader reader = IOHelper.buffered(isr)) { if (unmarshalGenericType == null) { return gson.fromJson(reader, unmarshalType); } else { return gson.fromJson(reader, unmarshalGenericType); } } }
@Override public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception { try (final OutputStreamWriter osw = new OutputStreamWriter(stream, IOHelper.getCharsetName(exchange)); final BufferedWriter writer = IOHelper.buffered(osw)) { gson.toJson(graph, writer); } if (contentTypeHeader) { if (exchange.hasOut()) { exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json"); } else { exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json"); } } }
private static String send4LetterWord(String hp, String cmd) throws IOException { String split[] = hp.split(":"); String host = split[0]; int port; try { port = Integer.parseInt(split[1]); } catch (RuntimeException e) { throw new RuntimeException("Problem parsing " + hp + e.toString()); } Socket sock = new Socket(host, port); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write(cmd.getBytes()); outstream.flush(); reader = IOHelper.buffered(new InputStreamReader(sock.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } return sb.toString(); } finally { sock.close(); if (reader != null) { reader.close(); } } }
private static String send4LetterWord(String hp, String cmd) throws IOException { String split[] = hp.split(":"); String host = split[0]; int port; try { port = Integer.parseInt(split[1]); } catch (RuntimeException e) { throw new RuntimeException("Problem parsing " + hp + e.toString()); } Socket sock = new Socket(host, port); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write(cmd.getBytes()); outstream.flush(); reader = IOHelper.buffered(new InputStreamReader(sock.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } return sb.toString(); } finally { sock.close(); if (reader != null) { reader.close(); } } }
private void writeToStream(OutputStream outputStream, Exchange exchange) throws IOException, CamelExchangeException { Object body = exchange.getIn().getBody(); if (body == null) { log.debug("Body is null, cannot write it to the stream."); return; } // if not a string then try as byte array first if (!(body instanceof String)) { byte[] bytes = exchange.getIn().getBody(byte[].class); if (bytes != null) { LOG.debug("Writing as byte[]: {} to {}", bytes, outputStream); outputStream.write(bytes); return; } } // okay now fallback to mandatory converterable to string String s = exchange.getIn().getMandatoryBody(String.class); Charset charset = endpoint.getCharset(); Writer writer = new OutputStreamWriter(outputStream, charset); BufferedWriter bw = IOHelper.buffered(writer); if (LOG.isDebugEnabled()) { LOG.debug("Writing as text: {} to {} using encoding: {}", body, outputStream, charset); } bw.write(s); bw.write(System.lineSeparator()); bw.flush(); }
private BufferedReader initializeStream() throws Exception { // close old stream, before obtaining a new stream IOHelper.close(inputStreamToClose); if ("in".equals(uri)) { inputStream = System.in; inputStreamToClose = null; } else if ("file".equals(uri)) { inputStream = resolveStreamFromFile(); inputStreamToClose = inputStream; } else if ("url".equals(uri)) { inputStream = resolveStreamFromUrl(); inputStreamToClose = inputStream; } if (inputStream != null) { Charset charset = endpoint.getCharset(); return IOHelper.buffered(new InputStreamReader(inputStream, charset)); } else { return null; } }
public static InputStream getInputStream(String path) { InputStream is = null; if (isClasspathResource(path)) { String classpathResourcePath = ResourceUtils.getClasspathResourcePath(path); is = ResourceUtils.class.getResourceAsStream(classpathResourcePath); if (is == null) { throw new RuntimeIOException("Certificate stream is null: '" + classpathResourcePath + "'"); } } else { try { is = IOHelper.buffered(new FileInputStream(path)); } catch (FileNotFoundException e) { throw new RuntimeIOException(e); } } return is; }
@Override public Class<?> findClass(String key, String propertyPrefix, Class<?> checkClass) throws ClassNotFoundException, IOException { final String prefix = propertyPrefix != null ? propertyPrefix : ""; final String classKey = propertyPrefix + key; return addToClassMap(classKey, () -> { BundleEntry entry = getResource(key, checkClass); if (entry != null) { URL url = entry.url; InputStream in = url.openStream(); // lets load the file BufferedInputStream reader = null; try { reader = IOHelper.buffered(in); Properties properties = new Properties(); properties.load(reader); String className = properties.getProperty(prefix + "class"); if (className == null) { throw new IOException("Expected property is missing: " + prefix + "class"); } return entry.bundle.loadClass(className); } finally { IOHelper.close(reader, key, null); IOHelper.close(in, key, null); } } else { throw new NoFactoryAvailableException(classKey); } }); }