bytebuffer

Java - Heap vs Direct memory access

谁说胖子不能爱 提交于 2019-12-02 16:01:15
I recenty came across sun.misc.Unsafe class, allowing user to allocate,deallocate and in general access memory in a similar fashion like in C. I read in a couple of blogs that tackle this issue e.g. Which is faster - heap or direct memory - test results claim heap Off-heap memory vs DirectByteBuffer vs Heap - Off-heap seems to be fastest Memory mapped files for time series data - MappedByteBuffer faster than heap objects Article 1) seems to be in contradiction with the other ones and I fail to comprehend why. DirectMemoryBuffer is using sun.misc.Unsafe under the hood (so is MappedByteBuffer ),

Java NIO scan through ByteBuffer for certain bytes and word with sections

我只是一个虾纸丫 提交于 2019-12-02 11:19:32
问题 Okay, so I'm trying to do something that seemed like it should be fairly simple, but with these new NIO interfaces, things are confusing the hell out of me! Here's what I'm trying to do, I need to scan through a file as bytes until encountering certain bytes! When I encounter those certain bytes, need to grab that segment of the data and do something with it, and then move on and do this again. I would have thought that with all these markers and positions and limits in ByteBuffer, I'd be

nio(2)更多功能

狂风中的少年 提交于 2019-12-02 11:17:03
目录 一 Scatter/Gatter scatter gatter 二 transferFrom & transferTo transferFrom transferTo 三 Pipe 四 内存映射文件 内存映射IO Java的内存映射IO的要点 使用 五 直接缓冲 六 总结 参考资料 一 Scatter/Gatter Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作。 分散(scatter)从Channel中读取是指在读操作时将读取的数据写入多个buffer中。因此,Channel将从Channel中读取的数据“分散(scatter)”到多个Buffer中。 聚集(gather)写入Channel是指在写操作时将多个buffer的数据写入同一个Channel,因此,Channel 将多个Buffer中的数据“聚集(gather)”后发送到Channel。 scatter / gather经常用于需要将传输的数据分开处理的场合,例如传输一个由 消息头 和 消息体 组成的消息,你可能会将消息体和消息头分散到不同的buffer中,这样你可以方便的处理消息头和消息体 scatter //这是分散(scatter) ByteBuffer header

nio(4)字符集编解码

柔情痞子 提交于 2019-12-02 11:13:50
目录 一 map映射结合Charset 二 各种编码介绍 ASCII ISO-8859-1 gb2321 unicode utf8 推荐 参考 一 map映射结合Charset public static void main ( String [ ] args ) throws Exception { String inputFile = "input_1.txt" ; String outputFile = "output_1.txt" ; RandomAccessFile inputRandomAccessFile = new RandomAccessFile ( inputFile , "r" ) ; RandomAccessFile outputRandomAccessFile = new RandomAccessFile ( outputFile , "rw" ) ; long inputFileLength = new File ( inputFile ) . length ( ) ; FileChannel inputFileChannel = inputRandomAccessFile . getChannel ( ) ; FileChannel outputFileChannel = outputRandomAccessFile . getChannel ( )

ByteBuffer使用实例

谁说胖子不能爱 提交于 2019-12-02 07:05:16
  ByteBuffer作为老牌的字节流处理对象,这里举个小例子说明下用法,直接上代码: package com.wlf.netty.nettyserver; import org.junit.Assert; import org.junit.Test; import java.nio.ByteBuffer; public class ByteBufferTest { @Test public void byteBufferTest() { // 写入消息体 ByteBuffer byteBuffer = ByteBuffer.allocate(10); byteBuffer.putInt(0xabef0101); byteBuffer.putInt(1024); // 今天过节 byteBuffer.put((byte) 1); byteBuffer.put((byte) 0); // 读取消息头,因为写完后position已经到10了,所以需要先反转为0,再从头读取 byteBuffer.flip(); printDelimiter(byteBuffer); // 读取length printLength(byteBuffer); // 继续读取剩下数据 byteBuffer.get(); byteBuffer.get(); printByteBuffer(byteBuffer

Java NIO之缓存Buffer代码实例

醉酒当歌 提交于 2019-12-02 05:44:53
文章目录 代码实例 控制台输出结果 代码实例 import java . nio . Buffer ; import java . nio . ByteBuffer ; import java . util . ArrayList ; import java . util . List ; /** * java NIO系列之缓冲区Buffer: * *主要负责数据的存取,其底层的实现就是数组,用于存储不同数据类型的数据, * 根据不同的数据类型(Boolean除外),提供相应类型的缓冲区: * ByteBuffer * ShortBuffer * IntBuffer * LongBuffer * FloatBuffer * DoubleBuffer * CharBuffer * * 这几种Buffer获取缓冲区的方式都是: * allocate(size):获取非直接缓冲区 * allocateDirect(size)获取直接缓冲区 * size为指定分配大小的缓冲区 * * 缓冲区的四个属性值: * 1.capacity:缓冲区的最大容量,一旦声明就不能改变 * 2.limit:界限,缓冲区中可以操作的数据大小 * 3.position:缓冲区中正在操作数据的位置 * 4.mark:记录当前posion的位置,可以通过reset()恢复到mark的位置 */ public

JAVA NIO udp 实现 群转发

折月煮酒 提交于 2019-12-02 05:36:32
场景很简单,就是多个客户端通过udp,连接到服务器(其实是无连接的,就是服务器保存了客户端的ip信息 server代码 public class Server { private static LinkedList<SocketAddress> list=new LinkedList<SocketAddress>(); private static final ExecutorService executorService = Executors.newFixedThreadPool(4);//这里用多线程去转发,可以提高效率。emmm,udp没有3次握手,速度应该很快,多不多线程应该差不了太多 private static DatagramChannel server=null; private static Selector selector=null; static { try{ server=DatagramChannel.open().bind(new InetSocketAddress(8889)); server.configureBlocking(false); selector=Selector.open(); }catch (Exception e){ e.printStackTrace(); } } public static void main(String

Java NIO scan through ByteBuffer for certain bytes and word with sections

こ雲淡風輕ζ 提交于 2019-12-02 05:21:21
Okay, so I'm trying to do something that seemed like it should be fairly simple, but with these new NIO interfaces, things are confusing the hell out of me! Here's what I'm trying to do, I need to scan through a file as bytes until encountering certain bytes! When I encounter those certain bytes, need to grab that segment of the data and do something with it, and then move on and do this again. I would have thought that with all these markers and positions and limits in ByteBuffer, I'd be able to do this, but I can't seem make it work! Here's what I have so far.. test.text: this is a line of

Basler USB Camera byte buffer to image conversion

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 04:44:43
问题 I have Basler acA3800 USB camera. IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException); // Image grabbed successfully? if (grabResult.GrabSucceeded) { byte[] buffer = grabResult.PixelData as byte[]; ImageWindow.DisplayImage(0, grabResult); pictureBox1.Image = ImageFromRawBgraArray(buffer,3840,2748,PixelFormat.Format8bppIndexed); MessageBox.Show(grabResult.PixelTypeValue.ToString()); } This code section shows image self window. I have pixel data of

Basler USB Camera byte buffer to image conversion

冷暖自知 提交于 2019-12-02 01:50:34
I have Basler acA3800 USB camera. IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException); // Image grabbed successfully? if (grabResult.GrabSucceeded) { byte[] buffer = grabResult.PixelData as byte[]; ImageWindow.DisplayImage(0, grabResult); pictureBox1.Image = ImageFromRawBgraArray(buffer,3840,2748,PixelFormat.Format8bppIndexed); MessageBox.Show(grabResult.PixelTypeValue.ToString()); } This code section shows image self window. I have pixel data of captured image. Original image is good but when I convert it to Image, it corrupt. And here is my