bytebuffer

How to read a request using CompletionHandlers and a ByteBuffer smaller than the request?

≡放荡痞女 提交于 2019-11-29 21:11:26
问题 I am using Java 7 and AsynchronousSocketChannel. I would like to read a request (e.g. HTTP POST ) but I'm struggling to come up with a nice solution to read the full request, if it's bigger than the size of the ByteBuffer I'm using. E.g. if the ByteBuffer is 4048 bytes and the HTTP POST contains an image that is larger than 4kB. Is there any nice recursive solution or loop for this? Here is my code for reading requests: public void readRequest(final AsynchronousSocketChannel ch) { final

What is the purpose of ByteBuffer's flip method? (And why is it called “flip”?)

ぐ巨炮叔叔 提交于 2019-11-29 19:41:36
Why does ByteBuffer's flip() method called "flip"? What is "flipped" here? According to apidoc, two successive flips won't restore original state, and multiple flips will probably tend limit() to become zero. Can I "unflip" somehow to reuse bytes went out of a limit? Can I concatenate tail to be flipped with some other data? One fairly common use case for the ByteBuffer is to construct some data structure piece-by-piece and then write that whole structure to disk. flip is used to flip the ByteBuffer from "reading from I/O" ( put ting) to "writing to I/O" ( get ting): after a sequence of put s

java nio详解

蓝咒 提交于 2019-11-29 18:11:07
NIO简介 NIO是一种同步非阻塞的I/O模型,在Java 1.4 中引入了NIO框架,对应 java.nio 包,提供了 Channel , Selector,Buffer等抽象。 NIO中的N可以理解为Non-blocking,不单纯是New。它支持面向缓冲的,基于通道的I/O操作方法。 NIO提供了与传统BIO模型中的 Socket 和 ServerSocket 相对应的 SocketChannel 和 ServerSocketChannel 两种不同的套接字通道实现,两种通道都支持阻塞和非阻塞两种模式。阻塞模式使用就像传统中的支持一样,比较简单,但是性能和可靠性都不好;非阻塞模式正好与之相反。对于低负载、低并发的应用程序,可以使用同步阻塞I/O来提升开发速率和更好的维护性;对于高负载、高并发的(网络)应用,应使用 NIO 的非阻塞模式来开发。 NIO的特性/NIO与IO区别 如果是在面试中回答这个问题,我觉得首先肯定要从 NIO 流是非阻塞 IO 而 IO 流是阻塞 IO 说起。然后,可以从 NIO 的3个核心组件/特性为 NIO 带来的一些改进来分析。如果,你把这些都回答上了我觉得你对于 NIO 就有了更为深入一点的认识,面试官问到你这个问题,你也能很轻松的回答上来了。 1、Non-blocking IO(非阻塞IO) IO流是阻塞的,NIO流是不阻塞的。 Java

Java高并发网络编程(三)NIO

这一生的挚爱 提交于 2019-11-29 16:53:41
从Java 1.4开始,Java提供了新的非阻塞IO操作API,用意是替代Java IO和Java Networking相关的API。 NIO中有三个核心组件: Buffer缓冲区 Channel通道 Selector选择器 一、Buffer缓冲区 缓冲区 本质上是一个可以写入数据的 内存块 (类似数组),然后可以再次读取。此内存块包含在NIO Buffer对象中,该对象提供了一组方法,可以更轻松地使用内存块。 相比较直接对数组的操作,BufferAPI更容易操作和管理。 使用Buffer进行数据写入与读取的 四个步骤 : 将数据写入缓冲区 调用buffer.flip(),转换为读取模式 缓冲区读取数据 调用buffer.clear()或buffer.compact()清除缓冲区 Buffer工作原理 Buffer三个重要属性: capacity 容量:作为一个内存块,Buffer具有一定的固定大小,称为“容量”。 position 位置: 写入模式 时代表写数据的位置。 读取模式 时代表读取数据的位置。 limit 限制: 写入模式 ,限制等于buffer的容量。 读取模式 下,limit等于写入的数据量。 二、Buffer的API 一个示例 public class BufferDemo { public static void main(String[] args) { //

How to fill byte array with junk?

本小妞迷上赌 提交于 2019-11-29 16:35:57
问题 I am using this: byte[] buffer = new byte[10240]; As I understand this initialize the buffer array of 10kb filled with 0s. Whats the fastest way to fill this array (or initialize it) with junk data every time? I need to use that array like >5000 times and fill it every time with different junk data, that's why I am looking for a fast method to do it. The array size will also have to change every time. 回答1: If you are happy with the data being random, but being created form a random seed

Determining Appropriate Buffer Size

微笑、不失礼 提交于 2019-11-29 11:21:56
I am using ByteBuffer.allocateDirect() to allocate some buffer memory for reading a file into memory and then eventually hashing that files bytes and getting a file hash (SHA) out of it. The input files range greatly in size, anywhere from a few KB's to several GB's. I have read several threads and pages (even some on SO) regarding selecting a buffer size. Some advised trying to select one that the native FileSystem uses in an attempt to minimalize chances of a read operation for a partial block,etc. Such as buffer of 4100 bytes and NTFS defaults to 4096, so the extra 4 bits would require a

Java InputStream to ByteBuffer

不想你离开。 提交于 2019-11-29 09:08:58
I am reading dds textures, but since once built the jar I can't access those textures through url and file and have to use InputStream instead. So I would need to know how I can obtain a java.​nio.ByteBuffer from an java.io.InputStream . Ps: no matter through 3rd part libraries, I just need it working For me the best in this case is Apache commons-io to handle this and similar tasks. The IOUtils type has a static method to read an InputStream and return a byte[] . InputStream is; byte[] bytes = IOUtils.toByteArray(is); Internally this creates a ByteArrayOutputStream and copies the bytes to the

Extending ByteBuffer class

二次信任 提交于 2019-11-29 05:52:30
Is there any way to create class that extends ByteBuffer class? Some abstract methods from ByteBuffer are package private, and if I create package java.nio, security exception is thrown. I would want to do that for performance reasons - getInt for example has about 10 method invocations, as well as quite a few if's. Even if all checks are left, and only method calls are inlined and big/small endian checks are removed, tests that I've created show that it can be about 4 times faster. You cant extend ByteBuffer and thanks God for. You cant extend b/c there are no protected c-tors. Why thank god

How to convert a String array to a Byte array? (java)

江枫思渺然 提交于 2019-11-28 23:40:08
I have a one dimensional String array that I want to convert into a one dimensional byte array. How do I do this? Does this require ByteBuffer? How can I do this? (The strings can be any length, just want to know how to go about doing such an act. And after you convert it into a byte array how could I convert it back into a String array? -Dan Array to Array you should convert manually with parsing into both sides, but if you have just a String you can String.getBytes() and new String(byte[] data) ; like this public static void main(String[] args) { String[] strings = new String[]{"first",

Java NIO MappedByteBuffer OutOfMemoryException

血红的双手。 提交于 2019-11-28 22:05:51
I am really in trouble: I want to read HUGE files over several GB using FileChannel s and MappedByteBuffer s - all the documentation I found implies it's rather simple to map a file using the FileChannel.map() method. Of course there is a limit at 2GB as all the Buffer methods use int for position, limit and capacity - but what about the system implied limits below that? In reality, I get lots of problems regarding OutOfMemoryException s! And no documentation at all that really defines the limits! So - how can I map a file that fits into the int-limit safely into one or several