bytebuffer

Compare Direct and Non-Direct ByteBuffer get/put operations

陌路散爱 提交于 2019-11-30 07:18:48
Is get/put from a non-direct bytebuffer faster than get/put from direct bytebuffer ? If I have to read / write from direct bytebuffer , is it better to first read /write in to a thread local byte array and then update ( for writes ) the direct bytebuffer fully with the byte array ? Is get/put from a non-direct bytebuffer faster than get/put from direct bytebuffer ? If you are comparing heap buffer with direct buffer which does not use native byte order (most systems are little endian and the default for direct ByteBuffer is big endian), the performance is very similar. If you use native

nio读取文件,输出文件

自作多情 提交于 2019-11-30 05:44:12
io流的一种: package com.cxy.ssp.Automic; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import java.util.concurrent.atomic.AtomicStampedReference; //通过nio实现文件io public class Demo3 { public static void main(String[] args) throws Exception{ //1 创建输出流 FileOutputStream fileOutputStream = new FileOutputStream("bas.txt");    //构建通道 FileChannel channel = fileOutputStream.getChannel();     //创建缓存区 ByteBuffer buffer =

Is there a way to create a direct ByteBuffer from a pointer solely in Java?

試著忘記壹切 提交于 2019-11-30 05:41:06
Or do I have to have a JNI helper function that calls env->NewDirectByteBuffer(buffer, size)? What I do is create a normal DirectByteBuffer and change it's address. Field address = Buffer.class.getDeclaredField("address"); address.setAccessible(true); Field capacity = Buffer.class.getDeclaredField("capacity"); capacity.setAccessible(true); ByteBuffer bb = ByteBuffer.allocateDirect(0).order(ByteOrder.nativeOrder()); address.setLong(bb, addressYouWantToSet); capacity.setInt(bb, theSizeOf); From this point you can access the ByteBuffer referencing the underlying address. I have done this for

分布式Java--基于消息方式实现系统间通信

拈花ヽ惹草 提交于 2019-11-30 01:52:43
分布式系统之间通信可以分为两种: 基于消息方式实现系统间通信 基于远程调用方式实现系统间通信 基于消息方式实现系统间通信 分布式子系统之间需要通信时,就发送消息。一般通信的两个要点是:消息处理和消息传输。 消息处理:例如读取数据和写入数据。基于消息方式实现系统通信的消息处理可以分为同步消息和异步消息。同步消息一般采用的是BIO(Blocking IO)和NIO(Non-Blocking IO);异步消息一般采用AIO方式。 消息传输:消息传输需要借助网络协议来实现,TCP/IP协议和UDP/IP协议可以用来完成消息传输。 术语解释: BIO:同步阻塞IO。就是当发生IO的读或者写操作时,均为阻塞操作。只有程序读到了流或者将流写入操作系统后,才会释放资源。 NIO: 同步非阻塞IO。是基于事件驱动思想的。从程序角度想,当发起IO的读和写操作时,是非阻塞的。当Socket有流可读或者可以写Socket时,操作系统会通知应用程序进行处理,应用再将流读取到缓冲区或操作系统。 AIO: 异步IO。同样基于事件驱动思想。当有流可读取时,操作系统会将流读取到read方法的缓冲区,然后通知应用程序;对于写操作,操作系统将write方法传入的流写入完毕时,操作系统主动通知应用程序。 TCP/IP: 一种可靠的网络数据传输协议。要求通信双方先建立连接,再进行通信。 UDP/IP:

Java NIO和IO的区别

*爱你&永不变心* 提交于 2019-11-30 01:28:17
下表总结了Java NIO和IO之间的主要差别,我会更详细地描述表中每部分的差异。 复制代码 代码如下: IO NIO 面向流 面向缓冲 阻塞IO 非阻塞IO 无 选择器 面向流与面向缓冲 Java NIO和IO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的。 Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方。此外,它不能前后移动流中的数据。如果需要前后移动从流中读取的数据,需要先将它缓存到一个缓冲区。 Java NIO的缓冲导向方法略有不同。数据读取到一个它稍后处理的缓冲区,需要时可在缓冲区中前后移动。这就增加了处理过程中的灵活性。但是,还需要检查是否该缓冲区中包含所有您需要处理的数据。而且,需确保当更多的数据读入缓冲区时,不要覆盖缓冲区里尚未处理的数据。 阻塞与非阻塞IO Java IO的各种流是阻塞的。这意味着,当一个线程调用read() 或 write()时,该线程被阻塞,直到有一些数据被读取,或数据完全写入。该线程在此期间不能再干任何事情了。 Java NIO的非阻塞模式,使一个线程从某通道发送请求读取数据,但是它仅能得到目前可用的数据,如果目前没有数据可用时,就什么都不会获取。而不是保持线程阻塞,所以直至数据变的可以读取之前,该线程可以继续做其他的事情。 非阻塞写也是如此。一个线程请求写入一些数据到某通道

2019-09-18 关键字匹配文件内容--搜索文件

空扰寡人 提交于 2019-11-30 00:32:30
一、程序描述   1、搜索出某个路径下的所有文件夹。   2、获取文件夹中的内容,与关键字进行匹配。   3、可以只查包含某后缀文件。   4、 只能 有效执行能被记事本打开不乱码的文件。 二、程序代码 /* 获取某文件夹中所有的文件, 获取文件中的内容,在文件的内容上进行相关搜索 */ public class SearchFileText { private ByteBuffer buffer; public static void main(String[] args) throws Exception{ //只支持记事本能打开不乱码的文件 setCond("D:\\工作日记\\2019年\\OA新通用移动模板\\work",".php","edit.php"); //setCond("D:\\工作日记\\2019年\\OA新通用移动模板\\work","[all]","edit.php"); } /* filePath 从哪个文件夹开始查, sufStr 只查哪些文件, searchStr 查询的内容 */ public static void setCond(String filePath,String sufStr,String searchStr) throws Exception{ File file = new File(filePath); File[]

简单认识java中的bytebuffer和netty中的bytebuf

帅比萌擦擦* 提交于 2019-11-30 00:32:21
一 nio中的bytebuffer的认识 1.bytebuffer的数据结构 对于ByteBuffer,其主要有五个属性:mark,position,limit,capacity和array。这五个属性的作用如下: mark:记录了当前所标记的索引下标; position:对于写入模式,表示当前可写入数据的下标,对于读取模式,表示接下来可以读取的数据的下标; limit:对于写入模式,表示当前可以写入的数组大小,默认为数组的最大长度,对于读取模式,表示当前最多可以读取的数据 的位置下标; capacity:表示当前数组的容量大小; array:保存了当前写入的数据。 这几个数据中,除了array是用于保存数据的以外,这里最终的主要是position,limit和capacity三个属性,因为对于写入和读取模式,这三个属性的表示的含义大不一样。 1.1写入模式: 如下图所示为初始状态和写入3个字节之后position,limit和capacity三个属性的状态: 从图中可以看出,在写入模式下,limit指向的始终是当前可最多写入的数组索引下标,position指向的则是下一个可以写入的数据的索引位置,而capacity则始终不会变化,即为数组大小。 注意:图片来源知乎,如果侵权,联系我删除 1.2读取模式 假设我们按照上述方式在初始长度为6的ByteBuffer中写入了三个字节的数据

JNI - native method with ByteBuffer parameter

时光毁灭记忆、已成空白 提交于 2019-11-30 00:22:47
I've got a method: public native void doSomething(ByteBuffer in, ByteBuffer out); Generated by javah C/C++ header of this method is: JNIEXPORT void JNICALL Java__MyClass_doSomething (JNIEnv *, jobject, jobject, jobject, jint, jint); How can I get a data array from jobject (that is a ByteBuffer instance) ? stacker Assuming you allocated the ByteBuffer using ByteBuffer.allocateDirect() you can use GetDirectBufferAddress jbyte* bbuf_in; jbyte* bbuf_out; bbuf_in = (*env)->GetDirectBufferAddress(env, buf1); bbuf_out= (*env)->GetDirectBufferAddress(env, buf2); 来源: https://stackoverflow.com/questions

How to put the content of a ByteBuffer into an OutputStream?

走远了吗. 提交于 2019-11-29 23:39:41
I need to put the contents of a java.nio.ByteBuffer into an java.io.OutputStream . (wish I had a Channel instead but I don't) What's the best way to do this? I can't use the ByteBuffer's array() method since it can be a read-only buffer. I also may be interspersing writes to the OutputStream between using this ByteBuffer and having a regular array of byte[] which I can with use OutputStream.write() directly. ng. Look at Channels.newChannel(OutputStream) . It will give you a channel given an OutputStream. With the WritableByteChannel adapter you can provide the ByteBuffer which will write it to

Java基础的实际应用

↘锁芯ラ 提交于 2019-11-29 22:00:05
IO流 java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中。 字节流基类 Inputstream OutputStream 字节文件操作流 FileInputStream FileOutputStream 字节缓冲流(高效流) BufferedInputStream BufferedOutputStream NIO 三大组件:chanel、buffer、selector ###FileChannel和buffer的简单使用 ​ RandomAccessFile accessFile = null; try { accessFile = new RandomAccessFile("test.txt", "rw"); FileChannel channel = accessFile.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead; while ((bytesRead = channel.read(buffer)) != -1) { buffer.flip(); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); }