关于 java,nio,bufferedreader,bytebuffer
2015-10-23 09:36
453 查看
有没有一种方法来读取的ByteBuffer有一个BufferedReader,而无需将其转换为String优先?我想读通过一个相当大的 ByteBuffer作为文本行和我想避免它写入磁盘性能方面的原因。对ByteBuffer的调用toString不起作用生成的字符串太大(它抛出 java.lang.OutOfMemoryError:Java堆空间)。我本来以为会有的API来包装的ByteBuffer在合适的读者,但我似乎 无法找到任何合适的。 下面是我做的一个简短的代码示例中):
1. 目前尚不清楚为什么你是一个字节的缓冲区开始。如果你有一个
2.
你NIO,但这里没有真正的需要。由于乔恩斯基特建议:[/code]
// 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例如像这样:[/code]
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,您可以创建输入流,把它包在[code]InputStreamReader,包裹在一个
BufferedReader和你离开。
2.
你NIO,但这里没有真正的需要。由于乔恩斯基特建议:[/code]
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);
相关文章推荐
- LeetCode Game of Life
- Node.js构建Web应用
- CSS旋转图片
- PhoneGap+Angularjs+ionic 环境搭建
- 浅析Node.js 中 Stream API 的使用
- jQuery实现右侧显示可向左滑动展示的深色QQ客服效果代码
- leetcode_19_Remove Nth Node From End of List (easy)
- CDH安装oozie页面缺少Ext JS library
- jquery validate 详解
- javascript === 与 ==
- ITIM二次开发:Workflow之JavaScriptExtension
- JavaScript权威指南_169_第17章_事件处理_17.3-调用事件处理程序-事件传播、事件取消
- 【bzoj1012】[JSOI2008]最大数maxnumber st表
- js alert()后进行跳转的方法
- 夺命雷公狗jquery---10小实例-表格插入内容
- Event Sourcing - ENode(二)
- 夺命雷公狗jquery---9表单对象元素选择器
- 夺命雷公狗jquery---8表单元素选择器
- 夺命雷公狗jquery---7子元素选择器
- 夺命雷公狗jquery---6属性选择器