private void restoreState() { try { Map<String, String> props = IOUtil.readProperties(GUI_PROPERTIES_FILE_NAME); exclusionField.setText(props.get("exclusionPattern")); } catch (Exception e) { // no file defined yet, use default settings } }
@Override public void close() { if (source instanceof Closeable) IOUtil.close((Closeable) source); }
@Override public String getText() { try { return IOUtil.getContentOfURI(fileName); } catch (IOException e) { throw new RuntimeException(e); } }
public static String getContentOfURI(String uri, String encoding) throws IOException { Reader reader = getReaderForURI(uri, encoding); return readAndClose(reader); }
public static void copyFile(String srcUri, String targetUri) throws IOException { LOGGER.debug("copying " + srcUri + " --> " + targetUri); InputStream in = null; OutputStream out = null; try { in = getInputStreamForURI(srcUri); out = openOutputStreamForURI(targetUri); IOUtil.transfer(in, out); } finally { close(out); close(in); } }
public CSVLineIterator(String uri, char separator, boolean ignoreEmptyLines, String encoding) throws IOException { this(IOUtil.getReaderForURI(uri, encoding), separator, ignoreEmptyLines); this.stringRep = uri; }
private static void readConfigFileIfExists(String uri) { if (!IOUtil.isURIAvailable(uri)) { logger.info("No custom Comparator setup defined, (" + uri + "), using defaults"); return; } BufferedReader reader = null; try { reader = IOUtil.getReaderForURI(uri); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!StringUtil.isEmpty(line)) createComparator(line); } } catch (IOException e) { throw new ConfigurationError(e); } finally { IOUtil.close(reader); } }
public static Document parse(String uri, boolean namespaceAware, EntityResolver resolver, String schemaUri, ClassLoader classLoader) throws IOException { InputStream stream = null; try { stream = IOUtil.getInputStreamForURI(uri); return parse(stream, namespaceAware, resolver, schemaUri, classLoader, DEFAULT_ERROR_HANDLER); } catch (ConfigurationError e) { throw new ConfigurationError("Error parsing " + uri, e); } finally { IOUtil.close(stream); } }
public static void printFile(String uri) throws IOException { ReaderLineIterator iterator = null; try { iterator = new ReaderLineIterator(IOUtil.getReaderForURI(uri)); while (iterator.hasNext()) System.out.println(iterator.next()); } finally { IOUtil.close(iterator); } }
private void load() { if (IOUtil.isURIAvailable(fileName)) { try { Map<String, String> values = IOUtil.readProperties(fileName); for (Map.Entry<String, String> entry : values.entrySet()) counters.put(entry.getKey(), new AtomicLong(Long.parseLong(entry.getValue()))); } catch (Exception e) { throw new ConfigurationError(e); } } }
public static ImageIcon readImageIcon(String resourceName) { try { InputStream in = getInputStreamForURI(resourceName); if (in == null) throw new FileNotFoundException("Resource not found: " + resourceName); return new ImageIcon(ImageIO.read(in)); } catch (Exception e) { throw new ConfigurationError(e); } }
public static InputStream getInputStreamForUriReference(String localUri, String contextUri, boolean required) throws IOException { LOGGER.debug("getInputStreamForUriReference({}, {})", localUri, contextUri); // do not resolve context for absolute URLs or missing contexts if (StringUtil.isEmpty(contextUri) || getProtocol(localUri) != null) return getInputStreamForURI(localUri, required); // now resolve the relative uri String uri = resolveRelativeUri(localUri, contextUri); if (localUri.startsWith("http://")) { return getInputStreamForURL(new URL(uri)); } if (localUri.startsWith("file:") && !localUri.startsWith("file://")) return getFileOrResourceAsStream(localUri.substring("file:".length()), true); if (!localUri.contains("://")) localUri = "file://" + localUri; if (localUri.startsWith("file://")) return getFileOrResourceAsStream(localUri.substring("file://".length()), true); else throw new ConfigurationError("Can't to handle URL " + localUri); }
private static void init() { File file = new File(SystemInfo.getUserHome() + File.separator + "databene", DATABENE_TEST_PROPERTIES); if (file.exists()) { try { properties = IOUtil.readProperties(file.getAbsolutePath()); } catch (IOException e) { LOGGER.error("Error reading " + file.getAbsolutePath(), e); createDefaultProperties(); } } else { createDefaultProperties(); try { IOUtil.writeProperties(properties, file.getAbsolutePath()); } catch (Exception e) { LOGGER.error("Error writing " + file.getAbsolutePath(), e); } } }
public static void writeTextFile(String filename, String content, String encoding) throws IOException { if (encoding == null) encoding = SystemInfo.getCharset().name(); Writer writer = null; try { writer = new OutputStreamWriter(openOutputStreamForURI(filename), encoding); transfer(new StringReader(content), writer); } finally { close(writer); } }
private static String resolveRelativeFile(String contextPath, String relativePath) { char firstChar = relativePath.charAt(0); boolean isAbsolutePath = firstChar == '/' || firstChar == File.separatorChar; if (isAbsolutePath || isURIAvailable(relativePath)) return relativePath; else return new File(contextPath, relativePath).getPath(); }
public static byte[] getBinaryContentOfUri(String uri) throws IOException { InputStream in = getInputStreamForURI(uri); ByteArrayOutputStream out = new ByteArrayOutputStream(25000); transfer(in, out); return out.toByteArray(); }
private static BufferedReader getFileReader(String filename, String defaultEncoding) throws IOException, UnsupportedEncodingException { if (defaultEncoding == null) defaultEncoding = SystemInfo.getFileEncoding(); InputStream is = getInputStreamForURI(filename); PushbackInputStream in = new PushbackInputStream(is, 4); defaultEncoding = bomEncoding(in, defaultEncoding); return new BufferedReader(new InputStreamReader(in, defaultEncoding)); }
public static void writeProperties(Map<String, ?> properties, String filename, String encoding) throws IOException { PrintWriter stream = null; try { stream = IOUtil.getPrinterForURI(filename, encoding); for (Map.Entry<String, ?> entry : properties.entrySet()) stream.println(entry.getKey() + "=" + ToStringConverter.convert(entry.getValue(), "")); } finally { IOUtil.close(stream); } }
static TreeBuilder loadClasspathResourceIfPresent(String resourceName, TreeBuilder base) throws IOException { InputStream in = IOUtil.getResourceAsStream(resourceName, false); if (in != null) { LOGGER.debug("Loading and merging structure of classpath resource '{}' ", resourceName); try { TreeBuilder newTree = TreeBuilder.loadFromStream(in, resourceName); return overwriteProperties(base, newTree); } finally { IOUtil.close(in); } } else return base; }