java.nio.channels.FileChannel Java Examples
The following examples show how to use
java.nio.channels.FileChannel.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZipCompletionScannerUnitTestCase.java From wildfly-core with GNU Lesser General Public License v2.1 | 8 votes |
@Test public void testMaxScan() throws Exception { int size = (1 << 16) + 22; File maxscan = testSupport.getFile("maxscan.jar"); FileChannel ch = testSupport.getChannel(maxscan, false); ByteBuffer bb = ByteBuffer.allocate(1); bb.put((byte)0); bb.flip(); ch.write(bb, size -1); Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscan)); File maxscanplus = testSupport.getFile("maxscanplus.jar"); ch = testSupport.getChannel(maxscanplus, false); bb.flip(); ch.write(bb, size); Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscanplus)); File maxscanminus = testSupport.getFile("maxscanminus.jar"); ch = testSupport.getChannel(maxscanminus, false); bb.flip(); ch.write(bb, size - 2); Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscanplus)); }
Example #2
Source File: BrowserMonitor.java From xdm with GNU General Public License v2.0 | 6 votes |
private void acquireGlobalLock() { try { fc = FileChannel.open(Paths.get(System.getProperty("user.home"), XDMApp.GLOBAL_LOCK_FILE), EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.READ, StandardOpenOption.WRITE)); int maxRetry = 10; for (int i = 0; i < maxRetry; i++) { fileLock = fc.tryLock(); if (fileLock != null) { Logger.log("Lock acquired..."); return; } // if lock is already acquired by some other process wait // and retry for at most 5 sec, after that throw error and // exit Thread.sleep(500); } } catch (Exception e) { e.printStackTrace(); } }
Example #3
Source File: IOTinyUtils.java From rocketmq_trans_message with Apache License 2.0 | 6 votes |
static public void copyFile(String source, String target) throws IOException { File sf = new File(source); if (!sf.exists()) { throw new IllegalArgumentException("source file does not exist."); } File tf = new File(target); tf.getParentFile().mkdirs(); if (!tf.exists() && !tf.createNewFile()) { throw new RuntimeException("failed to create target file."); } FileChannel sc = null; FileChannel tc = null; try { tc = new FileOutputStream(tf).getChannel(); sc = new FileInputStream(sf).getChannel(); sc.transferTo(0, sc.size(), tc); } finally { if (null != sc) { sc.close(); } if (null != tc) { tc.close(); } } }
Example #4
Source File: DirectByteBuffer.java From TorrentEngine with GNU General Public License v3.0 | 6 votes |
public int read( byte subsystem, FileChannel chan ) throws IOException { if ( TRACE ){ traceUsage( subsystem, OP_READ_FC); } try{ return( chan.read(buffer )); }catch( IllegalArgumentException e ){ dumpTrace(e); throw( e ); } }
Example #5
Source File: MigrationUtils.java From aeron with Apache License 2.0 | 6 votes |
public static FileChannel createMigrationTimestampFile( final File directory, final int fromVersion, final int toVersion) { final String filename = MIGRATION_TIMESTAMP_FILE_PREFIX + fromVersion + "-to-" + toVersion + MIGRATION_TIMESTAMP_FILE_SUFFIX; final File timestampFile = new File(directory, filename); FileChannel fileChannel = null; try { fileChannel = FileChannel.open(timestampFile.toPath(), CREATE_NEW, READ, WRITE, SPARSE); fileChannel.force(true); } catch (final Exception ex) { System.err.println("Could not create migration timestamp file:" + timestampFile); LangUtil.rethrowUnchecked(ex); } return fileChannel; }
Example #6
Source File: BlockDeviceSize.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Throwable { try (FileChannel ch = FileChannel.open(BLK_PATH, READ); RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) { long size1 = ch.size(); long size2 = file.length(); if (size1 != size2) { throw new RuntimeException("size differs when retrieved" + " in different ways: " + size1 + " != " + size2); } System.out.println("OK"); } catch (NoSuchFileException nsfe) { System.err.println("File " + BLK_FNAME + " not found." + " Skipping test"); } catch (AccessDeniedException ade) { System.err.println("Access to " + BLK_FNAME + " is denied." + " Run test as root."); } }
Example #7
Source File: TestDisabledEvents.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private static void useFileChannel(File tmp) throws Throwable { tmp.delete(); try (RandomAccessFile rf = new RandomAccessFile(tmp, "rw"); FileChannel ch = rf.getChannel()) { final String bufContent = "0123456789"; final int bufSize = bufContent.length(); ByteBuffer writeBuf = ByteBuffer.allocateDirect(bufSize); writeBuf.put(bufContent.getBytes()); writeBuf.flip(); int writeSize = ch.write(writeBuf); assertEquals(writeSize, bufSize, "Wrong writeSize for FileChannel"); ch.position(0); ByteBuffer readBuf = ByteBuffer.allocateDirect(bufSize); int readSize = ch.read(readBuf); assertEquals(readSize, bufSize, "Wrong readSize full for FileChannel"); assertEquals(0, writeBuf.compareTo(readBuf), "Unexpected readBuf content"); } }
Example #8
Source File: FileUtils.java From mobikul-standalone-pos with MIT License | 6 votes |
/** * Creates the specified <code>toFile</code> as a byte for byte copy of the * <code>fromFile</code>. If <code>toFile</code> already exists, then it * will be replaced with a copy of <code>fromFile</code>. The name and path * of <code>toFile</code> will be that of <code>toFile</code>.<br/> * <br/> * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by * this function.</i> * * @param fromFile * - FileInputStream for the file to copy from. * @param toFile * - FileInputStream for the file to copy to. */ public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException { FileChannel fromChannel = null; FileChannel toChannel = null; try { fromChannel = fromFile.getChannel(); toChannel = toFile.getChannel(); fromChannel.transferTo(0, fromChannel.size(), toChannel); } finally { try { if (fromChannel != null) { fromChannel.close(); } } finally { if (toChannel != null) { toChannel.close(); } } } }
Example #9
Source File: IgniteSnapshotManager.java From ignite with Apache License 2.0 | 6 votes |
/** * @param factory Factory to produce FileIO access. * @param from Copy from file. * @param to Copy data to file. * @param length Number of bytes to copy from beginning. */ static void copy(FileIOFactory factory, File from, File to, long length) { try (FileIO src = factory.create(from, READ); FileChannel dest = new FileOutputStream(to).getChannel()) { if (src.size() < length) { throw new IgniteException("The source file to copy has to enough length " + "[expected=" + length + ", actual=" + src.size() + ']'); } src.position(0); long written = 0; while (written < length) written += src.transferTo(written, length - written, dest); } catch (IOException e) { throw new IgniteException(e); } }
Example #10
Source File: FileUtil.java From hsac-fitnesse-fixtures with Apache License 2.0 | 6 votes |
/** * Copies source file to target. * * @param source source file to copy. * @param target destination to copy to. * @return target as File. * @throws IOException when unable to copy. */ public static File copyFile(String source, String target) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(target).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { if (inputChannel != null) { inputChannel.close(); } if (outputChannel != null) { outputChannel.close(); } } return new File(target); }
Example #11
Source File: IoUtil.java From che with Eclipse Public License 2.0 | 6 votes |
private static void nioCopyFile(File source, File target, boolean replaceIfExists) throws IOException { if (!target.createNewFile()) // atomic { if (target.exists() && !replaceIfExists) { throw new IOException( String.format("File '%s' already exists. ", target.getAbsolutePath())); } } try (FileInputStream sourceStream = new FileInputStream(source); FileOutputStream targetStream = new FileOutputStream(target); FileChannel sourceChannel = sourceStream.getChannel(); FileChannel targetChannel = targetStream.getChannel()) { final long size = sourceChannel.size(); long transferred = 0L; while (transferred < size) { transferred += targetChannel.transferFrom(sourceChannel, transferred, (size - transferred)); } } }
Example #12
Source File: Helper.java From packer-ng-plugin with Apache License 2.0 | 6 votes |
public static void copyFile(File src, File dest) throws IOException { if (!dest.exists()) { dest.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(src).getChannel(); destination = new FileOutputStream(dest).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
Example #13
Source File: DexExtractor.java From sbt-android-protify with Apache License 2.0 | 6 votes |
private static <T> T withBlockingLock(File dexDir, File sourceApk, RunnableIO<T> r) throws IOException { final long currentCrc = getZipCrc(sourceApk); File lockFile = new File(dexDir, LOCK_FILE); RandomAccessFile lockRaf = new RandomAccessFile(lockFile, "rw"); FileChannel lockChannel = null; FileLock cacheLock = null; try { lockChannel = lockRaf.getChannel(); Log.v(TAG, "Waiting for lock on: " + lockFile.getAbsolutePath()); cacheLock = lockChannel.lock(); Log.v(TAG, "Locked " + lockFile.getPath()); long l = lockRaf.length() >= (Long.SIZE / 8) ? lockRaf.readLong() : 0; long c = lockRaf.length() >= 2 * (Long.SIZE / 8) ? lockRaf.readLong() : 0; return r.run(c != currentCrc || l != getTimeStamp(sourceApk)); } finally { lockRaf.seek(0); lockRaf.writeLong(getTimeStamp(sourceApk)); lockRaf.writeLong(currentCrc); if (cacheLock != null) cacheLock.release(); if (lockChannel != null) lockChannel.close(); lockRaf.close(); Log.v(TAG, "Unlocked " + lockFile.getPath()); } }
Example #14
Source File: BufferedFileTest.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testGetBufferReallocate() throws Exception { BufferedFile instance; int fileSize = 64; String filename = "bytes64"; byte[] bytes = fileData(fileSize); fileSystem.addFile(filename, bytes); FileChannel file = fileSystem.getInputChannel(filename); int regionOff = 5; int regionSize = 50; int maxAlloc = 20; int cacheOff = regionOff + 5; instance = new BufferedFile(file, regionOff, regionSize, maxAlloc); instance.getBuffer(cacheOff, maxAlloc); assertCase("Realloc after", instance, cacheOff + maxAlloc, maxAlloc, maxAlloc, maxAlloc); assertCase("Realloc before", instance, cacheOff, maxAlloc, maxAlloc, maxAlloc); assertCase("Realloc just after", instance, cacheOff + 5, maxAlloc, maxAlloc, maxAlloc); assertCase("Realloc just before", instance, cacheOff, maxAlloc, maxAlloc, maxAlloc); assertCase("Realloc supersize", instance, cacheOff, maxAlloc + 5, maxAlloc + 5, maxAlloc + 5); }
Example #15
Source File: LocalFilesystem.java From reader with MIT License | 6 votes |
/** * Moved this code into it's own method so moveTo could use it when the move is across file systems */ private void copyAction(File srcFile, File destFile) throws FileNotFoundException, IOException { FileInputStream istream = new FileInputStream(srcFile); FileOutputStream ostream = new FileOutputStream(destFile); FileChannel input = istream.getChannel(); FileChannel output = ostream.getChannel(); try { input.transferTo(0, input.size(), output); } finally { istream.close(); ostream.close(); input.close(); output.close(); } }
Example #16
Source File: OpenAtlasFileLock.java From ACDD with MIT License | 6 votes |
/** * lock odex * * @param bundleDexFile optimize dex file **/ public boolean LockExclusive(File bundleDexFile) { if (bundleDexFile == null) { return false; } try { File lockFile = new File(bundleDexFile.getParentFile().getAbsolutePath().concat("/lock")); if (!lockFile.exists()) { lockFile.createNewFile(); } RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile.getAbsolutePath(), "rw"); FileChannel channel = randomAccessFile.getChannel(); FileLock lock = channel.lock(); if (!lock.isValid()) { return false; } RefCntInc(lockFile.getAbsolutePath(), lock, randomAccessFile, channel); return true; } catch (Exception e) { log.error(processName + " FileLock " + bundleDexFile.getParentFile().getAbsolutePath().concat("/lock") + " Lock FAIL! " + e.getMessage()); return false; } }
Example #17
Source File: TestUtil.java From jackcess with Apache License 2.0 | 6 votes |
public static Database open(FileFormat fileFormat, File file, boolean inMem, Charset charset) throws Exception { FileChannel channel = (inMem ? MemFileChannel.newChannel( file, MemFileChannel.RW_CHANNEL_MODE) : null); final Database db = new DatabaseBuilder(file).setReadOnly(true) .setAutoSync(getTestAutoSync()).setChannel(channel) .setCharset(charset).open(); Assert.assertEquals("Wrong JetFormat.", DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(), ((DatabaseImpl)db).getFormat()); Assert.assertEquals("Wrong FileFormat.", fileFormat, db.getFileFormat()); return db; }
Example #18
Source File: BufferReaderWriterUtilTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void readPrematureEndOfFile2() throws Exception { final FileChannel fc = tmpFileChannel(); final Buffer buffer = createTestBuffer(); final MemorySegment readBuffer = MemorySegmentFactory.allocateUnpooledOffHeapMemory(buffer.getSize(), null); BufferReaderWriterUtil.writeToByteChannel(fc, buffer, BufferReaderWriterUtil.allocatedWriteBufferArray()); fc.truncate(2); // less than a header size fc.position(0); try { BufferReaderWriterUtil.readFromByteChannel( fc, BufferReaderWriterUtil.allocatedHeaderBuffer(), readBuffer, FreeingBufferRecycler.INSTANCE); fail(); } catch (IOException e) { // expected } }
Example #19
Source File: TestMMapBuffer.java From util with Apache License 2.0 | 6 votes |
@Test public void testMadviseDontNeedTrackedBuffers() throws IOException { try { MMapBuffer.setTrackingEnabled(false); MMapBuffer.madviseDontNeedTrackedBuffers(); MMapBuffer.setTrackingEnabled(true); MMapBuffer.madviseDontNeedTrackedBuffers(); final File tempFile = File.createTempFile("TestMMapBuffer", ""); try (MMapBuffer ignored = new MMapBuffer(tempFile, 0, 10, FileChannel.MapMode.READ_WRITE, ByteOrder.nativeOrder())) { MMapBuffer.madviseDontNeedTrackedBuffers(); } MMapBuffer.madviseDontNeedTrackedBuffers(); } finally { MMapBuffer.setTrackingEnabled(false); } }
Example #20
Source File: FLVReader.java From red5-io with Apache License 2.0 | 6 votes |
/** * Creates FLV reader from file channel. * * @param channel * file channel * @throws IOException * on error */ public FLVReader(FileChannel channel) throws IOException { if (null == channel) { log.warn("Reader was passed a null channel"); log.debug("{}", org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString(this)); } if (!channel.isOpen()) { log.warn("Reader was passed a closed channel"); return; } this.channel = channel; channelSize = channel.size(); log.debug("Channel size: {}", channelSize); if (channel.position() > 0) { log.debug("Channel position: {}", channel.position()); channel.position(0); } fillBuffer(); postInitialize(); }
Example #21
Source File: DataParser.java From iBioSim with Apache License 2.0 | 5 votes |
/** * A helper function. Read a file into a string. Thanks to erickson at * http:/ * /stackoverflow.com/questions/326390/how-to-create-a-java-string-from * -the-contents-of-a-file * * @param path * : the path to the file * @return * @throws IOException */ public static String readFileToString(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
Example #22
Source File: ConcurrentDiskStore.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
private void openWriteChannel(long startOffset) throws IOException { if (startOffset > 0) { // TODO does not support append yet writeChannel = FileChannel.open(diskFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE); } else { diskFile.delete(); writeChannel = FileChannel.open(diskFile.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); } }
Example #23
Source File: SysFsPerfTest.java From diozero with MIT License | 5 votes |
private static void testMmapToggle(int gpio, int iterations) throws IOException { try (FileChannel fc = FileChannel.open(Paths.get("/sys/class/gpio/gpio" + gpio + "/value"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SYNC)) { MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 1); long start = System.currentTimeMillis(); for (int i=0; i<iterations; i++) { mbb.put(0, (byte) '1'); mbb.put(0, (byte) '0'); } long duration = System.currentTimeMillis() - start; Logger.info("mmap toggle: {0.000} ms per iteration", Double.valueOf(duration*1000 / (double) iterations)); } }
Example #24
Source File: TranslogWriter.java From crate with Apache License 2.0 | 5 votes |
private TranslogWriter( final ChannelFactory channelFactory, final ShardId shardId, final Checkpoint initialCheckpoint, final FileChannel channel, final Path path, final ByteSizeValue bufferSize, final LongSupplier globalCheckpointSupplier, LongSupplier minTranslogGenerationSupplier, TranslogHeader header, TragicExceptionHolder tragedy, final LongConsumer persistedSequenceNumberConsumer) throws IOException { super(initialCheckpoint.generation, channel, path, header); assert initialCheckpoint.offset == channel.position() : "initial checkpoint offset [" + initialCheckpoint.offset + "] is different than current channel position [" + channel.position() + "]"; this.shardId = shardId; this.channelFactory = channelFactory; this.minTranslogGenerationSupplier = minTranslogGenerationSupplier; this.outputStream = new BufferedChannelOutputStream(java.nio.channels.Channels.newOutputStream(channel), bufferSize.bytesAsInt()); this.lastSyncedCheckpoint = initialCheckpoint; this.totalOffset = initialCheckpoint.offset; assert initialCheckpoint.minSeqNo == SequenceNumbers.NO_OPS_PERFORMED : initialCheckpoint.minSeqNo; this.minSeqNo = initialCheckpoint.minSeqNo; assert initialCheckpoint.maxSeqNo == SequenceNumbers.NO_OPS_PERFORMED : initialCheckpoint.maxSeqNo; this.maxSeqNo = initialCheckpoint.maxSeqNo; assert initialCheckpoint.trimmedAboveSeqNo == SequenceNumbers.UNASSIGNED_SEQ_NO : initialCheckpoint.trimmedAboveSeqNo; this.globalCheckpointSupplier = globalCheckpointSupplier; this.nonFsyncedSequenceNumbers = new LongArrayList(64); this.persistedSequenceNumberConsumer = persistedSequenceNumberConsumer; this.seenSequenceNumbers = Assertions.ENABLED ? new HashMap<>() : null; this.tragedy = tragedy; }
Example #25
Source File: JimfsFileChannel.java From jimfs with Apache License 2.0 | 5 votes |
@Override public FileChannel truncate(long size) throws IOException { Util.checkNotNegative(size, "size"); checkOpen(); checkWritable(); synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return this; // AsynchronousCloseException will be thrown } file.writeLock().lockInterruptibly(); try { file.truncate(size); if (position > size) { position = size; } file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return this; }
Example #26
Source File: CsvParser.java From SimpleFlatMapper with MIT License | 5 votes |
private static Reader newReader(File file, Charset charset) throws IOException { //IFJAVA8_START if (true) { FileChannel fileChannel = FileChannel.open(file.toPath()); try { return Channels.newReader(fileChannel, charset.newDecoder(), -1); } catch(Throwable e) { safeClose(fileChannel); throw e; } } //IFJAVA8_END return newReaderJava6(file, charset); }
Example #27
Source File: QrcodeUtils.java From qrcode-utils with Apache License 2.0 | 5 votes |
/** * 将文件转换为字节数组, * 使用MappedByteBuffer,可以在处理大文件时,提升性能 * * @param file 文件 * @return 二维码图片的字节数组 */ private static byte[] toByteArray(File file) { try (FileChannel fc = new RandomAccessFile(file, "r").getChannel();) { MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load(); byte[] result = new byte[(int) fc.size()]; if (byteBuffer.remaining() > 0) { byteBuffer.get(result, 0, byteBuffer.remaining()); } return result; } catch (Exception e) { logger.warn("文件转换成byte[]发生异常!", e); return null; } }
Example #28
Source File: Sources.java From MFL with Apache License 2.0 | 5 votes |
public static Source openFile(File file) throws IOException { checkFileExists(file); // File is larger than the max capacity (2 GB) of a buffer, so we use an InputStream instead. // Unfortunately, this limits us to read a very large file using a single thread :( At some point // we could build more complex logic that can map to more than a single buffer. if (file.length() > Integer.MAX_VALUE) { return openStreamingFile(file); } // File is small enough to be memory-mapped into a single buffer final FileChannel channel = new RandomAccessFile(file, "r").getChannel(); final ByteBuffer buffer = channel .map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size()) .load(); buffer.order(ByteOrder.nativeOrder()); // Wrap as source return new ByteBufferSource(buffer, 512) { @Override public void close() throws IOException { super.close(); Unsafe9R.invokeCleaner(buffer); channel.close(); } }; }
Example #29
Source File: Utils.java From netbeans with Apache License 2.0 | 5 votes |
/** * Copy file in to out * * @param in source file * @param out output file * @throws Exception */ public static void copyFile(File in, File out) { try { out.createNewFile(); FileChannel srcChannel = new FileInputStream(in).getChannel(); FileChannel dstChannel = new FileOutputStream(out).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); } catch (IOException ioe) { ioe.printStackTrace(System.err); } }
Example #30
Source File: HaloDBFile.java From HaloDB with Apache License 2.0 | 5 votes |
private HaloDBFile(int fileId, File backingFile, DBDirectory dbDirectory, IndexFile indexFile, FileType fileType, FileChannel channel, HaloDBOptions options) throws IOException { this.fileId = fileId; this.backingFile = backingFile; this.dbDirectory = dbDirectory; this.indexFile = indexFile; this.fileType = fileType; this.channel = channel; this.writeOffset = Ints.checkedCast(channel.size()); this.options = options; }