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; // 將“字節(jié)輸入流”轉(zhuǎn)換成“字符輸入流” public class InputStreamReader extends Reader { private final StreamDecoder sd; // 根據(jù)in創(chuàng)建InputStreamReader,使用默認(rèn)的編碼 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 available throw new Error(e); } } // 根據(jù)in創(chuàng)建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); } // 根據(jù)in創(chuàng)建InputStreamReader,使用編碼cs public InputStreamReader(InputStream in, Charset cs) { super(in); if (cs == null) throw new NullPointerException("charset"); sd = StreamDecoder.forInputStreamReader(in, this, cs); } // 根據(jù)in創(chuàng)建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(); } // 讀取并返回一個(gè)字符 public int read() throws IOException { return sd.read(); } // 將InputStreamReader中的數(shù)據(jù)寫入cbuf中,從cbuf的offset位置開始寫入,寫入長度是length public int read(char cbuf[], int offset, int length) throws IOException { return sd.read(cbuf, offset, length); } // 能否從InputStreamReader中讀取數(shù)據(jù) public boolean ready() throws IOException { return sd.ready(); } // 關(guān)閉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; // 將“字節(jié)輸出流”轉(zhuǎn)換成“字符輸出流” public class OutputStreamWriter extends Writer { private final StreamEncoder se; // 根據(jù)out創(chuàng)建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); } // 根據(jù)out創(chuàng)建OutputStreamWriter,使用默認(rèn)的編碼 public OutputStreamWriter(OutputStream out) { super(out); try { se = StreamEncoder.forOutputStreamWriter(out, this, (String)null); } catch (UnsupportedEncodingException e) { throw new Error(e); } } // 根據(jù)out創(chuàng)建OutputStreamWriter,使用編碼cs public OutputStreamWriter(OutputStream out, Charset cs) { super(out); if (cs == null) throw new NullPointerException("charset"); se = StreamEncoder.forOutputStreamWriter(out, this, cs); } // 根據(jù)out創(chuàng)建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(); } // 刷新緩沖區(qū) void flushBuffer() throws IOException { se.flushBuffer(); } // 將單個(gè)字符寫入到OutputStreamWriter中 public void write(int c) throws IOException { se.write(c); } // 將字符數(shù)組cbuf從off開始的數(shù)據(jù)寫入到OutputStreamWriter中,寫入長度是len public void write(char cbuf[], int off, int len) throws IOException { se.write(cbuf, off, len); } // 將字符串str從off開始的數(shù)據(jù)寫入到OutputStreamWriter中,寫入長度是len public void write(String str, int off, int len) throws IOException { se.write(str, off, len); }java io系列01之 "目錄" // 刷新“輸出流” // 它與flushBuffer()的區(qū)別是:flushBuffer()只會(huì)刷新緩沖,而flush()是刷新流,flush()包括了flushBuffer。 public void flush() throws IOException { se.flush(); } // 關(guān)閉“輸出流” public void close() throws IOException { se.close(); } }
說明:
OutputStreamWriter 作用和原理都比較簡單。
作用就是將“字節(jié)輸出流”轉(zhuǎn)換成“字符輸出流”。它的原理是,我們創(chuàng)建“字符輸出流”對(duì)象時(shí),會(huì)指定“字節(jié)輸出流”以及“字符編碼”。
示例程序
InputStreamReader和OutputStreamWriter的使用示例,參考源碼(StreamConverter.java):