@Override public InputStream getInputStream() throws IOException { return resource.in(); }
@Override public InputStream in() { return delegate.in(); }
/** * Returns a resource full contents as a byte array. Usage is suggested only if the resource is * known to be small (e.g. a configuration file). * * @return * @throws IOException */ default byte[] getContents() throws IOException { try (InputStream in = in()) { return org.apache.commons.io.IOUtils.toByteArray(in); } }
/** * Test if the file or directory can be read. * * @see File#canRead() * @param resource Resource indicated * @return true If resource is not UNDEFINED */ public static boolean canRead(Resource resource) { try { InputStream is = resource.in(); is.read(); is.close(); return true; } catch (IOException | IllegalStateException e) { return false; } }
/** * Reads a property file resource. * * <p>This method delegates to {@link #loadUniversal(InputStream)}. */ public static Properties loadPropertyFile(Resource f) throws IOException { try (InputStream in = f.in()) { return loadUniversal(in); } }
/** reads a config file from the specified directly using the specified xstream persister */ <T extends SecurityConfig> T loadConfig(Class<T> config, Resource resource, XStreamPersister xp) throws IOException { InputStream in = resource.in(); try { Object loaded = xp.load(in, SecurityConfig.class).clone(true); return config.cast(loaded); } finally { in.close(); } } /** reads a config file from the specified directly using the specified xstream persister */
/** * Reads a raw style from persistence. * * @param style The configuration for the style. * @return A reader for the style. */ public BufferedReader readStyle(StyleInfo style) throws IOException { Resource styleResource = dataDir().style(style); if (styleResource == null) { throw new IOException("No such resource: " + style.getFilename()); } return new BufferedReader(new InputStreamReader(styleResource.in())); }
static InputStream input(URL url, Resource configDir) throws IOException { // check for a file url if ("file".equalsIgnoreCase(url.getProtocol())) { File f = URLs.urlToFile(url); // check if the file is relative if (!f.isAbsolute()) { // make it relative to the config directory for this password provider Resource res = configDir.get(f.getPath()); if (res.getType() != Type.RESOURCE) { // file must already exist. throw new FileNotFoundException(); } return res.in(); } else { return new FileInputStream(f); } } else { return url.openStream(); } }
/** * Parses the catalog.xml file into a DOM. * * <p>This method *must* be called before any other methods. * * @param file The catalog.xml file. * @throws IOException In event of a parser error. */ public void read(Resource file) throws IOException { Reader reader = XmlCharsetDetector.getCharsetAwareReader(file.in()); try { catalog = ReaderUtils.parse(reader); } finally { reader.close(); } }
private void copyResToDir(Resource r, Resource newDir) throws IOException { Resource newR = newDir.get(r.name()); try (InputStream in = r.in(); OutputStream out = newR.out()) { IOUtils.copy(in, out); } } }
/** reads a config file from the specified directly using the specified xstream persister */ SecurityConfig loadConfigFile(Resource directory, String filename, XStreamPersister xp) throws IOException { InputStream fin = directory.get(filename).in(); try { return xp.load(fin, SecurityConfig.class).clone(true); } finally { fin.close(); } }
/** * Parses the info.xml file into a DOM. * * <p>This method *must* be called before any other methods. * * @param file The info.xml file. * @throws IOException In event of a parser error. */ public void read(Resource file) throws IOException { parentDirectory = file.parent(); Reader reader = XmlCharsetDetector.getCharsetAwareReader(file.in()); try { featureType = ReaderUtils.parse(reader); } finally { reader.close(); } }
private Version getSecurityVersion() throws IOException { Resource security = security(); if (security.getType() == Type.UNDEFINED) { return BASE_VERSION; } Resource properties = security.get(VERSION_PROPERTIES); if (properties.getType() == Type.UNDEFINED) { return BASE_VERSION; } Properties p = new Properties(); try (InputStream is = properties.in()) { p.load(is); } String version = p.getProperty(VERSION); if (version != null) { return new Version(version); } else { return BASE_VERSION; } }
@Theory public void theoryNonDirectoriesPersistData(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); byte[] test = {42, 29, 32, 120, 69, 0, 1}; try (OutputStream ostream = res.out()) { ostream.write(test); } byte[] result = new byte[test.length]; try (InputStream istream = res.in()) { istream.read(result); assertThat(istream.read(), is(-1)); } assertThat(result, equalTo(test)); }
@Theory public void theoryLeavesHaveIstream(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(resource())); try (InputStream result = res.in()) { assertThat(result, notNullValue()); } }
@Theory public void theoryDirectoriesHaveNoIstreams(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); exception.expect(IllegalStateException.class); res.in().close(); }
@Theory public void theoryUndefinedHaveIstreamAndBecomeResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(undefined())); try (InputStream result = res.in()) { assertThat(result, notNullValue()); assertThat(res, is(resource())); } }
@Theory public void theoryRenamedResourcesAreEquivalent(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, resource()); final byte[] expectedContent; try (InputStream in = res.in()) { expectedContent = IOUtils.toByteArray(in); } final Resource target = getUndefined(); assertThat(res.renameTo(target), is(true)); assertThat(target, resource()); final byte[] resultContent; try (InputStream in = target.in()) { resultContent = IOUtils.toByteArray(in); } assertThat(resultContent, equalTo(expectedContent)); }
@Override protected void onSetUp(SystemTestData testData) throws Exception { super.onSetUp(testData); LayerInfo li = getCatalog().getLayerByName(getLayerId(SystemTestData.BUILDINGS)); Resource resource = getDataDirectory().config(li); Document dom; try (InputStream is = resource.in()) { dom = dom(resource.in()); } Element defaultStyle = (Element) dom.getElementsByTagName("defaultStyle").item(0); Element defaultStyleId = (Element) defaultStyle.getElementsByTagName("id").item(0); defaultStyleId.setTextContent("danglingReference"); try (OutputStream os = resource.out()) { print(dom, os); } getGeoServer().reload(); }
public final T load(GeoServer gs, Resource directory) throws Exception { // look for file matching classname Resource file; if (Resources.exists(file = directory.get(getFilename()))) { // xstream it in try (BufferedInputStream in = new BufferedInputStream(file.in())) { XStreamPersister xp = xpf.createXMLPersister(); initXStreamPersister(xp, gs); return initialize(xp.load(in, getServiceClass())); } } else { // create an 'empty' object ServiceInfo service = createServiceFromScratch(gs); return initialize((T) service); } }