有没有一种方法来读取的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);

关于 java,nio,bufferedreader,bytebuffer的更多相关文章

  1. JAVA NIO 之ByteBuffer的mark、position、limit、flip、reset,get方法介绍

    参考博客:http://blog.csdn.net/sunzhenhua0608/article/details/31778519 先来一个demo: import java.nio.ByteBuff ...

  2. java nio通过ByteBuffer输出文件信息

    1.通过ByteBuffer的get()方法每次读取一个字节转换成char类型输出. fc = new FileInputStream("src/demo20/data.txt") ...

  3. 快学Java NIO 续篇

    可以先看Java NIO的整体介绍,这篇接着说以下内容,<快学Java NIO>续篇 FileChannel SocketChannel ServerSocketChannel Java ...

  4. IO的详细解释:It's all about buffers: zero-copy, mmap and Java NIO

    There are use cases where data need to be read from source to a sink without modification. In code t ...

  5. JAVA NIO缓冲区(Buffer)------ByteBuffer常用方法

    参考:https://blog.csdn.net/xialong_927/article/details/81044759 缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I ...

  6. Java NIO ByteBuffer 的使用与源码研究

    一.结论 ByteBuffer 是Java NIO体系中的基础类,所有与Channel进行数据交互操作的都是以ByteBuffer作为数据的载体(即缓冲区).ByteBuffer的底层是byte数组, ...

  7. java.nio.ByteBuffer中的flip()、rewind()、compact()等方法的使用和区别

    java.nio.ByteBuffer 1. ByteBuffer中的参数position.limit.capacity.mark含义: position:表示当前指针的位置(下一个要操作的数据元素的 ...

  8. java.nio.ByteBuffer中flip,rewind,clear方法的区别

    对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...

  9. java.nio.ByteBuffer中flip、rewind、clear方法的区别

    对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...

随机推荐

  1. NOIp 2018 提高组

    T1铺设道路 传送门 题目描述 春春是一名道路工程师,负责铺设一条长度为 $ n $ 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 $ n $ 块首尾相连的区域,一开始,第 ii ...

  2. InnoDB Lock浅谈

    数据库使用锁是为了支持更好的并发,提供数据的完整性和一致性.InnoDB是一个支持行锁的存储引擎,锁的类型有:共享锁(S).排他锁(X).意向共享(IS).意向排他(IX).为了提供更好的并发,Inn ...

  3. html圈圈

    <html> <head> <meta charset="utf-8" /> <link href="images/style. ...

  4. 判断js数组/对象是否为空

    /** * 判断js数组/对象是否为空 * isPrototypeOf() 验证一个对象是否存在于另一个对象的原型链上.即判断 Object 是否存在于 $obj 的原型链上.js中一切皆对象,也就是 ...

  5. 040 DataFrame中的write与read编程

    一:SparkSQL支持的外部数据源 1.支持情况 2.External LIbraries 不是内嵌的,看起来不支持. 但是现在已经有很多开源插件,可以进行支持. 3.参考材料 · 支持的格式:ht ...

  6. 020.Zabbix的Actions配置

    一 Action概述 当产生Trigger后,即当触发器条件被满足时,采取一些操作,如发送事件通知,远程执行命令等,需要配置Action.   名称 作用 Trigger 当Trigger的状态从OK ...

  7. rabbitmq学习(一) —— 安装篇

    安装篇之windows: 略(楼主在windows上安装基本就是按部就班的没遇到什么坑) 安装篇值centos7: 主要记录下centos7下的安装,因为在该系统下安装稍微折腾了下 参考https:/ ...

  8. 循序渐进学.Net Core Web Api开发系列【10】:使用日志

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇介 ...

  9. eclipse中的maven build、maven clean、maven install和maven test的区别

    eclipse中的maven build.maven clean.maven install和maven test的区别 https://www.cnblogs.com/Marydon20170307 ...

  10. [python]接口签名

    一个主机中的数据要通过外网发送数据给另外一个主机,为了保证接口安全,需要对接口进行签名,由于重放攻击貌似对这种接口无效,所以没有加入时间戳 直接放代码: #!/usr/bin/env python # ...