首页 > 编程语言 >Java的readBytes是怎么实现的?

Java的readBytes是怎么实现的?

时间:2023-07-30 10:55:43浏览次数:33  
标签:Java 实现 bytes len return jint fd env readBytes

1.前言

众所周知,Java是一门跨平台语言,针对不同的操作系统有不同的实现。本文从一个非常简单的api调用来看看Java具体是怎么做的.

2.源码分析

从FileInputStream.java中看到readBytes最后是native调用

/**
     * Reads a subarray as a sequence of bytes.
     * @param b the data to be written
     * @param off the start offset in the data
     * @param len the number of bytes that are written
     * @exception IOException If an I/O error has occurred.
     */
    private native int readBytes(byte b[], int off, int len) throws IOException; // native调用

    /**
     * Reads up to <code>b.length</code> bytes of data from this input
     * stream into an array of bytes. This method blocks until some input
     * is available.
     *
     * @param      b   the buffer into which the data is read.
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the file has been reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public int read(byte b[]) throws IOException {
        return readBytes(b, 0, b.length);
    }

从jdk源码中,我们找到FileInputStream.c(/jdk/src/share/native/java/io),此文件定义了对应文件的native调用.

// FileInputStream.c

JNIEXPORT jint JNICALL
Java_java_io_FileInputStream_readBytes(JNIEnv *env, jobject this,
        jbyteArray bytes, jint off, jint len) {
    return readBytes(env, this, bytes, off, len, fis_fd);
}

我们观察下当前的目录,可以看到java 对典型的四种unix like的系统(bsd, linux, macosx, solaris), 以及windows 提供了特殊实现。share是公用部分。

在头部获取文件fd field (fd 是非负正整数,用来标识打开文件)

// FileInputStream.c

JNIEXPORT void JNICALL
Java_java_io_FileInputStream_initIDs(JNIEnv *env, jclass fdClass) {
    fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;"); /* fd field,后面用来获取 fd */
}

 继续调用readBytes

// ioutil.c

jint
readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
          jint off, jint len, jfieldID fid)
{
    jint nread;
    char stackBuf[BUF_SIZE];
    char *buf = NULL;
    FD fd;

    if (IS_NULL(bytes)) {
        JNU_ThrowNullPointerException(env, NULL);
        return -1;
    }

    if (outOfBounds(env, off, len, bytes)) { /* 越界判断 */
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
        return -1;
    }

    if (len == 0) {
        return 0;
    } else if (len > BUF_SIZE) {
        buf = malloc(len); /* 缓冲区不足,动态分配内存 */
        if (buf == NULL) {
            JNU_ThrowOutOfMemoryError(env, NULL);
            return 0;
        }
    } else {
        buf = stackBuf;
    }

    fd = GET_FD(this, fid); /* 获取fd */
    if (fd == -1) {
        JNU_ThrowIOException(env, "Stream Closed");
        nread = -1;
    } else {
        nread = IO_Read(fd, buf, len); /* 执行read,系统调用 */
        if (nread > 0) {
            (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
        } else if (nread == -1) {
            JNU_ThrowIOExceptionWithLastError(env, "Read error");
        } else { /* EOF */
            nread = -1;
        }
    }

    if (buf != stackBuf) {
        free(buf); /* 失败释放内存 */
    }
    return nread;
}

我们继续看看IO_Read的实现,是个宏定义

#define IO_Read handleRead

handleRead有两种实现

solaris实现:

// /jdk/src/solaris/native/java/io/io_util_md.c

ssize_t
handleRead(FD fd, void *buf, jint len)
{
    ssize_t result;
    RESTARTABLE(read(fd, buf, len), result);
    return result;
}

/*
 * Retry the operation if it is interrupted
 */
#define RESTARTABLE(_cmd, _result) do { \
    do { \
        _result = _cmd; \
    } while((_result == -1) && (errno == EINTR)); \ /* 如果是中断,则不断重试,避免进程调度等待*/
} while(0)

read方法可以参考unix man page

windows实现:

// jdk/src/windows/native/java/io/io_util_md.c

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;
}

3.java异常初探

// jdk/src/share/native/common/jni_util.c

/**
 * Throw a Java exception by name. Similar to SignalError.
 */
JNIEXPORT void JNICALL
JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
{
    jclass cls = (*env)->FindClass(env, name);

    if (cls != 0) /* Otherwise an exception has already been thrown */
        (*env)->ThrowNew(env, cls, msg); /* 调用JNI 接口*/
}

/* JNU_Throw common exceptions */

JNIEXPORT void JNICALL
JNU_ThrowNullPointerException(JNIEnv *env, const char *msg)
{
    JNU_ThrowByName(env, "java/lang/NullPointerException", msg);
}

最后是调用JNI:

// hotspot/src/share/vm/prims/jni.h

jint ThrowNew(jclass clazz, const char *msg) {
        return functions->ThrowNew(this, clazz, msg);
    }

jint (JNICALL *ThrowNew)
      (JNIEnv *env, jclass clazz, const char *msg);

4.总结

很多高级语言,有着不同的编程范式,但是归根到底还是(c语言)系统调用,c语言能够在更低的层面做非常多的优化。如果我们了解了这些底层的系统调用,就能看到问题的本质。

本文没有对JNI 做深入分析,后续继续解析。

5.参考

https://man7.org/linux/man-pages/man2/read.2.html

标签:Java,实现,bytes,len,return,jint,fd,env,readBytes
From: https://www.cnblogs.com/darcy-yuan/p/17591027.html

相关文章

  • python单例模式实现
    classSingleton(object):​  def__new__(cls,*args,**kwargs):    ifnothasattr(cls,'_instance'):      cls._instance=super(Singleton,cls).__new__(cls)    returncls._instance 注:__new__方法用于生成类实例,__new__应返回一个......
  • mybatis用注解如何实现模糊查询
    在MyBatis中使用注解实现模糊查询非常简单,只需要在相应的SQL语句中使用like关键字即可。以下是实现模糊查询的示例代码:@Select("SELECT*FROMusersWHEREnameLIKE'%${keyword}%'")List<User>findUsersByName(@Param("keyword")Stringkeyword);上述代码用@Selec......
  • PHP调用API接口的方法及实现
    随着互联网、云计算和大数据时代的到来,越来越多的应用程序需要调用第三方的API接口来获取数据,实现数据互通和协同工作。PHP作为一种常用的服务器端语言,也可以通过调用API接口来实现不同系统的数据交互和整合。本文将介绍PHP调用API接口的方法及实现过程。一、API接口简介API(Appl......
  • 【Java】《2小时搞定多线程》个人笔记
    简介基于慕课网站上的一个一元钱课程《2小时搞定多线程》的个人笔记。线程的起源我们先来看看网络中关于线程起源的说明,理解线程的来龙去脉对于掌握多线程有一定帮助。此部分内容整理自下面两篇网络博客:#线程是什么#线程的起源线程的起源与计算机的发展息息相关。早期的计算机系......
  • Quartz实战:基于Quartz实现定时任务的动态调度,实现定时任务的增删改查
    文章目录一、Quartz基础二、使用Quartz实现定时任务的动态调度1、使用Quartz-jobStore持久化2、前端页面实现效果图3、自定义job表4、增删改查Controller5、Quartz工具类6、测试任务类7、springboot启动初始化定时任务8、自定义JobFactory,使Task注册为Bean9、省略的内容10、总结......
  • VarHandle:Java9中保证变量读写可见性、有序性、原子性利器
    文章目录一、什么是VarHandle0、JMM1、jdk9之前无锁技术的实现二、VarHandle使用1、VarHandle快速上手2、VarHandle常用方法3、实战案例1:解决可见性(比volatile轻量)4、实战案例2:解决指令重排序(比volatile轻量)(1)案例分析:partialordering(2)案例分析:totalordering一、什么是VarHand......
  • 系统可扩展性的设计与实现
    总体思路系统可扩展性是指能够低成本、高质量地在现有系统中添加新功能和优化现有功能。可扩展设计的核心原则是:开闭原则。对新增开放,对修改关闭。也就是说,后续有新的需求,只需要新增组件,而不需要修改现有组件或逻辑(除非实现有BUG)。要保证一个大的业务模块的可扩展性,有效的策略......
  • Dubbo(一)_Java_SPI
    什么是SPI?Dubbo的源码中大量涉及了JavaSPI设计思想,所以理解SPI对理解Dubbo源码有很大帮助。JavaSPI全称JavaServiceProviderInterface,是Java提供的一种服务提供者发现机制。其核心功能是通过接口找到其实现类。在实际运用中,主要用在程序启动或者运行时,通过SPI机......
  • 4.JAVA的特性和优势
    4.JAVA的特性和优势跨平台/可移植性这是Java的核心优势。Java在设计时就很注重移植和跨平台性。比如:Java的int永远都是32位。不像C++可能是16,32,可能是根据编译器厂商规定的变化。这样的话程序的移植就会非常麻烦。安全性Java适合于网络/分布式环境,为了达到这个目标,在安全性......
  • JAVA体系结构
    JAVA体系结构JavaSE(Java Standard Edition):标准版,定位在个人计算机上的应用这个版本是Java平台的核心,它提供了非常丰富的API来开发一般个人计算机上的应用程序,包括用户界面接口AWT及Swing,网络功能与国际化、图像处理能力以及输入输出支持等。在上世纪90年代末互联网上大放异......