public class MappedByteBufferTest {
public static void main(String[] args) throws Exception {
File file = new File("D://db.txt");
long len = file.length();
byte[] ds = new byte[(int) len];
MappedByteBuffer mappedByteBuffer = new FileInputStream(file).getChannel().map(FileChannel.MapMode.READ_ONLY, 0,
len);
for (int offset = 0; offset < len; offset ) {
byte b = mappedByteBuffer.get();
ds[offset] = b;
}
Scanner scan = new Scanner(new ByteArrayInputStream(ds)).useDelimiter(" ");
while (scan.hasNext()) {
System.out.print(scan.next() " ");
}
}
}
public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;
public MappedByteBuffer map(MapMode mode, long position, long size)
throws IOException
{
...省略...
int pagePosition = (int)(position % allocationGranularity);
long mapPosition = position - pagePosition;
long mapSize = size pagePosition;
try {
// If no exception was thrown from map0, the address is valid
addr = map0(imode, mapPosition, mapSize);
} catch (OutOfMemoryError x) {
// An OutOfMemoryError may indicate that we've exhausted memory
// so force gc and re-attempt map
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException y) {
Thread.currentThread().interrupt();
}
try {
addr = map0(imode, mapPosition, mapSize);
} catch (OutOfMemoryError y) {
// After a second OOME, fail
throw new IOException("Map failed", y);
}
}
// On Windows, and potentially other platforms, we need an open
// file descriptor for some mapping operations.
FileDescriptor mfd;
try {
mfd = nd.duplicateForMapping(fd);
} catch (IOException ioe) {
unmap0(addr, mapSize);
throw ioe;
}
assert (IOStatus.checkAll(addr));
assert (addr % allocationGranularity == 0);
int isize = (int)size;
Unmapper um = new Unmapper(addr, mapSize, isize, mfd);
if ((!writable) || (imode == MAP_RO)) {
return Util.newMappedByteBufferR(isize,
addr pagePosition,
mfd,
um);
} else {
return Util.newMappedByteBuffer(isize,
addr pagePosition,
mfd,
um);
}
}
ByteBuffer directByteBuffer = ByteBuffer.allocateDirect(100);
public class ChannelTransfer {
public static void main(String[] argv) throws Exception {
String files[]=new String[1];
files[0]="D://db.txt";
catFiles(Channels.newChannel(System.out), files);
}
private static void catFiles(WritableByteChannel target, String[] files)
throws Exception {
for (int i = 0; i < files.length; i ) {
FileInputStream fis = new FileInputStream(files[i]);
FileChannel channel = fis.getChannel();
channel.transferTo(0, channel.size(), target);
channel.close();
fis.close();
}
}
}
public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;
public class CompositeChannelBuffer extends AbstractChannelBuffer {
private final ByteOrder order;
private ChannelBuffer[] components;
private int[] indices;
private int lastAccessedComponentId;
private final boolean gathering;
public byte getByte(int index) {
int componentId = componentId(index);
return components[componentId].getByte(index - indices[componentId]);
}
...省略...
END 十期推荐 【241期】面试官:你了解JVM中的ZGC垃圾收集器吗? 【242期】面试官:Spring AOP有哪些通知类型,它们的执行顺序是怎样的? 【243期】面试官:什么是前缀索引、为什么要用前缀使用、用在什么场景下? 【244期】万字 图解 Redis,面试不用愁了! 【245期】面试官:MySQL发生死锁有哪些原因,怎么避免? 【246期】面试官:说说你对 RabbitMQ 的理解以及使用它的场景 【247期】记一次Java面试中遇到的三个问题及感悟! 【248期】面试官:你能说几个Java8中Stream对列表去重的方法吗? 【249期】关于Java中的异常,面试可以问的都在这里了! 【250期】关于Mybatis知识点,面试可以问的都在这里了! ? ~