关于 java,nio,bufferedreader,bytebuffer
有没有一种方法来读取的ByteBuffer有一个BufferedReader,而无需将其转换为String优先?我想读通过一个相当大的 ByteBuffer作为文本行和我想避免它写入磁盘性能方面的原因。对ByteBuffer的调用toString不起作用生成的字符串太大(它抛出 java.lang.OutOfMemoryError:Java堆空间)。我本来以为会有的API来包装的ByteBuffer在合适的读者,但我似乎 无法找到任何合适的。 下面是我做的一个简短的代码示例中):
// input stream is from Process getInputStream() public String read(InputStream istream) { ReadableByteChannel source = Channels.newChannel(istream); ByteArrayOutputStream ostream = new ByteArrayOutputStream(bufferSize); WritableByteChannel destination = Channels.newChannel(ostream); ByteBuffer buffer = ByteBuffer.allocateDirect(writeBufferSize); while (source.read(buffer) != -1) { buffer.flip(); while (buffer.hasRemaining()) { destination.write(buffer); } buffer.clear(); } // this data can be up to 150 MB.. won't fit in a String. result = ostream.toString(); source.close(); destination.close(); return result; } // after the process is run, we call this method with the String public void readLines(String text) { BufferedReader reader = new BufferedReader(new StringReader(text)); String line; while ((line = reader.readLine()) != null) { // do stuff with line } }
1. 目前尚不清楚为什么你是一个字节的缓冲区开始。如果你有一个InputStream
和你想读行吧,你为什么不一个InputStreamReader
包裹在一个BufferedReader
?是什么在获得NIO涉及的利益? 调用toString()
上一个ByteArrayOutputStream
听起来好像即使你有它的空间是一个坏主意:不如把它作为一个字节数组并把它包在一个ByteArrayInputStream
然后一个InputStreamReader
,如果你真的必须有一个ByteArrayOutputStream
。如果你真的想调用toString()
在它接受的字符编码的过载-否则“系统默认的,这可能不是你想要的。 编辑:好了,你真的想NIO。你还在写一ByteArrayOutputStream
最终,所以你最终有一个BAOS与它的数据。如果你想避免让这些数据的副本,你需要从派生ByteArrayOutputStream
例如像这样:
public class ReadableByteArrayOutputStream extends ByteArrayOutputStream
{
/**
* Converts the data in the current stream into a ByteArrayInputStream.
* The resulting stream wraps the existing byte array directly;
* further writes to this output stream will result in unpredictable
* behavior.
*/
public InputStream toInputStream()
{
return new ByteArrayInputStream(array, 0, count);
}
}
然后 CodeGo.net,您可以创建输入流,把它包在InputStreamReader
,包裹在一个BufferedReader
和你离开。
2.
你NIO,但这里没有真正的需要。由于乔恩斯基特建议:
public byte[] read(InputStream istream)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // Experiment with this value
int bytesRead;
while ((bytesRead = istream.read(buffer)) != -1)
{
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
// after the process is run, we call this method with the String
public void readLines(byte[] data)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
String line;
while ((line = reader.readLine()) != null)
{
// do stuff with line
}
}
3.
这是一个示例:
public class ByteBufferBackedInputStream extends InputStream {
ByteBuffer buf;
public ByteBufferBackedInputStream(ByteBuffer buf) {
this.buf = buf;
}
public synchronized int read() throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
return buf.get() & 0xFF;
}
@Override
public int available() throws IOException {
return buf.remaining();
}
public synchronized int read(byte[] bytes, int off, int len) throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
len = Math.min(len, buf.remaining());
buf.get(bytes, off, len);
return len;
}
}
而你是这样的:
String text = "this is text"; // It can be Unicode text
ByteBuffer buffer = ByteBuffer.wrap(text.getBytes("UTF-8"));
InputStream is = new ByteBufferBackedInputStream(buffer);
InputStreamReader r = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(r);
BufferedReader br = new BufferedReader(r);