美文网首页
JVM-FileOutputStream源码

JVM-FileOutputStream源码

作者: YDDMAX_Y | 来源:发表于2018-05-07 23:22 被阅读0次

OutPutStream的write方法的返回值为Void,但是操作系统的write接口返回值为int,下面以FileOutputStream为例看下是怎么封装的。
1.FileOutputStream.write

public void write(byte b[]) throws IOException {
        writeBytes(b, 0, b.length, append);
    }
  1. FileOutputStream.writeBytes
 /**
     * Writes a sub array 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
     * @param append {@code true} to first advance the position to the
     *     end of file
     * @exception IOException If an I/O error has occurred.
     */
    private native void writeBytes(byte b[], int off, int len, boolean append)
        throws IOException;
  1. 最后调用的native方法
    文件名:/jdk/src/share/native/java/io/io_util.c
void
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
           jint off, jint len, jboolean append, jfieldID fid)
{
    jint n;
    char stackBuf[BUF_SIZE];
    char *buf = NULL;
    FD fd;

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

    if (outOfBounds(env, off, len, bytes)) {
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
        return;
    }

    if (len == 0) {
        return;
    } else if (len > BUF_SIZE) {
        buf = malloc(len);
        if (buf == NULL) {
            JNU_ThrowOutOfMemoryError(env, NULL);
            return;
        }
    } else {
        buf = stackBuf;
    }

    (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);

    if (!(*env)->ExceptionOccurred(env)) {
        off = 0;
        while (len > 0) {
            fd = GET_FD(this, fid);
            if (fd == -1) {
                JNU_ThrowIOException(env, "Stream Closed");
                break;
            }
            if (append == JNI_TRUE) {
                n = IO_Append(fd, buf+off, len);
            } else {
                n = IO_Write(fd, buf+off, len);
            }
            if (n == -1) {
                JNU_ThrowIOExceptionWithLastError(env, "Write error");
                break;
            }
            off += n;
            len -= n;
        }
    }
    if (buf != stackBuf) {
        free(buf);
    }
}

相关文章

  • JVM-FileOutputStream源码

    OutPutStream的write方法的返回值为Void,但是操作系统的write接口返回值为int,下面以Fi...

  • iOS-OC相关源码下载和OC代码转C++/汇编/LVVM

    目录 OC相关源码下载----objc源码----malloc源码----Runloop源码----GCD源码OC...

  • go run

    源码文件 Golang源码文件分为三种类型,分别是命令源码文件、库源码文件、测试源码文件 命令源码文件 命令源码文...

  • 文章目录汇总

    Java 源码 String源码-Java源码系列之StringInteger、Long源码-Java源码系列之I...

  • 小米便签产品级的源码

    小米便签产品级的源码 源码简介 小米便签Android源码,可以再桌面创建widget。 源码截图 源码下载 源码下载

  • 命令源码文件

    包是有源码文件组成,源码文件分为三种,库源码文件,命令源码文件,测试源码文件 命令源码文件 定义:命令源码文件是程...

  • @@程序员——看完源码记不住?掌握这套方法,Alibaba不会少

    都说大厂面试必问源码,可很多人看完MMKV 源码、Handler 源码、Binder 源码、OkHttp 源码等源...

  • 【Tip】Go语言学习:命令源码文件

    源码文件组织形式 Go语言以代码包的形式组织源码文件。有三种类型的源码文件:命令源码、库源码和测试源码。命令源码即...

  • 源码学习之Mybatis

    Mybatis源码解读 1 源码下载 学习源码之前需要先将源码下载下来,这里需要下载mybatis源码和mybat...

  • HashMap源码

    HashMap的源码,基于jdk1.7Map的源码 AbstractMap的源码 HashMap的源码

网友评论

      本文标题:JVM-FileOutputStream源码

      本文链接:https://www.haomeiwen.com/subject/fbvbrftx.html