`

system.println的源码实现

    博客分类:
  • java
阅读更多

println方法:
 public void println(String x) {
	synchronized (this) {
	    print(x);
	    newLine();
	}
    }


其实主要是调用了print方法:
  public void print(String s) {
	if (s == null) {
	    s = "null";
	}
	write(s);
    }



  private void write(String s) {
	try {
	    synchronized (this) {
		ensureOpen();
		textOut.write(s);
		textOut.flushBuffer();
		charOut.flushBuffer();
		if (autoFlush && (s.indexOf('\n') >= 0))
		    out.flush();
	    }
	}
	catch (InterruptedIOException x) {
	    Thread.currentThread().interrupt();
	}
	catch (IOException x) {
	    trouble = true;
	}
    }



 private OutputStreamWriter charOut;


 public void write(String str) throws IOException {
	write(str, 0, str.length());
    }


public void write(String str, int off, int len) throws IOException {
	synchronized (lock) {
	    char cbuf[];
	    if (len <= writeBufferSize) {
		if (writeBuffer == null) {
		    writeBuffer = new char[writeBufferSize];
		}
		cbuf = writeBuffer;
	    } else {	// Don't permanently allocate very large buffers.
		cbuf = new char[len];
	    }
	    str.getChars(off, (off + len), cbuf, 0);
	    write(cbuf, 0, len);
	}
    }



 public void write(char cbuf[], int off, int len) throws IOException {
	synchronized (lock) {
	    ensureOpen();
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
                throw new IndexOutOfBoundsException();
            } else if (len == 0) {
                return;
            } 

	    if (len >= nChars) {
		/* If the request length exceeds the size of the output buffer,
		   flush the buffer and then write the data directly.  In this
		   way buffered streams will cascade harmlessly. */
		flushBuffer();
		out.write(cbuf, off, len);
		return;
	    }

	    int b = off, t = off + len;
	    while (b < t) {
		int d = min(nChars - nextChar, t - b);
		System.arraycopy(cbuf, b, cb, nextChar, d);
		b += d;
		nextChar += d;
		if (nextChar >= nChars)
		    flushBuffer();
	    }
	}
    }

看到write里是用到flushBuffer的。

 void flushBuffer() throws IOException {
	synchronized (lock) {
	    ensureOpen();
	    if (nextChar == 0)
		return;
	    out.write(cb, 0, nextChar);
	    nextChar = 0;
	}
    }

可以看到BufferedWriter做的事情其实就是把一个string转为char数组,然后事情是由
private OutputStreamWriter charOut;
来做的

在这里两者产生关联
 private void init(OutputStreamWriter osw) {
	this.charOut = osw;
	this.textOut = new BufferedWriter(osw);
    }


charOut.flushBuffer();


  void flushBuffer() throws IOException {
	se.flushBuffer();
    }



 public void flushBuffer() throws IOException {
  102           synchronized (lock) {
  103               if (isOpen())
  104                   implFlushBuffer();
  105               else
  106                   throw new IOException("Stream closed");
  107           }
  108       }


再往下就发现调用的是
 out.write(bb.array(), bb.arrayOffset() + pos, rem);
out是OutputStream

再然后就比较纠结了  唉 看代码这么累  后面的部分还是以后再去看了。
0
0
分享到:
评论

相关推荐

    细聊java中的System.out.println()

    我们从System的源码中可以了解到System是java中的一个自定义的类,位于java.lang包下面。 Out是System里面的一个静态成员,他是java.io.PrintStream类的引用。他是个不可更改的类方法。并且是通过Static修饰的。...

    socket应用小程序

    几个很小的socket程序(源码)给其中一个小例: package cn.com.socket; import java.io.*; import java.net.*; public class ServerSocketThread extends Thread{ private Socket socket; private ...

    秋招Java-面试官就System.out.println()考了我半个小时?

    去年秋招面试我被问及,你如何理解System.out.println() ? 学了这么久的面向对象编程,那如何用一行代码体现呢? 如果你能自己读懂System.out.println(),就真正了解了Java面向对象编程的含义 面向对象编程即创建...

    一个完整可用的证书签名(验签),加密(解密)java源码

    System.err.println("私钥签名——公钥验证签名"); // 产生签名 String sign = CertificateUtils.signToBase64(data.getBytes("utf-8"), keyStorePath, alias, password); System.out.println("私钥签名:" + ...

    Java开发新猜数字小游戏,使用Git进行源码管理,使用单元测试工具JUnit对各个方法进行单元测试

    Java开发新猜数字小游戏,使用Git进行源码管理,使用单元测试工具JUnit对各个方法进行单元测试 public static void main(String[] args) { System.out.println("|----新猜数字游戏-----|"); while(true) { ...

    用java写的凯撒加密器源码

    System.out.println("******************欢 迎 使 用凯撒加密器******************"); Scanner input = new Scanner(System.in); System.out.print("\n请选择操作(1.加密,2.解密): "); int operator = ...

    JAVAMD5源代码(JAVA版本的md5类包)

    使用netbeans 开发 在Java中提供MD5加密 ... System.out.println(mad.toMd5("0.0")); System.out.println(mad.toMd5(0.0D)); System.out.println(mad.toMd5(0.0F)); System.out.println(mad.toMd5(1)); } }

    java培训教程教学课件

    System.out.println((f * b) + " + " + (i / c) + " - " + (d * s)); System.out.println("result = " + result); } } 《Java就业培训教程》P35源码 程序清单:TestScope.java public class TestScope { ...

    经典java组合算法源码--TryCombination(算法源码)

    System.out.println("对整数数组进行组合:C(n,n)"); int[] intArray=new int[4]; for(int i=0;i&lt;intArray.length;i++){ intArray[i]=i+1; } System.out.println("对整数数组进行组合:C(4,4)"); ...

    java简单的图书管理代码

    System.out.println("**********"); System.out.println("查书请按\t1"); System.out.println("删除请按\t2"); System.out.println("退出请按\t3"); System.out.println("**********"); int m = ...

    Java创建和关闭数据库连接的方法附代码.rar

     System.out.println(dbmd.getDatabaseProductName()); //获取数据库名称  System.out.println(dbmd.getDatabaseProductVersion()); //获取数据库版本号  System.out.println(dbmd.getDriverName()); //获取...

    Java重载和重写的区别--源码实例

    System.out.println("method fun in OverloadTest, no parameter"); } public void fun(float f) { System.out.println("method fun in OverloadTest, parameter type: float"); } public void fun(int i){ ...

    Java连接数据库和断开数据库讲解 代码.rar

     System.out.println(dbmd.getDatabaseProductName()); //获取数据库名称  System.out.println(dbmd.getDatabaseProductVersion()); //获取数据库版本号  System.out.println(dbmd.getDriverName()); //获取JDBC...

    java md5 加密源码

    使用java语言实现MD5的加密算法 测试通过 public static void main(String args[]) { MD5 md5 = new MD5(); if (Array.getLength(args) == 0) { System.out.println("MD5 Test suite:"); System.out....

    音乐搜索API 带源码

    带源码的音乐搜索APIjar包,一导入。 将这些jar包导入工程即可,使用方法如下 package music_basic; import java.util.List; import org.apache.commons.lang.StringUtils; import music.MusicBasic; import ...

    Java小游戏源代码

    System.out.println("请猜一下电脑随机生成的数字为(0-9)"); person = in.nextInt(); if (person ) { System.out.println("猜小了!在" + person + "至8之间"); } else if (person &gt; computer) { ...

    jedis调用redis源码Demo

    jedis调用redis源码Demo,直接导入工程就可以运行。 redis目录下有redis安装文件和入门电子书 redis64-2.8.9.zip redisbin_x32.zip Redis入门指南(JB51.NET).pdf Redis设计与实现.pdf src: FirestJedisTest.java ...

    编写简单的代理服务器(java源码)

    System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } ...

    javazhuabaoshili.rar

    Java抓包工具及测试实例源码package netPackage; /******************* * JpcapTip.java */ //import java.net.NetworkInterface; //import java.net.InetAddress; //import java.net.UnknownHostException; ...

    石头剪子布源码

    System.out.println("----猜拳游戏----"); System.out.println("请出拳(1.剪刀 2 .石头 3.布)"); int person=in.nextInt(); int computer=(int)(Math.random()*3)+1; String Marks="拳头";//这是做一...

Global site tag (gtag.js) - Google Analytics