Search in sources :

Example 6 with BufferedOutputStream

use of java.io.BufferedOutputStream in project elasticsearch by elastic.

the class DeflateCompressor method streamOutput.

@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {

        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    deflater.end();
                }
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 7 with BufferedOutputStream

use of java.io.BufferedOutputStream in project buck by facebook.

the class ZipStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) {
    if (filesystem.exists(pathToZipFile)) {
        context.postEvent(ConsoleEvent.severe("Attempting to overwrite an existing zip: %s", pathToZipFile));
        return StepExecutionResult.ERROR;
    }
    // Since filesystem traversals can be non-deterministic, sort the entries we find into
    // a tree map before writing them out.
    final Map<String, Pair<CustomZipEntry, Optional<Path>>> entries = Maps.newTreeMap();
    FileVisitor<Path> pathFileVisitor = new SimpleFileVisitor<Path>() {

        private boolean isSkipFile(Path file) {
            return !paths.isEmpty() && !paths.contains(file);
        }

        private String getEntryName(Path path) {
            Path relativePath = junkPaths ? path.getFileName() : baseDir.relativize(path);
            return MorePaths.pathWithUnixSeparators(relativePath);
        }

        private CustomZipEntry getZipEntry(String entryName, final Path path, BasicFileAttributes attr) throws IOException {
            boolean isDirectory = filesystem.isDirectory(path);
            if (isDirectory) {
                entryName += "/";
            }
            CustomZipEntry entry = new CustomZipEntry(entryName);
            // We want deterministic ZIPs, so avoid mtimes.
            entry.setFakeTime();
            entry.setCompressionLevel(isDirectory ? ZipCompressionLevel.MIN_COMPRESSION_LEVEL.getValue() : compressionLevel.getValue());
            // If we're using STORED files, we must manually set the CRC, size, and compressed size.
            if (entry.getMethod() == ZipEntry.STORED && !isDirectory) {
                entry.setSize(attr.size());
                entry.setCompressedSize(attr.size());
                entry.setCrc(new ByteSource() {

                    @Override
                    public InputStream openStream() throws IOException {
                        return filesystem.newFileInputStream(path);
                    }
                }.hash(Hashing.crc32()).padToLong());
            }
            long externalAttributes = filesystem.getFileAttributesForZipEntry(path);
            LOG.verbose("Setting mode for entry %s path %s to 0x%08X", entryName, path, externalAttributes);
            entry.setExternalAttributes(externalAttributes);
            return entry;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (!isSkipFile(file)) {
                CustomZipEntry entry = getZipEntry(getEntryName(file), file, attrs);
                entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            if (!dir.equals(baseDir) && !isSkipFile(dir)) {
                CustomZipEntry entry = getZipEntry(getEntryName(dir), dir, attrs);
                entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
            }
            return FileVisitResult.CONTINUE;
        }
    };
    try (BufferedOutputStream baseOut = new BufferedOutputStream(filesystem.newFileOutputStream(pathToZipFile));
        CustomZipOutputStream out = ZipOutputStreams.newOutputStream(baseOut, THROW_EXCEPTION)) {
        filesystem.walkRelativeFileTree(baseDir, pathFileVisitor);
        // Write the entries out using the iteration order of the tree map above.
        for (Pair<CustomZipEntry, Optional<Path>> entry : entries.values()) {
            out.putNextEntry(entry.getFirst());
            if (entry.getSecond().isPresent()) {
                try (InputStream input = filesystem.newFileInputStream(entry.getSecond().get())) {
                    ByteStreams.copy(input, out);
                }
            }
            out.closeEntry();
        }
    } catch (IOException e) {
        context.logError(e, "Error creating zip file %s", pathToZipFile);
        return StepExecutionResult.ERROR;
    }
    return StepExecutionResult.SUCCESS;
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Optional(java.util.Optional) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteSource(com.google.common.io.ByteSource) BufferedOutputStream(java.io.BufferedOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Pair(com.facebook.buck.model.Pair)

Example 8 with BufferedOutputStream

use of java.io.BufferedOutputStream in project jetty.project by eclipse.

the class IncludedGzipTest method setUp.

@Before
public void setUp() throws Exception {
    testdir.ensureEmpty();
    File testFile = testdir.getPathFile("file.txt").toFile();
    try (OutputStream testOut = new BufferedOutputStream(new FileOutputStream(testFile))) {
        ByteArrayInputStream testIn = new ByteArrayInputStream(__content.getBytes("ISO8859_1"));
        IO.copy(testIn, testOut);
    }
    tester = new ServletTester("/context");
    tester.getContext().setResourceBase(testdir.getPath().toString());
    tester.getContext().addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
    GzipHandler gzipHandler = new GzipHandler();
    tester.getContext().insertHandler(gzipHandler);
    tester.start();
}
Also used : ServletTester(org.eclipse.jetty.servlet.ServletTester) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Before(org.junit.Before)

Example 9 with BufferedOutputStream

use of java.io.BufferedOutputStream in project druid by druid-io.

the class ByteBufferUtilsTest method testUnmapDoesntCrashJVM.

@Test
public void testUnmapDoesntCrashJVM() throws Exception {
    final File file = temporaryFolder.newFile("some_mmap_file");
    try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
        final byte[] data = new byte[4096];
        Arrays.fill(data, (byte) 0x5A);
        os.write(data);
    }
    final MappedByteBuffer mappedByteBuffer = Files.map(file);
    Assert.assertEquals((byte) 0x5A, mappedByteBuffer.get(0));
    ByteBufferUtils.unmap(mappedByteBuffer);
    ByteBufferUtils.unmap(mappedByteBuffer);
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Example 10 with BufferedOutputStream

use of java.io.BufferedOutputStream in project che by eclipse.

the class JGitDiffPage method writeTo.

@Override
public final void writeTo(OutputStream out) throws IOException {
    DiffFormatter formatter = new DiffFormatter(new BufferedOutputStream(out));
    formatter.setRepository(repository);
    List<String> rawFileFilter = params.getFileFilter();
    TreeFilter pathFilter = (rawFileFilter != null && rawFileFilter.size() > 0) ? PathFilterGroup.createFromStrings(rawFileFilter) : TreeFilter.ALL;
    formatter.setPathFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
    try {
        String commitA = params.getCommitA();
        String commitB = params.getCommitB();
        boolean cached = params.isCached();
        List<DiffEntry> diff;
        if (commitA == null && commitB == null && !cached) {
            diff = indexToWorkingTree(formatter);
        } else if (commitA != null && commitB == null && !cached) {
            diff = commitToWorkingTree(commitA, formatter);
        } else if (commitA == null && commitB != null) {
            diff = emptyToCommit(commitB, formatter);
        } else if (commitB == null) {
            diff = commitToIndex(commitA, formatter);
        } else {
            diff = commitToCommit(commitA, commitB, formatter);
        }
        DiffType type = params.getType();
        if (type == DiffType.NAME_ONLY) {
            writeNames(diff, out);
        } else if (type == DiffType.NAME_STATUS) {
            writeNamesAndStatus(diff, out);
        } else {
            writeRawDiff(diff, formatter);
        }
    } finally {
        formatter.close();
        repository.close();
    }
}
Also used : AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) BufferedOutputStream(java.io.BufferedOutputStream) DiffType(org.eclipse.che.api.git.shared.DiffType) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)2418 FileOutputStream (java.io.FileOutputStream)1659 IOException (java.io.IOException)1239 File (java.io.File)1060 OutputStream (java.io.OutputStream)791 BufferedInputStream (java.io.BufferedInputStream)527 InputStream (java.io.InputStream)374 FileInputStream (java.io.FileInputStream)332 DataOutputStream (java.io.DataOutputStream)242 ByteArrayOutputStream (java.io.ByteArrayOutputStream)228 ZipEntry (java.util.zip.ZipEntry)212 ZipOutputStream (java.util.zip.ZipOutputStream)209 FileNotFoundException (java.io.FileNotFoundException)191 ZipFile (java.util.zip.ZipFile)115 ArrayList (java.util.ArrayList)107 URL (java.net.URL)101 ObjectOutputStream (java.io.ObjectOutputStream)99 PrintStream (java.io.PrintStream)98 Test (org.junit.Test)94 ByteArrayInputStream (java.io.ByteArrayInputStream)89