001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: ReaderToInputStream.java 4029 2006-12-22 04:36:44Z jzhang $
024: */
025: package com.bostechcorp.cbesb.runtime.ccsl.lib;
026:
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStreamWriter;
031: import java.io.Reader;
032: import java.io.UnsupportedEncodingException;
033: import java.io.Writer;
034:
035: /**
036: *
037: */
038: public class ReaderToInputStream extends InputStream {
039:
040: protected Reader reader;
041:
042: protected ByteArrayOutputStream byteArrayOut;
043:
044: protected Writer writer;
045:
046: protected char[] chars;
047:
048: protected byte[] buffer;
049:
050: protected int index, length;
051:
052: public ReaderToInputStream(Reader reader) {
053: this .reader = reader;
054: byteArrayOut = new ByteArrayOutputStream();
055: writer = new OutputStreamWriter(byteArrayOut);
056: chars = new char[1024];
057: }
058:
059: public ReaderToInputStream(Reader reader, String encoding)
060: throws UnsupportedEncodingException {
061: this .reader = reader;
062: byteArrayOut = new ByteArrayOutputStream();
063: writer = new OutputStreamWriter(byteArrayOut, encoding);
064: chars = new char[1024];
065: }
066:
067: public int read() throws IOException {
068: if (index >= length)
069: fillBuffer();
070: if (index >= length)
071: return -1;
072: return 0xff & buffer[index++];
073: }
074:
075: protected void fillBuffer() throws IOException {
076: if (length < 0)
077: return;
078: int numChars = reader.read(chars);
079: if (numChars < 0) {
080: length = -1;
081: } else {
082: byteArrayOut.reset();
083: writer.write(chars, 0, numChars);
084: writer.flush();
085: buffer = byteArrayOut.toByteArray();
086: length = buffer.length;
087: index = 0;
088: }
089: }
090:
091: public int read(byte[] data, int off, int len) throws IOException {
092: if (index >= length)
093: fillBuffer();
094: if (index >= length)
095: return -1;
096: int amount = Math.min(len, length - index);
097: System.arraycopy(buffer, index, data, off, amount);
098: index += amount;
099: return amount;
100: }
101:
102: public int available() throws IOException {
103: return (index < length) ? length - index
104: : ((length >= 0) && reader.ready()) ? 1 : 0;
105: }
106:
107: public void close() throws IOException {
108: reader.close();
109: }
110: }
|