问题描述
我需要将 Reader 对象转换为 InputStream。我现在的解决方案如下。但我担心的是,由于这将处理大量数据,因此会大幅增加内存使用量。
private static InputStream getInputStream(final Reader reader) {
char[] buffer = new char[10240];
StringBuilder builder = new StringBuilder();
int charCount;
try {
while ((charCount = reader.read(buffer,buffer.length)) != -1) {
builder.append(buffer,charCount);
}
reader.close();
} catch (final IOException e) {
e.printstacktrace();
}
return new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
}
由于我使用 StringBuilder,这会将读取器对象的完整内容保留在内存中。我想避免这种情况。有没有办法通过管道传输 Reader 对象?非常感谢您对此的任何帮助。
解决方法
使用 Apache Commons IO 库,您可以在一行中完成此转换:
//import org.apache.commons.io.input.ReaderInputStream;
InputStream inputStream = new ReaderInputStream(reader,StandardCharsets.UTF_8);
阅读本课程的文档
可能值得一试,看看它是否也解决了内存问题。
,第一:一种罕见的需求,通常是相反的,或者有一个 FileChannel,所以可以使用一个 ByteBuffer。
PipedInputStream 是可能的,在第二个线程中启动 PipedOutputStream。然而这是不必要的。
Reader 给出字符。 Unicode 代码点源自一个或两个字符(后者是代理对)。
/**
* Reader for an InputSteam of UTF-8 text bytes.
*/
public class ReaderInputStream extends InputStream {
private final Reader reader;
private boolean eof;
private int byteCount;
private byte[] bytes = new byte[6];
public ReaderInputStream(Reader reader) {
this.reader = reader;
}
@Override
public int read() throws IOException {
if (byteCount > 0) {
int c = bytes[0];
--byteCount;
for (int i = 0; i < byteCount; ++i) {
bytes[i] = bytes[i + 1];
}
return c;
}
if (eof) {
return -1;
}
int c = reader.read();
if (c == -1) {
eof = true;
return -1;
}
char ch = (char) c;
String s;
if (Character.isHighSurrogate(ch)) {
c = reader.read();
if (c == -1) {
// Error,low surrogate expected.
eof = true;
//return -1;
throw new IOException("Expected a low surrogate char i.o. EOF");
}
char ch2 = (char) c;
if (!Character.isLowSurrogate(ch2)) {
throw new IOException("Expected a low surrogate char");
}
s = new String(new char [] {ch,ch2});
} else {
s = Character.toString(ch);
}
byte[] bs = s.getBytes(StandardCharsets.UTF_8);
byteCount = bs.length;
System.arraycopy(bs,bytes,byteCount);
return read();
}
}
Path source = Paths.get("...");
Path target = Paths.get("...");
try (Reader reader = Files.newBufferedReader(source,StandardCharsets.UTF_8);
InputStream in = new ReaderInputStream(reader)) {
Files.copy(in,target);
}