/** * Unzips the input stream via a gzip filter. use gunzip(ByteSource, File, Predicate) if possible * * @param in The input stream to run through the gunzip filter. This stream is closed * @param outFile The file to output to * * @throws IOException */ public static FileUtils.FileCopyResult gunzip(InputStream in, File outFile) throws IOException { try (GZIPInputStream gzipInputStream = gzipInputStream(in)) { NativeIO.chunkedCopy(gzipInputStream, outFile); return new FileUtils.FileCopyResult(outFile); } }
final int fd = getfd(raf.getFD()); trySyncFileRange(fd, offset, numBytes, SYNC_FILE_RANGE_WRITE); if (offset > 0) { trySyncFileRange(fd, lastOffset, lastBytes, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER); trySkipCache(fd, lastOffset, lastBytes);
posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
@Test public void testDisabledFadviseChunkedCopy() throws Exception { boolean possible = NativeIO.isFadvisePossible(); NativeIO.setFadvisePossible(false); File f = tempFolder.newFile(); byte[] bytes = new byte[]{(byte) 0x8, (byte) 0x9}; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); NativeIO.chunkedCopy(bis, f); byte[] data = Files.readAllBytes(f.toPath()); NativeIO.setFadvisePossible(possible); Assert.assertTrue(Arrays.equals(bytes, data)); }
@Test public void testDisabledSyncFileRangePossible() throws Exception { boolean possible = NativeIO.isSyncFileRangePossible(); NativeIO.setSyncFileRangePossible(false); File f = tempFolder.newFile(); byte[] bytes = new byte[]{(byte) 0x8, (byte) 0x9}; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); NativeIO.chunkedCopy(bis, f); byte[] data = Files.readAllBytes(f.toPath()); NativeIO.setSyncFileRangePossible(possible); Assert.assertTrue(Arrays.equals(bytes, data)); }
int ret_code = sync_file_range(fd, offset, nbytes, flags); if (ret_code != 0) { log.warn("failed on syncing fd [%d], offset [%d], bytes [%d], ret_code [%d], errno [%d]",
int ret_code = sync_file_range(fd, offset, nbytes, flags); if (ret_code != 0) { log.warn("failed on syncing fd [%d], offset [%d], bytes [%d], ret_code [%d], errno [%d]",
/** * Unzip from the input stream to the output directory, using the entry's file name as the file name in the output directory. * The behavior of directories in the input stream's zip is undefined. * If possible, it is recommended to use unzip(ByteStream, File) instead * * @param in The input stream of the zip data. This stream is closed * @param outDir The directory to copy the unzipped data to * * @return The FileUtils.FileCopyResult containing information on all the files which were written * * @throws IOException */ public static FileUtils.FileCopyResult unzip(InputStream in, File outDir) throws IOException { try (final ZipInputStream zipIn = new ZipInputStream(in)) { final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult(); ZipEntry entry; while ((entry = zipIn.getNextEntry()) != null) { final File file = new File(outDir, entry.getName()); validateZipOutputFile("", file, outDir); NativeIO.chunkedCopy(zipIn, file); result.addFile(file); zipIn.closeEntry(); } return result; } }
final RandomAccessFile raf = new RandomAccessFile(dest, "rw") ) { final int fd = getfd(raf.getFD()); trySyncFileRange(fd, offset, numBytes, SYNC_FILE_RANGE_WRITE); if (offset > 0) { trySyncFileRange(fd, lastOffset, lastBytes, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER); trySkipCache(fd, lastOffset, lastBytes);
posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
@Test public void testChunkedCopy() throws Exception { File f = tempFolder.newFile(); byte[] bytes = new byte[]{(byte) 0x8, (byte) 0x9}; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); NativeIO.chunkedCopy(bis, f); byte[] data = Files.readAllBytes(f.toPath()); Assert.assertTrue(Arrays.equals(bytes, data)); }
@Test(expected = IOException.class) public void testException() throws Exception { File dir = tempFolder.newFolder(); NativeIO.chunkedCopy(null, dir); }
final File outFile = new File(outDir, fname); try (final FSDataInputStream in = fs.open(childPath)) { NativeIO.chunkedCopy(in, outFile);
/** * Unzips the input stream via a gzip filter. use gunzip(ByteSource, File, Predicate) if possible * * @param in The input stream to run through the gunzip filter. This stream is closed * @param outFile The file to output to * * @throws IOException */ public static FileUtils.FileCopyResult gunzip(InputStream in, File outFile) throws IOException { try (GZIPInputStream gzipInputStream = gzipInputStream(in)) { NativeIO.chunkedCopy(gzipInputStream, outFile); return new FileUtils.FileCopyResult(outFile); } }
/** * Unzip from the input stream to the output directory, using the entry's file name as the file name in the output directory. * The behavior of directories in the input stream's zip is undefined. * If possible, it is recommended to use unzip(ByteStream, File) instead * * @param in The input stream of the zip data. This stream is closed * @param outDir The directory to copy the unzipped data to * * @return The FileUtils.FileCopyResult containing information on all the files which were written * * @throws IOException */ public static FileUtils.FileCopyResult unzip(InputStream in, File outDir) throws IOException { try (final ZipInputStream zipIn = new ZipInputStream(in)) { final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult(); ZipEntry entry; while ((entry = zipIn.getNextEntry()) != null) { final File file = new File(outDir, entry.getName()); validateZipOutputFile("", file, outDir); NativeIO.chunkedCopy(zipIn, file); result.addFile(file); zipIn.closeEntry(); } return result; } }