⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readerinputstream.java

📁 一个有关于ajax的应用示例是什么 样子的
💻 JAVA
字号:
package com.ata.struts2;

import java.io.*;

public class ReaderInputStream extends InputStream {
    protected Reader reader;
    protected ByteArrayOutputStream byteArrayOut;
    protected Writer writer;
    protected char[] chars;
    protected byte[] buffer;
    protected int index, length;

      /**
       * 带Reader参数构造函数
       *
       * @param reader - InputStream使用的Reader
       */
      public ReaderInputStream(Reader reader) {
        this.reader = reader;
        byteArrayOut = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(byteArrayOut);
        chars = new char[1024];
      }

      /**
       * 带Reader和字符编码格式参数的构造函数
       *
       * @param reader   - InputStream使用的Reader
       * @param encoding - InputStream使用的字符编码格式.
       * @throws 如果字符编码格式不支持,则抛UnsupportedEncodingException异常
       */
      public ReaderInputStream(Reader reader, String encoding)
          throws UnsupportedEncodingException {
        this.reader = reader;
        byteArrayOut = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(byteArrayOut, encoding);
        chars = new char[1024];
      }

      /**
       * @see java.io.InputStream#read()
       */
      public int read() throws IOException {
        if (index >= length)
          fillBuffer();
        if (index >= length)
          return -1;
        return 0xff & buffer[index++];
      }

      protected void fillBuffer() throws IOException {
        if (length < 0)
          return;
        int numChars = reader.read(chars);
        if (numChars < 0) {
          length = -1;
        } else {
          byteArrayOut.reset();
          writer.write(chars, 0, numChars);
          writer.flush();
          buffer = byteArrayOut.toByteArray();
          length = buffer.length;
          index = 0;
        }
      }

      /**
       * @see java.io.InputStream#read(byte[], int, int)
       */
      public int read(byte[] data, int off, int len) throws IOException {
        if (index >= length)
          fillBuffer();
        if (index >= length)
          return -1;
        int amount = Math.min(len, length - index);
        System.arraycopy(buffer, index, data, off, amount);
        index += amount;
        return amount;
      }

      /**
       * @see java.io.InputStream#available()
       */
      public int available() throws IOException {
        return (index < length) ? length - index :
            ((length >= 0) && reader.ready()) ? 1 : 0;
      }

      /**
       * @see java.io.InputStream#close()
       */
      public void close() throws IOException {
        reader.close();
      }

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -