一个有用的从Reader向InputStream转换的类
2008-11-20 22:36
351 查看
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();
}
}
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();
}
}
相关文章推荐
- 字符流和字节流转换桥梁,InputStreamReader,OutputStreamWriter
- 处理流中的转换流。InputStreamReader & OutputStreamWriter
- 黑马程序员:转换流介绍:InputStreamReader、OutputStreamWriter
- StringUtils:将一个InputStream流转换成字符串
- java之String, inputStream与Reader转换
- JAVA提高篇(20)--转换流-InputStreamReader、OutputStreamWriter
- String 、InputStream、Reader 之间的转换
- Java IO--字节-字符流转换OutputStreamWriter/InputStreamReader
- 读取并解析一个文本文件处理流程:编码、InputStreamReader、BufferedReader
- Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter
- I/O流之--转换流:InputStreamReader 和InputStreamWriter
- Mysql一个非常有用的内置函数今天碰到要把MySQL数据库中的varchar转换成date类型进
- I/O流--转换流:OutputStreamWriter和InputStreamReader使用
- Java之IO(七)转换流 InputStreamReader、OutputStreamWrite
- Java - IO - 转换流 - InputStreamReader - OutputStreamWriter
- java文件传输基础:字节字符流的转换OutputStreamWriter/InputStreamReader
- IOS假设将一个十六进制的color转换成UIColor,非常有用
- String 、InputStream、Reader 之间的转换
- String 、InputStream、Reader 之间的转换
- Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter