/** * Read the input stream into a string * * @param in * the stream * @param charset * the charset to be used * @return the string */ public static String readToString(InputStream in, Charset charset) { return readToString(in, 1024, charset); }
private Component handle(URL resource) { try (InputStream in = resource.openStream()) { String data = Util.readToString(in, Charset.forName("UTF-8")); //$NON-NLS-1$ JAXBContext jaxbContext; if (data.contains("http://www.osgi.org/xmlns/scr/v1.1.0")) { //$NON-NLS-1$ jaxbContext = JAXBContext.newInstance(Component11.class); } else { jaxbContext = JAXBContext.newInstance(Component12.class); } Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); return (Component) jaxbUnmarshaller.unmarshal(new StringReader(data)); } catch (Throwable e) { logError("Unable to load component '"+resource+"'", e); //$NON-NLS-1$//$NON-NLS-2$ } return null; } }
@Override public @Nullable InputStream createDataStream(URL url) { try { URL realURL = new URL(url.getPath()); String data; try (InputStream stream = realURL.openStream()) { data = StrSubstitutor.replace(Util.readToString(stream, Charset.forName("UTF-8")), //$NON-NLS-1$ map(url.getQuery()),"_(",")"); //$NON-NLS-1$//$NON-NLS-2$ } return new ByteArrayInputStream(data.getBytes()); } catch (IOException e) { LoggerCreator.createLogger(TplURLDynamicDataStreamHandler.class).error("Failed to load real data", e); //$NON-NLS-1$ } return null; }