public OutputStream getOutputStream(final boolean append) throws AccessDeniedException { return this.getOutputStream(path, append); }
private void save(final Properties properties) { try (OutputStream out = file.getOutputStream(false)) { properties.store(out, "Credentials"); } catch(AccessDeniedException e) { log.warn(String.format("Failure saving credentials to %s. %s", file.getAbsolute(), e.getDetail())); } catch(IOException e) { log.warn(String.format("Failure saving credentials to %s. %s", file.getAbsolute(), e.getMessage())); } }
@Override public OutputStream getOutputStream(boolean append) throws AccessDeniedException { final NSURL resolved; try { resolved = this.lock(this.exists()); if(null == resolved) { return super.getOutputStream(append); } } catch(LocalAccessDeniedException e) { log.warn(String.format("Failure obtaining lock for %s. %s", this, e.getMessage())); return super.getOutputStream(append); } try { return new ProxyOutputStream(new FileOutputStream(new File(resolved.path()), append)) { @Override public void close() throws IOException { try { super.close(); } finally { release(resolved); } } }; } catch(FileNotFoundException e) { throw new LocalAccessDeniedException(e.getMessage(), e); } }
@Override public void write(final S item, final Local file) throws AccessDeniedException { final String content = item.<NSDictionary>serialize(SerializerFactory.get()).toXMLPropertyList(); final OutputStream out = file.getOutputStream(false); try { IOUtils.write(content, out, Charset.forName("UTF-8")); } catch(IOException e) { throw new AccessDeniedException(String.format("Cannot create file %s", file.getAbsolute()), e); } finally { IOUtils.closeQuietly(out); } } }
public void copy(final Local copy, final CopyOptions options) throws AccessDeniedException { if(copy.equals(this)) { log.warn(String.format("%s and %s are identical. Not copied.", this.getName(), copy.getName())); } else { if(log.isDebugEnabled()) { log.debug(String.format("Copy to %s with options %s", copy, options)); } InputStream in = null; OutputStream out = null; try { in = this.getInputStream(); out = copy.getOutputStream(options.append); IOUtils.copy(in, out); } catch(IOException e) { throw new LocalAccessDeniedException(MessageFormat.format( LocaleFactory.localizedString("Cannot copy {0}", "Error"), this.getName()), e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } }
@Override public void write(final Collection<S> collection, final Local file) throws AccessDeniedException { final NSArray list = new NSArray(collection.size()); int i = 0; for(S bookmark : collection) { list.setValue(i, bookmark.<NSDictionary>serialize(SerializerFactory.get())); i++; } final String content = list.toXMLPropertyList(); final OutputStream out = file.getOutputStream(false); try { IOUtils.write(content, out, Charset.forName("UTF-8")); } catch(IOException e) { throw new AccessDeniedException(String.format("Cannot create file %s", file.getAbsolute()), e); } finally { IOUtils.closeQuietly(out); } }
/** * Write temporary file with data * * @param icon Base64 encoded image information * @return Path to file */ private Local write(final String icon) { if(StringUtils.isBlank(icon)) { return null; } final byte[] favicon = Base64.decodeBase64(icon); final Local file = TemporaryFileServiceFactory.get().create(new AlphanumericRandomStringService().random()); try { new DefaultLocalTouchFeature().touch(file); final OutputStream out = file.getOutputStream(false); try { IOUtils.write(favicon, out); } finally { IOUtils.closeQuietly(out); } return file; } catch(IOException | AccessDeniedException e) { log.error("Error writing temporary file", e); } return null; }
@Override public void download(final Path file, final Local local, final BandwidthThrottle throttle, final StreamListener listener, final TransferStatus status, final ConnectionCallback connectionCallback, final PasswordCallback passwordCallback) throws BackgroundException { final InputStream in = reader.read(file, status, connectionCallback); final OutputStream out = local.getOutputStream(status.isAppend()); new StreamCopier(status, status) .withOffset(0L) .withLimit(status.getLength()) .withListener(listener) .transfer(new ThrottledInputStream(in, throttle), out); }