@Override public OutputStream out() { return delegate.out(); }
/** * Write the contents of a stream into a resource * * @param data data to write * @param destination resource to write to * @throws IOException If data could not be copied to destination */ public static void copy(InputStream data, Resource destination) throws IOException { try (OutputStream out = destination.out()) { IOUtils.copy(data, out); } }
/** * Writes a resource contents as a byte array. Usage is suggested only if the resource is known * to be small (e.g. a configuration file). * * @param byteArray * @throws IOException */ default void setContents(byte[] byteArray) throws IOException { try (OutputStream os = out()) { IOUtils.write(byteArray, os); } } }
/** * Safe write on styleFile the passed inputStream * * @param in the new stream to write to styleFile * @param styleFile file to update * @throws IOException */ public static void writeStyle(final InputStream in, final Resource styleFile) throws IOException { try (BufferedOutputStream out = new BufferedOutputStream(styleFile.out())) { IOUtils.copy(in, out); out.flush(); } }
/** Helper method which uses xstream to persist an object as xml on disk. */ void persist(XStreamPersister xp, Object obj, Resource f) throws Exception { BufferedOutputStream out = new BufferedOutputStream(f.out()); xp.save(obj, out); out.flush(); out.close(); }
@Override protected void loadProperties(Properties props) throws IOException { try { super.loadProperties(props); } catch (FileNotFoundException e) { // location was not found, create if (configFile != null && copyOutTemplate) { try (OutputStream fout = configFile.out()) { props.store(fout, comments); fout.flush(); } } } }
/** * Performs serialization with an {@link XStreamPersister} in a safe manner in which a temp file * is used for the serialization so that the true destination file is not partially written in * the case of an error. * * @param r The resource to write to, only modified if the temp file serialization was error * free. * @param obj The object to serialize. * @param xp The persister. */ public static void xStreamPersist(Resource r, Object obj, XStreamPersister xp) throws IOException { try (OutputStream out = r.out()) { xp.save(obj, out); out.flush(); } } }
static OutputStream output(URL url, Resource configDir) throws IOException { // check for file url if ("file".equalsIgnoreCase(url.getProtocol())) { File f = URLs.urlToFile(url); if (!f.isAbsolute()) { // make relative to config dir return configDir.get(f.getPath()).out(); } else { return new FileOutputStream(f); } } else { URLConnection cx = url.openConnection(); cx.setDoOutput(true); return cx.getOutputStream(); } }
void saveMasterPasswordDigest(String masterPasswdDigest) throws IOException { OutputStream fout = security().get(MASTER_PASSWD_DIGEST_FILENAME).out(); try { IOUtils.write(masterPasswdDigest, fout); } finally { fout.close(); } }
private void writeCurrentVersion() throws IOException { Resource security = security(); security.dir(); Resource properties = security.get(VERSION_PROPERTIES); Properties p = new Properties(); p.put(VERSION, CURR_VERSION.toString()); try (OutputStream os = properties.out()) { p.store( os, "Current version of the security directory. Do not remove or alter this file"); } }
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); } } }
@Override public void storeKeyStore() throws IOException { // store away the keystore assertActivatedKeyStore(); try (OutputStream fos = getResource().out()) { char[] passwd = securityManager.getMasterPassword(); try { ks.store(fos, passwd); } catch (Exception e) { throw new IOException(e); } finally { securityManager.disposePassword(passwd); } } }
@Theory public void theoryDoubleClose(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(resource())); OutputStream os = res.out(); os.close(); os.close(); }
public final void save(T service, GeoServer gs, Resource directory) throws Exception { String filename = getFilename(); Resource resource = directory == null ? resourceLoader.get(filename) : directory.get(filename); // using resource output stream makes sure we write on a temp file and them move try (OutputStream out = resource.out()) { XStreamPersister xp = xpf.createXMLPersister(); initXStreamPersister(xp, gs); xp.save(service, out); } }
@Theory public void theoryNonDirectoriesHaveFileWithSameContents(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 = new FileInputStream(res.file())) { istream.read(result); assertThat(istream.read(), is(-1)); } assertThat(result, equalTo(test)); }
@Theory public void theoryLeavesHaveOstream(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(resource())); try (OutputStream result = res.out()) { assertThat(result, notNullValue()); } }
@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 theoryDirectoriesHaveNoOstream(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); exception.expect(IllegalStateException.class); res.out().close(); }
@Theory public void theoryUndefinedHaveOstreamAndBecomeResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(undefined())); try (OutputStream result = res.out()) { assertThat(result, notNullValue()); assertThat(res, is(resource())); } }
@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(); }