java file nio_Java文件NIO读取的本质——FileInputStream与FileChannel对比

仓促成文,还请指正。

FileInputStream典型代码

public static void main(String[] args) {

System.out.println(System.getProperty("user.dir"));

File file = new File(System.getProperty("user.dir") + "/src/oio/file.txt");

System.out.println("file name: " + file.getName());

InputStream inputStream = null;

try {

inputStream = new FileInputStream(file);

byte[] bytes = new byte[(int) file.length()];

int len = inputStream.read(bytes);

System.out.println("bytes len :" + len + " detail: " + new String(bytes));

} catch (IOException e) {

e.printStackTrace();

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

FileChannel典型代码

public class NIOTest {

public static void main(String[] args) throws IOException {

ByteBuffer byteBuffer = ByteBuffer.allocate(4);//①

Path path = Paths.get(System.getProperty("user.dir") + "/assets/file.txt");

FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ);//②

int len = fileChannel.read(byteBuffer);//③

while (len != -1) {

byteBuffer.flip();//④

while (byteBuffer.hasRemaining()){

System.out.print((char) byteBuffer.get());//⑤

}

byteBuffer.clear();//⑥

len = fileChannel.read(byteBuffer);//⑦

}

}

}

FileInputStream和FileChannel的深度分析

FileInputStream的read方法,调用了native的read0

jint

readSingle(JNIEnv *env, jobject this, jfieldID fid) {

jint nread;

char ret;

FD fd = GET_FD(this, fid);

if (fd == -1) {

JNU_ThrowIOException(env, "Stream Closed");

return -1;

}

nread = IO_Read(fd, &ret, 1);

if (nread == 0) { /* EOF */

return -1;

} else if (nread == -1) { /* error */

JNU_ThrowIOExceptionWithLastError(env, "Read error");

}

return ret & 0xFF;

}

核心是IO_Read方法。

#define IO_Read handleRead

JNIEXPORT

jint

handleRead(FD fd, void *buf, jint len)

{

DWORD read = 0;

BOOL result = 0;

HANDLE h = (HANDLE)fd;

if (h == INVALID_HANDLE_VALUE) {

return -1;

}

result = ReadFile(h, /* File handle to read */

buf, /* address to put data */

len, /* number of bytes to read */

&read, /* number of bytes read */

NULL); /* no overlapped struct */

if (result == 0) {

int error = GetLastError();

if (error == ERROR_BROKEN_PIPE) {

return 0; /* EOF */

}

return -1;

}

return (jint)read;

}

核心方法是ReadFile方法。

FileChannel的read方法

使用FIleChannelImpl作为FileChannel的实现类,read方法:

public int read(ByteBuffer dst) throws IOException {

ensureOpen();

if (!readable)

throw new NonReadableChannelException();

synchronized (positionLock) {

int n = 0;

int ti = -1;

try {

begin();

ti = threads.add();

if (!isOpen())

return 0;

do {

n = IOUtil.read(fd, dst, -1, nd);

} while ((n == IOStatus.INTERRUPTED) && isOpen());

return IOStatus.normalize(n);

} finally {

threads.remove(ti);

end(n > 0);

assert IOStatus.check(n);

}

}

}

核心方法是IOUtil.read。

进入IOUtil类:

static int read(FileDescriptor fd, ByteBuffer dst, long position,

NativeDispatcher nd)

throws IOException

{

if (dst.isReadOnly())

throw new IllegalArgumentException("Read-only buffer");

if (dst instanceof DirectBuffer)

return readIntoNativeBuffer(fd, dst, position, nd);

// Substitute a native buffer

ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());

try {

int n = readIntoNativeBuffer(fd, bb, position, nd);

bb.flip();

if (n > 0)

dst.put(bb);

return n;

} finally {

Util.offerFirstTemporaryDirectBuffer(bb);

}

}

核心方法是readIntoNativeBuffer。

进入readIntoNativeBuffer方法

private static int readIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,

long position, NativeDispatcher nd)

throws IOException

{

int pos = bb.position();

int lim = bb.limit();

assert (pos <= lim);

int rem = (pos <= lim ? lim - pos : 0);

if (rem == 0)

return 0;

int n = 0;

if (position != -1) {

n = nd.pread(fd, ((DirectBuffer)bb).address() + pos,

rem, position);

} else {

n = nd.read(fd, ((DirectBuffer)bb).address() + pos, rem);

}

if (n > 0)

bb.position(pos + n);

return n;

}

核心方法是nd.pread(或nd.read,本质上一样)。这里的nd是抽象类sun.nio.ch.NativeDispatcher,具体类是sun.nio.ch.FileDispatcherImpl。

进入sun.nio.ch.FileDispatcherImpl类:

int read(FileDescriptor var1, long var2, int var4) throws IOException {

return read0(var1, var2, var4);

}

最终进入了一个native的read0方法。

JNIEXPORT jint JNICALL

Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo,

jlong address, jint len)

{

DWORD read = 0;

BOOL result = 0;

HANDLE h = (HANDLE)(handleval(env, fdo));

if (h == INVALID_HANDLE_VALUE) {

JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");

return IOS_THROWN;

}

result = ReadFile(h, /* File handle to read */

(LPVOID)address, /* address to put data */

len, /* number of bytes to read */

&read, /* number of bytes read */

NULL); /* no overlapped struct */

if (result == 0) {

int error = GetLastError();

if (error == ERROR_BROKEN_PIPE) {

return IOS_EOF;

}

if (error == ERROR_NO_DATA) {

return IOS_UNAVAILABLE;

}

JNU_ThrowIOExceptionWithLastError(env, "Read failed");

return IOS_THROWN;

}

return convertReturnVal(env, (jint)read, JNI_TRUE);

}

核心方法是ReadFile方法。

结论

FileInputStream和FileChannel最终均调用了native的ReadFile方法,本质是一样的!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值