如何看java源码,java源码怎么用

  • 编辑: 壤驷恺歌
  • 2019-11-18 09:44:54
  • 人阅读
java买源码,Java 1.InputStreamReader源代码(基于jdkg.7.40)Packagejava中的InputStreamReader和OutputStreamWriter源代码。arraylist源码...

Java中的InputStreamReader和OutputStreamWriter源码

1. InputStreamReader 源码(基于jdk1.7.40)

package java.io; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import sun.nio.cs.StreamDecoder; // 将字节输入流转换成字符输入流 public class InputStreamReader extends Reader { private final StreamDecoder sd; // 根据in创建InputStreamReader,使用默认的编码 public InputStreamReader(InputStream in) {super(in);try {sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object} catch (UnsupportedEncodingException e) {// The default encoding should always be availablethrow new Error(e);} } // 根据in创建InputStreamReader,使用编码charsetName(编码名) public InputStreamReader(InputStream in, String charsetName)throws UnsupportedEncodingException {super(in);if (charsetName == null)throw new NullPointerException(charsetName);sd = StreamDecoder.forInputStreamReader(in, this, charsetName); } // 根据in创建InputStreamReader,使用编码cs public InputStreamReader(InputStream in, Charset cs) {super(in);if (cs == null)throw new NullPointerException(charset);sd = StreamDecoder.forInputStreamReader(in, this, cs); } // 根据in创建InputStreamReader,使用解码器dec public InputStreamReader(InputStream in, CharsetDecoder dec) {super(in);if (dec == null)throw new NullPointerException(charset decoder);sd = StreamDecoder.forInputStreamReader(in, this, dec); } // 获取解码器 public String getEncoding() {return sd.getEncoding(); } // 读取并返回一个字符 public int read() throws IOException {return sd.read(); } // 将InputStreamReader中的数据写入cbuf中,从cbuf的offset位置开始写入,写入长度是length public int read(char cbuf[], int offset, int length) throws IOException {return sd.read(cbuf, offset, length); } // 能否从InputStreamReader中读取数据 public boolean ready() throws IOException {return sd.ready(); } // 关闭InputStreamReader public void close() throws IOException {sd.close(); } }

2. OutputStreamWriter 源码(基于jdk1.7.40)

package java.io; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import sun.nio.cs.StreamEncoder; // 将字节输出流转换成字符输出流 public class OutputStreamWriter extends Writer { private final StreamEncoder se; // 根据out创建OutputStreamWriter,使用编码charsetName(编码名) public OutputStreamWriter(OutputStream out, String charsetName)throws UnsupportedEncodingException {super(out);if (charsetName == null)throw new NullPointerException(charsetName);se = StreamEncoder.forOutputStreamWriter(out, this, charsetName); } // 根据out创建OutputStreamWriter,使用默认的编码 public OutputStreamWriter(OutputStream out) {super(out);try {se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);} catch (UnsupportedEncodingException e) {throw new Error(e);} } // 根据out创建OutputStreamWriter,使用编码cs public OutputStreamWriter(OutputStream out, Charset cs) {super(out);if (cs == null)throw new NullPointerException(charset);se = StreamEncoder.forOutputStreamWriter(out, this, cs); } // 根据out创建OutputStreamWriter,使用编码器enc public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {super(out);if (enc == null)throw new NullPointerException(charset encoder);se = StreamEncoder.forOutputStreamWriter(out, this, enc); }java io系列01之 目录 // 获取编码器enc public String getEncoding() {return se.getEncoding(); } // 刷新缓冲区 void flushBuffer() throws IOException {se.flushBuffer(); } // 将单个字符写入到OutputStreamWriter中 public void write(int c) throws IOException {se.write(c); } // 将字符数组cbuf从off开始的数据写入到OutputStreamWriter中,写入长度是len public void write(char cbuf[], int off, int len) throws IOException {se.write(cbuf, off, len); } // 将字符串str从off开始的数据写入到OutputStreamWriter中,写入长度是len public void write(String str, int off, int len) throws IOException {se.write(str, off, len); }java io系列01之 目录 // 刷新输出流 // 它与flushBuffer()的区别是:flushBuffer()只会刷新缓冲,而flush()是刷新流,flush()包括了flushBuffer。 public void flush() throws IOException {se.flush(); } // 关闭输出流 public void close() throws IOException {se.close(); } }

说明:

OutputStreamWriter 作用和原理都比较简单。

作用就是将字节输出流转换成字符输出流。它的原理是,我们创建字符输出流对象时,会指定字节输出流以及字符编码。

示例程序

InputStreamReader和OutputStreamWriter的使用示例,参考源码(StreamConverter.java):

本文原标题:Java源码,java源码怎么看

版权声明:本文由 壤驷恺歌 整理编辑,如果侵权请联系我们!

转载注明出处:https://www.nintaus.net/houtai@123/makehtml_archives_action.php?endid=0&startid=0&typeid=0&totalnum=11732&startdd=5420&pagesize=20&seltime=0&sstime=1589370344&stime=&etime=&uptype=mkall&mkvalue=0&isremote=0&serviterm=

文章评论

共有 条评论来说两句吧...

用户名:

验证码:

Top