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();
}
}
}
};
}
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;
}
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();
}
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);
}
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();
}
}
Aggregations