List of usage examples for java.nio.channels Channels newOutputStream
public static OutputStream newOutputStream(AsynchronousByteChannel ch)
From source file:com.almende.eve.state.ConcurrentJsonFileState.java
/** * Open file.//from w ww . ja va 2s .c o m * * @throws IOException * Signals that an I/O exception has occurred. */ @SuppressWarnings("resource") protected void openFile() throws IOException { synchronized (locked) { while (locked.containsKey(filename) && locked.get(filename)) { try { locked.wait(); } catch (final InterruptedException e) { } } locked.put(filename, true); final File file = new File(filename); if (!file.exists()) { locked.put(filename, false); locked.notifyAll(); throw new IllegalStateException("Warning: File doesn't exist (anymore):'" + filename + "'"); } channel = new RandomAccessFile(file, "rw").getChannel(); try { // TODO: add support for shared locks, allowing parallel reading // operations. lock = channel.lock(); } catch (final Exception e) { channel.close(); channel = null; lock = null; locked.put(filename, false); locked.notifyAll(); throw new IllegalStateException("error, couldn't obtain file lock on:" + filename, e); } fis = Channels.newInputStream(channel); fos = Channels.newOutputStream(channel); } }
From source file:com.lemania.sis.server.servlet.GcsServlet.java
/** * Writes the payload of the incoming post as the contents of a file to GCS. * If the request path is /gcs/Foo/Bar this will be interpreted as a request * to create a GCS file named Bar in bucket Foo. * // ww w .j av a2s . c o m * @throws ServletException */ @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // GcsOutputChannel outputChannel = gcsService.createOrReplace(getFileName(req), GcsFileOptions.getDefaultInstance()); ServletFileUpload upload = new ServletFileUpload(); resp.setContentType("text/plain"); // InputStream fileContent = null; FileItemIterator iterator; try { iterator = upload.getItemIterator(req); while (iterator.hasNext()) { FileItemStream item = iterator.next(); InputStream stream = item.openStream(); if (item.isFormField()) { // } else { fileContent = stream; break; } } } catch (FileUploadException e) { // e.printStackTrace(); } // copy(fileContent, Channels.newOutputStream(outputChannel)); }
From source file:com.almende.eve.state.file.ConcurrentSerializableFileState.java
/** * Open file./* w w w .ja v a2 s . c om*/ * * @throws IOException * Signals that an I/O exception has occurred. */ @SuppressWarnings("resource") protected void openFile() throws IOException { synchronized (locked) { while (locked.containsKey(filename) && locked.get(filename)) { try { locked.wait(); } catch (final InterruptedException e) { } } locked.put(filename, true); final File file = new File(filename); if (!file.exists()) { locked.put(filename, false); locked.notifyAll(); throw new IllegalStateException("Warning: File doesn't exist (anymore):'" + filename + "'"); } channel = new RandomAccessFile(file, "rw").getChannel(); try { // TODO: add support for shared locks, allowing parallel reading // operations. lock = channel.lock(); } catch (final Exception e) { channel.close(); channel = null; lock = null; locked.put(filename, false); locked.notifyAll(); throw new IllegalStateException("error, couldn't obtain file lock on:" + filename, e); } fis = new BufferedInputStream(Channels.newInputStream(channel)); fos = new BufferedOutputStream(Channels.newOutputStream(channel)); } }
From source file:com.almende.eve.state.file.ConcurrentJsonFileState.java
/** * Open file.//w ww . ja va 2 s . co m * * @throws IOException * Signals that an I/O exception has occurred. */ @SuppressWarnings("resource") protected void openFile() throws IOException { Lock llock = null; synchronized (LOCKED) { llock = LOCKED.get(filename); if (llock == null) { llock = new Lock(); LOCKED.put(filename, llock); } } synchronized (llock) { while (llock.locked) { try { llock.wait(); } catch (final InterruptedException e) { } } llock.locked = true; final File file = new File(filename); if (!file.exists()) { llock.locked = false; llock.notifyAll(); throw new IllegalStateException("Warning: File doesn't exist (anymore):'" + filename + "'"); } channel = new RandomAccessFile(file, "rw").getChannel(); try { // TODO: add support for shared locks, allowing parallel reading // operations. lock = channel.lock(); } catch (final Exception e) { channel.close(); channel = null; lock = null; llock.locked = false; llock.notifyAll(); throw new IllegalStateException("error, couldn't obtain file lock on:" + filename, e); } fis = new BufferedInputStream(Channels.newInputStream(channel)); fos = new BufferedOutputStream(Channels.newOutputStream(channel)); } }
From source file:ee.ria.xroad.common.messagelog.archive.LogArchiveWriter.java
private boolean archiveAsicContainers() { try (InputStream input = logArchiveCache.getArchiveFile(); OutputStream output = Channels.newOutputStream(archiveOut)) { IOUtils.copy(input, output);/*from www. j av a2 s. co m*/ } catch (IOException e) { log.error("Failed to archive ASiC containers due to IO error", e); return false; } return true; }
From source file:com.thinkberg.vfs.s3.jets3t.Jets3tFileObject.java
protected OutputStream doGetOutputStream(boolean bAppend) throws Exception { return new MonitorOutputStream(Channels.newOutputStream(getCacheFile().getChannel())) { protected void onClose() throws IOException { try { LOG.debug(String.format("sending '%s' to storage (cached=%b)", object.getKey(), cacheFile)); if (cacheFile != null) { FileChannel cacheFc = getCacheFile().getChannel(); object.setContentLength(cacheFc.size()); object.setDataInputStream(Channels.newInputStream(cacheFc)); }/*from w w w. j av a 2 s.c o m*/ service.putObject(bucket, object); } catch (S3ServiceException e) { LOG.error(String.format("can't send object '%s' to storage", object), e); } } }; }
From source file:com.norconex.commons.lang.io.CachedOutputStream.java
@SuppressWarnings("resource") private void cacheToFile() throws IOException { fileCache = File.createTempFile("CachedOutputStream-", "-temp", cacheDirectory); fileCache.deleteOnExit();/* w ww. ja va 2 s. c om*/ LOG.debug("Reached max cache size. Swapping to file: " + fileCache); RandomAccessFile f = new RandomAccessFile(fileCache, "rw"); FileChannel channel = f.getChannel(); fileOutputStream = Channels.newOutputStream(channel); IOUtils.write(memOutputStream.toByteArray(), fileOutputStream); memOutputStream = null; }
From source file:com.github.jinahya.simple.file.back.LocalFileBackTest.java
@Test(enabled = true, invocationCount = 1) public void write() throws IOException, FileBackException { fileContext.fileOperationSupplier(() -> FileOperation.WRITE); final ByteBuffer fileKey = randomFileKey(); fileContext.targetKeySupplier(() -> fileKey); final Path leafPath = LocalFileBack.leafPath(rootPath, fileKey, true); final byte[] fileBytes = randomFileBytes(); final boolean fileWritten = Files.isRegularFile(leafPath) || current().nextBoolean(); if (fileWritten) { Files.write(leafPath, fileBytes, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); logger.trace("file written"); }//from ww w. jav a 2s .co m fileContext.sourceChannelSupplier(() -> Channels.newChannel(new ByteArrayInputStream(fileBytes))); fileContext.targetChannelConsumer(v -> { try { final long copied = IOUtils.copyLarge(new ByteArrayInputStream(fileBytes), Channels.newOutputStream(v)); } catch (final IOException ioe) { logger.error("failed to copy", ioe); } }); fileBack.operate(fileContext); if (fileWritten) { final byte[] actual = Files.readAllBytes(leafPath); assertEquals(actual, fileBytes); } }