ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          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參數(shù)構(gòu)造函數(shù)
                 *
                 * @param reader - InputStream使用的Reader
                 */
                public ReaderInputStream(Reader reader) {
                  this.reader = reader;
                  byteArrayOut = new ByteArrayOutputStream();
                  writer = new OutputStreamWriter(byteArrayOut);
                  chars = new char[1024];
                }

                /**
                 * 帶Reader和字符編碼格式參數(shù)的構(gòu)造函數(shù)
                 *
                 * @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();
                }

          }
          posted on 2005-08-11 09:27 ivaneeo 閱讀(2317) 評(píng)論(0)  編輯  收藏 所屬分類: java魔力
          主站蜘蛛池模板: 延吉市| 合阳县| 托里县| 邵阳县| 高州市| 富川| 西峡县| 营口市| 涟源市| 阳高县| 镇江市| 仪征市| 武冈市| 西林县| 太保市| 容城县| 兴隆县| 东光县| 长治市| 宾川县| 灵台县| 华阴市| 望谟县| 茂名市| 星子县| 泽普县| 皮山县| 桃源县| 古田县| 巴里| 甘德县| 万宁市| 泰兴市| 钟山县| 葵青区| 安福县| 田阳县| 彭山县| 乐亭县| 扎兰屯市| 平潭县|