关于 java,nio,bufferedreader,bytebuffer
有没有一种方法来读取的ByteBuffer有一个BufferedReader,而无需将其转换为String优先?我想读通过一个相当大的 ByteBuffer作为文本行和我想避免它写入磁盘性能方面的原因。对ByteBuffer的调用toString不起作用生成的字符串太大(它抛出 java.lang.OutOfMemoryError:Java堆空间)。我本来以为会有的API来包装的ByteBuffer在合适的读者,但我似乎 无法找到任何合适的。 下面是我做的一个简短的代码示例中):
// input stream is from Process getInputStream()
1. 目前尚不清楚为什么你是一个字节的缓冲区开始。如果你有一个
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
}
}
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的更多相关文章
- JAVA NIO 之ByteBuffer的mark、position、limit、flip、reset,get方法介绍
参考博客:http://blog.csdn.net/sunzhenhua0608/article/details/31778519 先来一个demo: import java.nio.ByteBuff ...
- java nio通过ByteBuffer输出文件信息
1.通过ByteBuffer的get()方法每次读取一个字节转换成char类型输出. fc = new FileInputStream("src/demo20/data.txt") ...
- 快学Java NIO 续篇
可以先看Java NIO的整体介绍,这篇接着说以下内容,<快学Java NIO>续篇 FileChannel SocketChannel ServerSocketChannel Java ...
- 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 ...
- JAVA NIO缓冲区(Buffer)------ByteBuffer常用方法
参考:https://blog.csdn.net/xialong_927/article/details/81044759 缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I ...
- Java NIO ByteBuffer 的使用与源码研究
一.结论 ByteBuffer 是Java NIO体系中的基础类,所有与Channel进行数据交互操作的都是以ByteBuffer作为数据的载体(即缓冲区).ByteBuffer的底层是byte数组, ...
- java.nio.ByteBuffer中的flip()、rewind()、compact()等方法的使用和区别
java.nio.ByteBuffer 1. ByteBuffer中的参数position.limit.capacity.mark含义: position:表示当前指针的位置(下一个要操作的数据元素的 ...
- java.nio.ByteBuffer中flip,rewind,clear方法的区别
对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...
- java.nio.ByteBuffer中flip、rewind、clear方法的区别
对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...
随机推荐
- Java 在方法和作用域内的内部类
通常,如果所读写 的代码包含了内部类,那么它们都是"平凡的"内部类,简单并且容易理解,然而,内部类的语法覆盖了大量其它的更加难以理解的计数,例如可以在一个方法里或者在任意的作用域里 ...
- <转> 解决异常:IllegalStateException: Fragment <ThisFragment> is not currently in the FragmentManager
上午敲代码时出现这个问题,简单记录一下解决办法,有时间详细描述一下深层原因. 问题出现在这: @Override public void onSaveInstanceState(Bundle outS ...
- .NetCore 中如何实现分页以及编写一个URL分页
首先看下效果 这个分页控件不是很完美,体现下思路就行了,有兴趣的可以自己完善,我把代码贴出来,在这边文章中已有一些介绍 代码 public class UosoPagerTagHelper : Tag ...
- oracle的sql语句大小写
我相信大家都知道,oracle数据库是区分大小写的,而且oracle的默认为大写的,也就是说你在sql脚本上面写的sql语句,oracle运行的时候,它会自动转化为大写的.注意一下,我这里举例子的计算 ...
- LINUX的STRACE命令用法 [转]
调用:strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] ...[ -ofile ] [ -ppid ] ... [ -sstrsize ] [ -u ...
- rabbitmq route
AMQP AMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Server(broker): 接受客户端连接,实现AMQP消息队列和路由功能的进程 ...
- zabbix agent配置文件记录
一.无论主动还是被动模式都要配置server和linstenPort 二.若要设置主动模式那么要添加ServerActive,若不添加则默认为被动模式
- Spring重复扫描导致事务失败的解决方案及深入分析
问题及日志使用Spring和mybatis,然后配置事务,出现SqlSession was not registered for synchronization because synchroniza ...
- BZOJ.1003.[ZJOI2006]物流运输(DP 最短路Dijkstra)
题目链接 容易看出是个最短路+DP.既然答案和天数有关,那么就令\(f[i]\)表示前\(i\)天最小成本. 这个转移很好想: \(f[i]=\min(f[i],\ f[j]+cost(j+1,i)+ ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...