欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中的InputStreamReader和OutputStreamWriter源码分析_动力节点Java学院整理

程序员文章站 2024-02-19 14:11:46
inputstreamreader和outputstreamwriter源码分析 1. inputstreamreader 源码(基于jdk1.7.40)...

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

 import java.io.file;
 import java.io.fileinputstream;
 import java.io.fileoutputstream;
 import java.io.outputstreamwriter;;
 import java.io.inputstreamreader;
 import java.io.ioexception;
 /**
 * inputstreamreader 和 outputstreamwriter 测试程序
 *
 * 
 */
 public class streamconverter {
  private static final string filename = "file.txt";
  private static final string charsetname = "utf-8";
  //private static final string charsetname = "gb2312";
  public static void main(string[] args) {
   testwrite();
   testread();
  }
  /**
  * outputstreamwriter 演示函数
  *
  */
  private static void testwrite() {
   try {
    // 创建文件“file.txt”对应file对象
    file file = new file(filename);
    // 创建fileoutputstream对应outputstreamwriter:将字节流转换为字符流,即写入out的数据会自动由字节转换为字符。
    outputstreamwriter out1 = new outputstreamwriter(new fileoutputstream(file), charsetname);
   // 写入10个汉字
   out.write("字节流转为字符流示例");
   // 向“文件中”写入"0123456789"+换行符
    out1.write("0123456789\n");
    out1.close();
   } catch(ioexception e) {
    e.printstacktrace();
   }
  }
  /**
  * inputstreamreader 演示程序
  */
  private static void testread() {
   try {
    // 方法1:新建fileinputstream对象
   // 新建文件“file.txt”对应file对象
    file file = new file(filename);
    inputstreamreader in = new inputstreamreader(new fileinputstream(file), charsetname);
    // 测试read(),从中读取一个字符
   char c1 = (char)in1.read();
    system.out.println("c1="+c1);
   // 测试skip(long bytecount),跳过4个字符
   in1.skip(6);
    // 测试read(char[] cbuf, int off, int len)
    char[] buf = new char[10];
   in1.read(buf, 0, buf.length);
    system.out.println("buf="+(new string(buf)));
    in.close();
   } catch(ioexception e) {
    e.printstacktrace();
   }
  }
 }

运行结果:

c1=字
buf=流示例0123456

结果说明:

(01) testwrite() 的作用是将“内容写入到输出流”。写入的时候,会将写入的内容转换utf-8编码并写入。

(02) testread() 的作用是将“内容读取到输入流”。读取的时候,会将内容转换成utf-8的内容转换成字节并读出来。

生成的文件utf-8的file.txt的16进制效果图如下: 

Java中的InputStreamReader和OutputStreamWriter源码分析_动力节点Java学院整理

将streamconverter.java中的charsetname修改为"gb2312"。运行程序,生产的file.txt的16进制效果图如下:

Java中的InputStreamReader和OutputStreamWriter源码分析_动力节点Java学院整理

以上所述是小编给大家介绍的java中的inputstreamreader和outputstreamwriter源码分析,希望对大家有所帮助