public static InputStream getStreamForUri(URI uri, ClassLoader loader) throws IOException { String protocol = uri.getScheme(); if (protocol.equals("classpath")) { String path = uri.getSchemeSpecificPart(); return getStreamForClasspathFile(path, loader); } URL url = uri.toURL(); return url.openStream(); }
public static InputStream getStreamForClasspathFile(String path, ClassLoader loader) throws IOException { // Get an array of class loaders to try loading the providers files from. List<ClassLoader> classLoaders = getClassLoaders(); if (loader != null) { classLoaders.add(0, loader); } for (ClassLoader classLoader : classLoaders) { InputStream is = classLoader.getResourceAsStream(path); if (is != null) { return is; } } throw new IOException("Unable to get '" + path + "' from classpath. Tried ClassLoaders:" + classLoaders); }
public static boolean addLines(String uriString, Set<String> set) throws MalformedURLException, IOException { URI uri = URI.create(uriString); InputStream is = getStreamForUri(uri, null); InputStreamReader sr = new InputStreamReader(is, StringUtils.UTF8); BufferedReader br = new BufferedReader(sr); try { String line; while ((line = br.readLine()) != null) { set.add(line); } } finally { br.close(); } return true; }
@Override protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException { File file = getTrustPath(owner, fingerprint); if (trust == null || trust == Trust.undecided) { FileUtils.maybeDeleteFileOrThrow(file); } FileUtils.maybeCreateFileWithParentDirectories(file); BufferedWriter writer = null; try { OutputStream outputStream = FileUtils.prepareFileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8); writer = new BufferedWriter(osw); writer.write(trust.toString()); } finally { CloseableUtil.maybeClose(writer, LOGGER); } }
@Override public String getRosterVersion() { String s = FileUtils.readFile(getVersionFile()); if (s == null) { return null; } String[] lines = s.split("\n", 2); if (lines.length < 2) { return null; } return lines[1]; }
private boolean setRosterVersion(String version) { return FileUtils.writeFile(getVersionFile(), STORE_ID + "\n" + version); }
public static boolean writeFile(File file, CharSequence content) { try { writeFileOrThrow(file, content); return true; } catch (IOException e) { LOGGER.log(Level.WARNING, "writeFile", e); return false; } }
public static String readFile(File file) { try { return readFileOrThrow(file); } catch (FileNotFoundException e) { LOGGER.log(Level.FINE, "readFile", e); } catch (IOException e) { LOGGER.log(Level.WARNING, "readFile", e); } return null; }
@Override public void configure(Binder binder) { try { ProviderManager.addLoader(new ProviderFileLoader(FileUtils.getStreamForUrl( "classpath:net/whistlingfish/harmony/smack-providers.xml", null))); } catch (IOException e) { throw new RuntimeException("Failed to initialize smack providers", e); } } }
static void writeFingerprintsAndDates(Map<OpenPgpV4Fingerprint, Date> data, File destination) throws IOException { if (data == null || data.isEmpty()) { FileUtils.maybeDeleteFileOrThrow(destination); return; } FileUtils.maybeCreateFileWithParentDirectories(destination); BufferedWriter writer = null; try { OutputStream outputStream = FileUtils.prepareFileOutputStream(destination); OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8); writer = new BufferedWriter(osw); for (OpenPgpV4Fingerprint fingerprint : data.keySet()) { Date date = data.get(fingerprint); String line = fingerprint.toString() + " " + (date != null ? XmppDateTime.formatXEP0082Date(date) : XmppDateTime.formatXEP0082Date(new Date())); writer.write(line); writer.newLine(); } } finally { CloseableUtil.maybeClose(writer, LOGGER); } }
/** * Opens a roster store. * @param baseDir * The directory containing the roster store. * @return A {@link DirectoryRosterStore} instance if successful, * <code>null</code> else. */ public static DirectoryRosterStore open(final File baseDir) { DirectoryRosterStore store = new DirectoryRosterStore(baseDir); String s = FileUtils.readFile(store.getVersionFile()); if (s != null && s.startsWith(STORE_ID + "\n")) { return store; } else { return null; } }
private boolean addEntryRaw (Item item) { return FileUtils.writeFile(getBareJidFile(item.getJid()), item.toXML(null)); }
public static boolean writeFile(File file, CharSequence content) { try { writeFileOrThrow(file, content); return true; } catch (IOException e) { LOGGER.log(Level.WARNING, "writeFile", e); return false; } } }
public static String readFile(File file) { try { return readFileOrThrow(file); } catch (FileNotFoundException e) { LOGGER.log(Level.FINE, "readFile", e); } catch (IOException e) { LOGGER.log(Level.WARNING, "readFile", e); } return null; }
@Test public void addClasspathFileLoaderProvider() throws Exception { ProviderManager.addLoader(new ProviderFileLoader(FileUtils.getStreamForClasspathFile("test.providers", null))); Assert.assertNotNull(ProviderManager.getIQProvider("provider", "test:file_provider")); }
@Override public String getRosterVersion() { String s = FileUtils.readFile(getVersionFile()); if (s == null) { return null; } String[] lines = s.split("\n", 2); if (lines.length < 2) { return null; } return lines[1]; }
private boolean setRosterVersion(String version) { return FileUtils.writeFile(getVersionFile(), STORE_ID + "\n" + version); }
try { final URI providerUri = URI.create(providerUriString); is = FileUtils.getStreamForUri(providerUri, classLoader); try { final URI configUri = URI.create(configUriString); is = FileUtils.getStreamForUri(configUri, classLoader); SmackInitialization.processConfigFile(is, exceptions, classLoader);
public static InputStream getStreamForClasspathFile(String path, ClassLoader loader) throws IOException { // Get an array of class loaders to try loading the providers files from. List<ClassLoader> classLoaders = getClassLoaders(); if (loader != null) { classLoaders.add(0, loader); } for (ClassLoader classLoader : classLoaders) { InputStream is = classLoader.getResourceAsStream(path); if (is != null) { return is; } } throw new IOException("Unable to get '" + path + "' from classpath. Tried ClassLoaders:" + classLoaders); }
public static InputStream getStreamForUri(URI uri, ClassLoader loader) throws IOException { String protocol = uri.getScheme(); if (protocol.equals("classpath")) { String path = uri.getSchemeSpecificPart(); return getStreamForClasspathFile(path, loader); } URL url = uri.toURL(); return url.openStream(); }