一个有用的从Reader向InputStream转换的类
来源:互联网 发布:it公司 招人先培训 编辑:程序博客网 时间:2024/05/22 02:12
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();
}
}
- 一个有用的从Reader向InputStream转换的类
- String 、InputStream、Reader 之间的转换
- String 、InputStream、Reader 之间的转换
- String 、InputStream、Reader 之间的转换
- InputStream向String的转换
- InputStream和Reader的区别
- InputStream和Reader的区别
- Reader和InputStream的区别
- InputStream和Reader的区别
- InputStream与Reader的区别
- InputStream和Reader的区别
- InputStream 与Reader的区别
- InputStream和Reader的区别
- InputStream和Reader的区别
- InputStream和Reader的区别
- InputStream和Reader的区别
- java中Reader类和InputStream类的区别
- 一句话JAVA--Reader和InputStream的区别
- 调整 oracle内存(SGA和PGA)优化数据库性能
- Practical Linux
- 两种实现二维数组的方式
- SGA和PGA结构图
- Java Frameworks and Components: Accelerate Your Web Application Development
- 一个有用的从Reader向InputStream转换的类
- website for flex showcase
- Learning UML [ILLUSTRATED]
- 版本控制系统 SVN
- 集合框架之迭代器
- SQL Performance Tuning
- Programming Spiders, Bots, and Aggregators in Java
- 第一次开博,转一下《没有风投的创业法则》
- oracle SGA PGA UGA 内存