use of java.nio.MappedByteBuffer in project ceylon-compiler by ceylon.
the class CeylonDocToolTests method assertMatchInFile.
protected void assertMatchInFile(File destDir, String path, Pattern pattern, GrepAsserter asserter) throws Exception {
assertFileExists(destDir, path);
Charset charset = Charset.forName("UTF-8");
File file = new File(destDir, path);
FileInputStream stream = new FileInputStream(file);
try {
FileChannel channel = stream.getChannel();
try {
MappedByteBuffer map = channel.map(MapMode.READ_ONLY, 0, channel.size());
CharBuffer chars = charset.decode(map);
Matcher matcher = pattern.matcher(chars);
asserter.makeAssertions(matcher);
} finally {
channel.close();
}
} finally {
stream.close();
}
}
use of java.nio.MappedByteBuffer in project smartmodule by carozhu.
the class FileEnDecryptHelper method encrypt.
/**
* 加密
*
* @param strFile
* 源文件绝对路径
* @return
*/
private boolean encrypt(String strFile) {
int len = REVERSE_LENGTH;
try {
File f = new File(strFile);
RandomAccessFile raf = new RandomAccessFile(f, "rw");
long totalLen = raf.length();
if (totalLen < REVERSE_LENGTH)
len = (int) totalLen;
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, REVERSE_LENGTH);
byte tmp;
for (int i = 0; i < len; ++i) {
byte rawByte = buffer.get(i);
tmp = (byte) (rawByte ^ i);
buffer.put(i, tmp);
}
buffer.force();
buffer.clear();
channel.close();
raf.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of java.nio.MappedByteBuffer in project jdk8u_jdk by JetBrains.
the class FileChannelImpl method transferToTrustedChannel.
private long transferToTrustedChannel(long position, long count, WritableByteChannel target) throws IOException {
boolean isSelChImpl = (target instanceof SelChImpl);
if (!((target instanceof FileChannelImpl) || isSelChImpl))
return IOStatus.UNSUPPORTED;
// Trusted target: Use a mapped buffer
long remaining = count;
while (remaining > 0L) {
long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
try {
MappedByteBuffer dbb = map(MapMode.READ_ONLY, position, size);
try {
// ## Bug: Closing this channel will not terminate the write
int n = target.write(dbb);
assert n >= 0;
remaining -= n;
if (isSelChImpl) {
// one attempt to write to selectable channel
break;
}
assert n > 0;
position += n;
} finally {
unmap(dbb);
}
} catch (ClosedByInterruptException e) {
// to be thrown after closing this channel.
assert !target.isOpen();
try {
close();
} catch (Throwable suppressed) {
e.addSuppressed(suppressed);
}
throw e;
} catch (IOException ioe) {
// Only throw exception if no bytes have been written
if (remaining == count)
throw ioe;
break;
}
}
return count - remaining;
}
use of java.nio.MappedByteBuffer in project jdk8u_jdk by JetBrains.
the class FileChannelImpl method transferFromFileChannel.
private long transferFromFileChannel(FileChannelImpl src, long position, long count) throws IOException {
if (!src.readable)
throw new NonReadableChannelException();
synchronized (src.positionLock) {
long pos = src.position();
long max = Math.min(count, src.size() - pos);
long remaining = max;
long p = pos;
while (remaining > 0L) {
long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
// ## Bug: Closing this channel will not terminate the write
MappedByteBuffer bb = src.map(MapMode.READ_ONLY, p, size);
try {
long n = write(bb, position);
assert n > 0;
p += n;
position += n;
remaining -= n;
} catch (IOException ioe) {
// Only throw exception if no bytes have been written
if (remaining == max)
throw ioe;
break;
} finally {
unmap(bb);
}
}
long nwritten = max - remaining;
src.position(pos + nwritten);
return nwritten;
}
}
use of java.nio.MappedByteBuffer in project sketches-core by DataSketches.
the class MemoryMappedFile method getInstance.
/**
* Factory method for creating a memory mapping a file.
*
* <p>Memory maps a file directly in off heap leveraging native map0 method used in
* FileChannelImpl.c. The owner will have read write access to that address space.</p>
*
* @param file File to be mapped
* @param position Memory map starting from this position in the file
* @param len Memory map at most len bytes > 0 starting from {@code position}
* @return A new MemoryMappedFile
* @throws Exception file not found or RuntimeException, etc.
*/
@SuppressWarnings("resource")
public static MemoryMappedFile getInstance(final File file, final long position, final long len) throws Exception {
checkPositionLen(position, len);
final RandomAccessFile raf = new RandomAccessFile(file, "rw");
final FileChannel fc = raf.getChannel();
final long nativeBaseAddress = map(fc, position, len);
final long capacityBytes = len;
// len can be more than the file.length
raf.setLength(len);
final MappedByteBuffer mbb = createDummyMbbInstance(nativeBaseAddress);
return new MemoryMappedFile(raf, mbb, nativeBaseAddress, capacityBytes);
}
Aggregations