关于 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);

转载于:https://www.cnblogs.com/interdrp/p/4903494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值