zoukankan      html  css  js  c++  java
  • Java字节流:InputStream OutputStream

    字节输入流:InputStream 

    类声明:

    public abstract class InputStream implements Closeable

    位于java.io包下,是一个抽象类.

    官方对其说明:
      This abstract class is the superclass of all classes representing an input stream of bytes.
      (简单翻译:抽象类InputStream是所有字节输入流的父类)

    主要方法:
      - int available();返回输入流中可读取的字符个数.
      - void close(): 关闭此输入流并释放与该流有关的系统资源.
      - void mark(int readlimit): 在此输入流中标记当前的位置.
      - boolean markSupported(): 检测此输入流是否支持mark和reset.
      - abstract int read(): 从输入流中读取数据的下一个字节.
      - int read(byte[] b): 从输入流中读取一定数量的字节,并将其存储在字节数组b中
      - int read(byte[] b,int off,int len): 从输入流中读取len个字节,并将其存储在字节数组b中off位置开始的地方
      - void reset(): 将此流重新定位到最后一次对此输入流调用mark方法时的位置.
      - long skip(long n): 跳过和丢弃此输入流中n个字节的数据.

      InputStream 是所有字节输入流的父类,其中有一个抽象方法read() 是字节输入流的核心,所有子类都必须实现此方法。
    InputStream源代码分析:

      1 package java.io;
      2 
      3 /**
      4  * 该抽象类是所有字节输入流的父类
      5  */
      6 public abstract class InputStream implements Closeable {
      7 
      8     private static final int MAX_SKIP_BUFFER_SIZE = 2048;//最大允许跳过的字节个数
      9 
     10 
     11 /*
     12     功能: 从输入流中读取下一个字节数据,字节数据的值以0-255之间的int类型返回,如果已经到达流末尾,则返回值-1.
     13         子类必须提供该方法的一个实现.
     14     返回值:下一个字节数据;如果到达流的末尾则返回-1
     15 */
     16     public abstract int read() throws IOException;
     17 
     18 
     19     public int read(byte b[]) throws IOException {
     20         return read(b, 0, b.length);
     21     }
     22 
     23 /*
     24     功能:从输入流中读取len个字节数据,并把这些字节数据写到字节数组b中(从off位置开始写)
     25     参数:
     26         byte b[]: 存储从输入流中读取的字节数据
     27         int off: 开始位置
     28         int len: 读取的字节个数
     29     返回值: 实际从输入流中读取到字节数组b中的字节个数
     30 */
     31     public int read(byte b[], int off, int len) throws IOException {
     32 
     33     //检查字节数组b是否为null,检查off len 等参数是否数组越界
     34         if (b == null) {
     35             throw new NullPointerException();
     36         } else if (off < 0 || len < 0 || len > b.length - off) {
     37             throw new IndexOutOfBoundsException();
     38         } else if (len == 0) {
     39             return 0;
     40         }
     41 
     42       //从输入流中读取第一个字节数组,并且判断输入流是否已经到达末尾
     43         int c = read();
     44         if (c == -1) {
     45             return -1;
     46         }
     47         b[off] = (byte)c;
     48 
     49     //循环从输入流中读取len个字节数据到字节数组b中(如果循环读取过程中到达了输入流的末尾,则跳出循环不再读取)
     50         int i = 1;
     51         try {
     52             for (; i < len ; i++) {
     53                 c = read();
     54                 if (c == -1) {
     55                     break;
     56                 }
     57                 b[off + i] = (byte)c;
     58             }
     59         } catch (IOException ee) {
     60         }
     61         return i;
     62     }
     63 
     64 
     65 /*
     66     功能: 在输入流中跳过n个字节数据
     67 */
     68     public long skip(long n) throws IOException {
     69         long remaining = n;
     70         int nr;
     71 
     72         if (n <= 0) {
     73             return 0;
     74         }
     75 
     76         int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
     77         byte[] skipBuffer = new byte[size];
     78         while (remaining > 0) {
     79             nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
     80             if (nr < 0) {
     81                 break;
     82             }
     83             remaining -= nr;
     84         }
     85 
     86         return n - remaining;
     87     }
     88 
     89 /*
     90     功能: 返回输入流中还可被读取的字节个数
     91 */
     92     public int available() throws IOException {
     93         return 0;
     94     }
     95 
     96 /*
     97     功能: 关闭输入流
     98 */
     99     public void close() throws IOException {}
    100 
    101 /*
    102     功能: 在输入流中标记一个位置
    103 */
    104     public synchronized void mark(int readlimit) {}
    105 
    106 /*
    107     功能: 将此流重新定位到最后一次对此输入流调用 mark 方法时的位置.
    108 */
    109     public synchronized void reset() throws IOException {
    110         throw new IOException("mark/reset not supported");
    111     }
    112 
    113 /*
    114     功能: 检查该输入流是否支持mark()和reset()方法
    115 */
    116     public boolean markSupported() {
    117         return false;
    118     }
    119 }

    字节输入流:OutputStream 

    类声明:

    public abstract class OutputStream implements Closeable

    位于java.io包下,是一个抽象类.
    官方对其说明:
      This abstract class is the superclass of all classes representing an output stream of bytes.
      (简单翻译:抽象类OutputStream是所有字节输出流的父类)

    主要方法:
      - void close(): 关闭此输出流并释放与该流有关的系统资源.
      - void flush(): 刷新此输出流并强制写出所有缓冲的输出字节.
      - void write(byte[] b): 将b.length个字节从指定的byte数组写入此输出流.
      - void write(byte[] b,int off,int len): 将byte数组中从off位置开始的len个字节写入此输出流.
      - abstract void write(int b): 将指定的字节写入此输出流.

      OutputStream 是所有字节输出流的父类,其中有一个抽象方法write(int b) 是字节输出流的核心,所有子类都必须实现此方法。

    OutputStream源代码分析:

     1 package java.io;
     2 
     3 /*
     4 该抽象类是所有字节输出流的父类。输出流接收输出字节并将这些字节发送到某个接收器。
     5 */
     6 public abstract class OutputStream implements Closeable, Flushable {
     7 
     8 /*
     9     功能: 将指定的数据b写入到输出流中.
    10 */
    11     public abstract void write(int b) throws IOException;
    12 
    13 /*
    14     功能:将字节数组b中的全部数据写入到此输出流中
    15 */
    16     public void write(byte b[]) throws IOException {
    17         write(b, 0, b.length);
    18     }
    19 
    20 /*
    21     功能: 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
    22 */
    23     public void write(byte b[], int off, int len) throws IOException {
    24         if (b == null) {
    25             throw new NullPointerException();
    26         } else if ((off < 0) || (off > b.length) || (len < 0) ||
    27                    ((off + len) > b.length) || ((off + len) < 0)) {
    28             throw new IndexOutOfBoundsException();
    29         } else if (len == 0) {
    30             return;
    31         }
    32 
    33       //循环写入到此输出流
    34         for (int i = 0 ; i < len ; i++) {
    35             write(b[off + i]);
    36         }
    37     }
    38 
    39 /*
    40     功能:刷新此输出流并强制写出所缓冲的输出字节
    41 */
    42     public void flush() throws IOException {
    43     }
    44 
    45 /*
    46     功能:关闭此输出流并释放与此流关的所系统资源
    47 */
    48     public void close() throws IOException {
    49     }
    50 }
  • 相关阅读:
    远程连接桌面报:这可能是由于credssp加密oracle修正
    MVC断点续传
    [COCI2011-2012#5] POPLOCAVANJE 后缀自动机
    [SDOI2016]生成魔咒 后缀自动机
    [JSOI2009]密码 AC自动机
    CF17E Palisection manacher
    [JSOI2007]字符加密 后缀数组
    [POI2012]OKR-A Horrible Poem hash
    [APIO2014]回文串 manacher 后缀数组
    [SHOI2011]双倍回文 manacher
  • 原文地址:https://www.cnblogs.com/xinhuaxuan/p/6038605.html
Copyright © 2011-2022 走看看