下面列出了怎么用java.nio.channels.FileChannel的API类实例代码及写法,或者点击链接到github查看源代码。
@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));
}
/**
* 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();
}
}
}
}
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));
}
}
}
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());
}
}
@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);
}
@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
}
}
/**
* @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);
}
}
/**
* 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();
}
@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);
}
}
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;
}
/**
* 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;
}
}
/**
* 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();
}
}
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();
}
}
}
/**
* 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);
}
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 );
}
}
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();
}
}
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();
}
}
}
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;
}
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");
}
}
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.");
}
}
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;
}
/**
* Fill a region of a file with a given byte value.
*
* @param fileChannel to fill.
* @param position at which to start writing.
* @param length of the region to write.
* @param value to fill the region with.
*/
public static void fill(final FileChannel fileChannel, final long position, final long length, final byte value)
{
try
{
final byte[] filler;
if (0 != value)
{
filler = new byte[BLOCK_SIZE];
Arrays.fill(filler, value);
}
else
{
filler = FILLER;
}
final ByteBuffer byteBuffer = ByteBuffer.wrap(filler);
fileChannel.position(position);
final int blocks = (int)(length / BLOCK_SIZE);
final int blockRemainder = (int)(length % BLOCK_SIZE);
for (int i = 0; i < blocks; i++)
{
byteBuffer.position(0);
fileChannel.write(byteBuffer);
}
if (blockRemainder > 0)
{
byteBuffer.position(0);
byteBuffer.limit(blockRemainder);
fileChannel.write(byteBuffer);
}
}
catch (final IOException ex)
{
LangUtil.rethrowUnchecked(ex);
}
}
public static void main(String[] args) throws Exception {
Path blah = Files.createTempFile("blah", null);
blah.toFile().deleteOnExit();
initTestFile(blah);
for (int i=0; i<10; i++) {
try (FileChannel fc = (generator.nextBoolean()) ?
FileChannel.open(blah, READ) :
new FileInputStream(blah.toFile()).getChannel()) {
for (int j=0; j<100; j++) {
long newPos = generator.nextInt(1000);
fc.position(newPos);
if (fc.position() != newPos)
throw new RuntimeException("Position failed");
}
}
}
for (int i=0; i<10; i++) {
try (FileChannel fc = (generator.nextBoolean()) ?
FileChannel.open(blah, APPEND) :
new FileOutputStream(blah.toFile(), true).getChannel()) {
for (int j=0; j<10; j++) {
if (fc.position() != fc.size())
throw new RuntimeException("Position expected to be size");
byte[] buf = new byte[generator.nextInt(100)];
fc.write(ByteBuffer.wrap(buf));
}
}
}
Files.delete(blah);
}
/**
* Create a new media server remuxer with auto-detection.
* <p/>
* The input stream is assumed TS.
*
* @param fileChannel The starting FileChannel to be used for writing.
* @param outputFormat The format of the remuxed stream.
* @param isTV Is the incoming stream from a TV Tuner?
* @param mediaServer The media server creating this instance.
*/
public MediaServerRemuxer(FileChannel fileChannel, int outputFormat, boolean isTV, MediaServer.Connection mediaServer)
{
// Any initial size that is not a multiple of 188 is a waste since the extra bytes will not be used.
this(fileChannel,
27888 * TS_ALIGN /* 5MB; one quarter the max size */, outputFormat,
isTV ? MPEGParser2.StreamFormat.ATSC : MPEGParser2.StreamFormat.FREE,
MPEGParser2.SubFormat.UNKNOWN,
MPEGParser2.TuneStringType.CHANNEL,
0, 0, 0, 0, 0,
mediaServer);
}
public static void main(String[] args) throws Throwable {
Path path = Files.createTempFile("LoopingTruncate.tmp", null);
try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
fc.position(FATEFUL_SIZE + 1L);
fc.write(ByteBuffer.wrap(new byte[] {0}));
Thread th = new Thread(() -> {
try {
fc.truncate(FATEFUL_SIZE);
} catch (ClosedByInterruptException ignore) {
} catch (Exception e) {
throw new RuntimeException(e);
}});
th.start();
th.join(TIMEOUT);
if (th.isAlive()) {
System.err.println("=== Stack trace of the guilty thread:");
for (StackTraceElement el : th.getStackTrace()) {
System.err.println("\t" + el);
}
System.err.println("===");
th.interrupt();
th.join();
throw new RuntimeException("Failed to complete on time");
}
} finally {
Files.deleteIfExists(path);
}
}
/**
* Creates new FileChannelMemoryMappedBoundedData, creating a memory mapped file at the given path.
* Each mapped region (= ByteBuffer) will be of the given size.
*/
public static FileChannelMemoryMappedBoundedData createWithRegionSize(Path memMappedFilePath, int regionSize) throws IOException {
checkNotNull(memMappedFilePath, "memMappedFilePath");
checkArgument(regionSize > 0, "regions size most be > 0");
final FileChannel fileChannel = FileChannel.open(memMappedFilePath,
StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
return new FileChannelMemoryMappedBoundedData(memMappedFilePath, fileChannel, regionSize);
}
private void expandIfNecessary(int dataLength) throws IOException {
int elementLength = dataLength + 4;
int remainingBytes = remainingBytes();
if (remainingBytes < elementLength) {
int newLength;
int previousLength = this.fileLength;
do {
remainingBytes += previousLength;
newLength = previousLength << 1;
previousLength = newLength;
} while (remainingBytes < elementLength);
setLength(newLength);
int endOfLastElement = wrapPosition((this.last.position + 4) + this.last.length);
if (endOfLastElement < this.first.position) {
FileChannel channel = this.raf.getChannel();
channel.position((long) this.fileLength);
int count = endOfLastElement - 4;
if (channel.transferTo(16, (long) count, channel) != ((long) count)) {
throw new AssertionError("Copied insufficient number of bytes!");
}
}
if (this.last.position < this.first.position) {
int newLastPosition = (this.fileLength + this.last.position) - 16;
writeHeader(newLength, this.elementCount, this.first.position, newLastPosition);
this.last = new Element(newLastPosition, this.last.length);
} else {
writeHeader(newLength, this.elementCount, this.first.position, this.last.position);
}
this.fileLength = newLength;
}
}
private void releaseLock(FileLock lock) throws IOException {
if (lock != null && lock.isValid()) {
FileChannel lockChannel = lock.channel();
lock.release();
lockChannel.close();
}
}
private boolean inReady() {
try {
return (((in != null) && (in.available() > 0))
|| (ch instanceof FileChannel)); // ## RBC.available()?
} catch (IOException x) {
return false;
}
}
public ByteBuffer getBuffer(DumpIndex idx) throws IOException {
if (cache.containsKey(idx)) {
MappedByteBuffer b = cache.get(idx);
b.position(0);
return b.duplicate().order(b.order());
}
FileChannel channel = dump.getChannel();
MappedByteBuffer res = channel.map(FileChannel.MapMode.READ_ONLY, idx.filePos, idx.size);
res.order(ByteOrder.LITTLE_ENDIAN);
cache.put(idx, res);
return res.duplicate().order(res.order());
}