久久96国产精品久久久-久久发布国产伦子伦精品-久久精品国产精品青草-久久天天躁夜夜躁狠狠85麻豆

技術員聯盟提供win764位系統下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:主頁 > 教程 > 服務器類 >

Java中的InputStreamReader和OutputStreamWriter源碼

來源:技術員聯盟┆發布時間:2017-10-04 06:00┆點擊:

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 available throw 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):