Search in sources :

Example 41 with OpenOption

use of java.nio.file.OpenOption in project fastr by oracle.

the class RCompression method bzipCompressToFile.

public static void bzipCompressToFile(byte[] data, TruffleFile path, boolean append) throws IOException {
    String[] command = new String[] { "bzip2", "-zc" };
    int rc;
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectError(Redirect.INHERIT);
    Process p = pb.start();
    InputStream is = p.getInputStream();
    OutputStream os = p.getOutputStream();
    ProcessOutputManager.OutputThreadVariable readThread = new ProcessOutputManager.OutputThreadVariable(command[0], is);
    readThread.start();
    os.write(data);
    os.close();
    try {
        rc = p.waitFor();
        if (rc == 0) {
            readThread.join();
            byte[] cData = Arrays.copyOf(readThread.getData(), readThread.getTotalRead());
            OpenOption[] openOptions = append ? new OpenOption[] { StandardOpenOption.APPEND } : new OpenOption[0];
            path.newOutputStream(openOptions).write(cData);
            return;
        } else {
            throw new IOException("bzip2 error code: " + rc);
        }
    } catch (InterruptedException ex) {
    // fall through
    }
    throw new IOException();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) LZMA2InputStream(org.tukaani.xz.LZMA2InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption)

Example 42 with OpenOption

use of java.nio.file.OpenOption in project SchemaCrawler by schemacrawler.

the class FileOutputResource method openNewOutputWriter.

@Override
public Writer openNewOutputWriter(final Charset charset, final boolean appendOutput) throws IOException {
    requireNonNull(charset, "No output charset provided");
    final OpenOption[] openOptions;
    if (appendOutput) {
        openOptions = new OpenOption[] { WRITE, CREATE, APPEND };
    } else {
        openOptions = new OpenOption[] { WRITE, CREATE, TRUNCATE_EXISTING };
    }
    final Writer writer = newBufferedWriter(outputFile, charset, openOptions);
    LOGGER.log(Level.FINE, new StringFormat("Opened output writer to file <%s>", outputFile));
    return wrapWriter(getDescription(), writer, true);
}
Also used : OpenOption(java.nio.file.OpenOption) InputResourceUtility.wrapWriter(us.fatehi.utility.ioresource.InputResourceUtility.wrapWriter) Writer(java.io.Writer) Files.newBufferedWriter(java.nio.file.Files.newBufferedWriter) StringFormat(us.fatehi.utility.string.StringFormat)

Example 43 with OpenOption

use of java.nio.file.OpenOption in project quilt-loader by QuiltMC.

the class QuiltJoinedFileSystemProvider method newByteChannel.

@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
    for (OpenOption o : options) {
        if (o != StandardOpenOption.READ) {
            throw new UnsupportedOperationException("'" + o + "' not allowed");
        }
    }
    if (!(path instanceof QuiltJoinedPath)) {
        throw new IllegalArgumentException("The given path is not a QuiltJoinedPath!");
    }
    QuiltJoinedPath p = (QuiltJoinedPath) path;
    int count = p.fs.getBackingPathCount();
    for (int i = 0; i < count; i++) {
        Path real = p.fs.getBackingPath(i, p);
        try {
            return Files.newByteChannel(real, options, attrs);
        } catch (NoSuchFileException e) {
            if (i == count - 1) {
                throw e;
            }
        }
    }
    throw new NoSuchFileException(path.toString());
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 44 with OpenOption

use of java.nio.file.OpenOption in project quilt-loader by QuiltMC.

the class QuiltMemoryFileSystemProvider method newInputStream.

@Override
public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
    for (OpenOption o : options) {
        if (o != StandardOpenOption.READ) {
            throw new UnsupportedOperationException("'" + o + "' not allowed");
        }
    }
    if (path instanceof QuiltMemoryPath) {
        QuiltMemoryPath p = (QuiltMemoryPath) path;
        QuiltMemoryEntry entry = p.fs.files.get(p.toAbsolutePath().normalize());
        if (entry instanceof QuiltMemoryFile) {
            return ((QuiltMemoryFile) entry).createInputStream();
        } else if (entry != null) {
            throw new FileSystemException("Cannot open an InputStream on a directory!");
        } else {
            throw new NoSuchFileException(path.toString());
        }
    } else {
        throw new IllegalArgumentException("The given path is not a QuiltMemoryPath!");
    }
}
Also used : OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) FileSystemException(java.nio.file.FileSystemException) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 45 with OpenOption

use of java.nio.file.OpenOption in project mina-sshd by apache.

the class ScpHelper method receiveFileStream.

public void receiveFileStream(OutputStream local, int bufferSize) throws IOException {
    receive((session, line, isDir, timestamp) -> {
        if (isDir) {
            throw new StreamCorruptedException("Cannot download a directory into a file stream: " + line);
        }
        Path path = new MockPath(line);
        receiveStream(line, new ScpTargetStreamResolver() {

            @Override
            // see
            @SuppressWarnings("synthetic-access")
            public // https://bugs.eclipse.org/bugs/show_bug.cgi?id=537593
            OutputStream resolveTargetStream(Session session, String name, long length, Set<PosixFilePermission> perms, OpenOption... options) throws IOException {
                if (log.isDebugEnabled()) {
                    log.debug("resolveTargetStream({}) name={}, perms={}, len={} - started local stream download", ScpHelper.this, name, perms, length);
                }
                return local;
            }

            @Override
            public Path getEventListenerFilePath() {
                return path;
            }

            @Override
            // see
            @SuppressWarnings("synthetic-access")
            public // https://bugs.eclipse.org/bugs/show_bug.cgi?id=537593
            void postProcessReceivedData(String name, boolean preserve, Set<PosixFilePermission> perms, ScpTimestampCommandDetails time) throws IOException {
                if (log.isDebugEnabled()) {
                    log.debug("postProcessReceivedData({}) name={}, perms={}, preserve={} time={}", ScpHelper.this, name, perms, preserve, time);
                }
            }

            @Override
            public String toString() {
                return line;
            }
        }, timestamp, false, bufferSize);
    });
}
Also used : Path(java.nio.file.Path) MockPath(org.apache.sshd.common.file.util.MockPath) ScpTimestampCommandDetails(org.apache.sshd.scp.common.helpers.ScpTimestampCommandDetails) OutputStream(java.io.OutputStream) MockPath(org.apache.sshd.common.file.util.MockPath) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) OpenOption(java.nio.file.OpenOption) StreamCorruptedException(java.io.StreamCorruptedException) Session(org.apache.sshd.common.session.Session)

Aggregations

OpenOption (java.nio.file.OpenOption)108 StandardOpenOption (java.nio.file.StandardOpenOption)81 IOException (java.io.IOException)43 HashSet (java.util.HashSet)34 Path (java.nio.file.Path)33 File (java.io.File)19 Test (org.junit.Test)19 FileChannel (java.nio.channels.FileChannel)18 NoSuchFileException (java.nio.file.NoSuchFileException)16 OutputStream (java.io.OutputStream)15 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11 ArrayList (java.util.ArrayList)11 InputStream (java.io.InputStream)10 ByteBuffer (java.nio.ByteBuffer)10 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)10 FileIODecorator (org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator)10 Set (java.util.Set)8 FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)8 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)8 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)7