1. 源起

需要跟踪FileInputStream的Read的Nativie实现,开始走了弯路,Java工程下的FileInputStream实现与Android工程的实现不同。另外,http://blog.chinaunix.net/uid-26926660-id-3326678.html中分析的很好。


2. java.io.FileInputStream

import libcore.io.Libcore;
import libcore.io.Streams;
@Override public int read() throws IOException {
return Streams.readSingleByte(this);
}
@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
return IoBridge.read(fd, buffer, byteOffset, byteCount);
}

继续跟踪Streams.readSingleByte和IoBridge.read

3. libcore.io.Streams

http://grepcode.com/file/repo1.maven.org/maven2/com.google.okhttp/okhttp/20120626/libcore/io/Streams.java

public static int readSingleByte(InputStream in) throws IOException {
byte[] buffer = new byte[1];
int result = in.read(buffer, 0, 1);
return (result != -1) ? buffer[0] & 0xff : -1;
}

这里用了InputStream.read(byte[], int, int),实际上还是调用的FileInputStream.read(byte[], int, int),最后还是调用IoBridge.read(byte[], int, int)

4. libcore.io.ioBridge

https://android.googlesource.com/platform/libcore/+/796f0d5a4e7b83c3efc5e587b6766977dc20b0c3/luni/src/main/java/libcore/io/IoBridge.java

    /**
* java.io thinks that a read at EOF is an error and should return -1, contrary to traditional
* Unix practice where you'd read until you got 0 bytes (and any future read would return -1).
*/
public static int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException {
Arrays.checkOffsetAndCount(bytes.length, byteOffset, byteCount);
if (byteCount == 0) {
return 0;
}
try {
int readCount = Libcore.os.read(fd, bytes, byteOffset, byteCount);
if (readCount == 0) {
return -1;
}
return readCount;
} catch (ErrnoException errnoException) {
if (errnoException.errno == EAGAIN) {
// We return 0 rather than throw if we try to read from an empty non-blocking pipe.
return 0;
}
throw errnoException.rethrowAsIOException();
}
}


5. libcore.io.Libcore

http://grepcode.com/file/repo1.maven.org/maven2/org.robovm/robovm-rt/0.0.2/libcore/io/Libcore.java#Libcore

package libcore.io;

public final class Libcore {
private Libcore() { } public static Os os = new BlockGuardOs(new Posix());
}

到这里有点断片了,BlockGuardOs里面还有一些调用,这里参考了http://blog.chinaunix.net/uid-26926660-id-3326678.html文章,read操作是由Posix实现的。


6. Libcore.io.Posix

https://android.googlesource.com/platform/libcore/+/6c9b5377550a9649ed1532d1fcdfeba116c74ead/luni/src/main/java/libcore/io/Posix.java

public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
if (buffer.isDirect()) {
return readBytes(fd, buffer, buffer.position(), buffer.remaining());
} else {
return readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining());
}
}

到这里就是native方法了。

7. src/main/native/libcore_io_Posix.cpp

https://android.googlesource.com/platform/libcore/+/721ceca2a52a3c27aa751476c8562e1e68088e15/luni/src/main/native/libcore_io_Posix.cpp

#include <unistd.h>
#include "ScopeBytes.h" static jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) {
ScopedBytesRW bytes(env, javaBytes);
if (bytes.get() == NULL) {
return -1;
}
int fd = jniGetFDFromFileDescriptor(env, javaFd);
return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
}

第一:return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));

throwIfMinusOne与TEMP_FAILURE_RETRY是宏定义。

write是<unistd.h>中的C语言库函数

第二:ScopedBytesRW

https://android.googlesource.com/platform/libnativehelper/+/3d9d2148155c2e0b3bf51cd548f58f93d1199a4e/include/nativehelper/ScopedBytes.h

这个文件比较简单,仅仅include了JNIHelper.h,用来区分byte[]s与ByteBuffers的。

class ScopedBytesRW : public ScopedBytes<false> {
public:
ScopedBytesRW(JNIEnv* env, jobject object) : ScopedBytes<false>(env, object) {}
jbyte* get() {
return mPtr;
}
};


8. 本文链接

http://blog.csdn.net/xiaodongrush/article/details/10004997

[Android] FileInputStream跟踪的更多相关文章

  1. android 流程跟踪

    #记录一下 Thread cur_thread = Thread.currentThread(); StackTraceElement stack[] = cur_thread.getStackTra ...

  2. Android内存管理(5)*官方教程:Logcat内存日志各字段含义,查看当前内存快照,跟踪记录内存分配,用adb查看内存情况时各行列的含义,捕获内存快照的3种方法,如何让程序暴漏内存泄漏的方法

    Investigating Your RAM Usage In this document Interpreting Log Messages                 内存分析日志中各消息的含 ...

  3. Android 8.0 功能和 API

    Android 8.0 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 用户体验 通知 在 Android 8.0 中,我们已重新设计通知,以便为管理通知行为和设置提供更轻松和更统一的 ...

  4. Android Studio 3.0 新特性

    最新Android Studio版本是Android Studio 3.0,本文提供了所有新功能和更改的摘要. 所有这些功能都可以在最新的金丝雀版本中发布,但beta测试版本可能尚未提供. 核心IDE ...

  5. Android Mokoid Open Source Project hacking

    /***************************************************************************** * Android Mokoid Open ...

  6. Android Studio 3.0 下载 使用新功能介绍

    谷歌2017发布会更新了挺多内容的,而且也发布了AndroidStudio3.0预览版,一些功能先睹为快.(英语一般,有些翻译不太好) 下载地址 https://developer.android.g ...

  7. Android Studio3.0 新特性 ~ New Features in Android Studio Preview (译文)

    原文地址:https://developer.android.google.cn/studio/preview/features/index.html 最新Android Studio版本是Andro ...

  8. [异常特工]android常见bug跟踪

    前言 对app的线上bug的收集(友盟.云捕等)有时会得到这样的异常堆栈信息:没有一行代码是有关自身程序代码的.这使得对bug的解决无从下手,根据经验,内存不足OOM,Dialog关闭,ListVie ...

  9. android应用安全——(数据抓包)跟踪监控android数据包

    转载博客:http://blog.csdn.net/xyz_lmn/article/details/8808169 web开发中Chrome.IE.firefox等浏览器都自带提供了插件帮助开发者跟踪 ...

随机推荐

  1. Qt 继承QWidget setstylesheet解决

    void myMainWidget::paintEvent(QPaintEvent * e) { QStyleOption opt; opt.init(this); QPainter p(this); ...

  2. poj1061

    构造方程 (x + m * s) - (y + n * s) = k * l(k = 0, 1, 2,...) 变形为 (n-m) * s + k * l = x - y.即转化为模板题,a * x ...

  3. Mysql的Debug模式实现

    前一段领导开发了一个内核的模块,测试的过程中,发现导致MYSQL客户端无法连接服务器. 经过查询文档,追根溯源,终于找到了MYSQL实现链接客户端的代码,在源文件sql-common/client.c ...

  4. HDU 2202 最大三角形

    题解:先算出凸包,然后枚举凸包上的点计算即可 #include <cstdio> #include <cmath> #include <cstdlib> #incl ...

  5. 【浏览器那些基础】Android平台有那些CPU类型

    在编译Android应用的时候,需要配置支持的CPU类型,而目前Android支持的CPU类型包含了ARM和X86,所以在编译前需要指定CPU类型(不同的cpu的特性不一样): X86系列的 expo ...

  6. block 解析 - 局部变量

    局部变量 block内使用局部变量,一般都是截获变量(只读),截获离block初始化最近的一次的值. 引用官方文档: Stack (non-static) variables local to the ...

  7. Visual Studio warning MSB3270:There was a mismatch between the processor architecture of the project being built "MSIL"

    Problem: There was a mismatch between the processor architecture of the project being built "MS ...

  8. 使用约束给tableview添加暂无数据的提示

    在tableview没有数据的时候,我们经常需要在tableview的正中间插入一张图片,来提示用户暂无数据 /** * 添加一个view,提示暂无数据 */ - (void)addPromptVie ...

  9. 「Foundation」结构体

    一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...

  10. HDU 2023题解分析

    我想说这道题我还没弄明白我错哪了,交了20多遍一直都是Runtime Error,改了N次还是不对,后来搜了一下,说是数组开小了,又把数组开大,还不对,又改发现一个平均值求错,再改,还不对,洗洗睡吧. ...