bytebuffer

transferring bytes from one ByteBuffer to another

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What's the most efficient way to put as many bytes as possible from a ByteBuffer bbuf_src into another ByteBuffer bbuf_dest (as well as know how many bytes were transferred)? I'm trying bbuf_dest.put(bbuf_src) but it seems to want to throw a BufferOverflowException and I can't get the javadocs from Sun right now (network problems) when I need them. >:( argh. edit: darnit, @Richard's approach (use put() from the backing array of bbuf_src ) won't work if bbuf_src is a ReadOnly buffer, as you can't get access to that array. What can I do in

ByteBuffer: 图解ByteBuffer(转)

試著忘記壹切 提交于 2019-12-03 02:18:34
ByteBuffer前前后后看过好几次了,实际使用也用了一些,总觉得条理不够清晰。 《程序员的思维修炼》一本书讲过,主动学习,要比单纯看资料效果来的好,所以干脆写个详细点的文章来记录一下。 概述 ByteBuffer是NIO里用得最多的Buffer,它包含两个实现方式: HeapByteBuffer 是基于 Java 堆的实现,而 DirectByteBuffer 则使用了 unsafe 的API进行了堆外的实现。这里只说HeapByteBuffer。 使用 ByteBuffer最核心的方法是 put(byte) 和 get() 。分别是往ByteBuffer里写一个字节,和读一个字节。 值得注意的是,ByteBuffer的读写模式是分开的,正常的应用场景是:往ByteBuffer里写一些数据,然后flip(),然后再读出来。 这里插两个Channel方面的对象,以便更好的理解Buffer。 ReadableByteChannel 是一个从Channel中读取数据,并保存到ByteBuffer的接口,它包含一个方法: public int read(ByteBuffer dst) throws IOException; WritableByteChannel 则是从ByteBuffer中读取数据,并输出到Channel的接口: public int write(ByteBuffer

What is the use of ByteBuffer in Java? [closed]

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What are example applications for a ByteBuffer in Java? Please list any example scenarios where this is used. Thank you! 回答1: This is a good description of its uses and shortcomings. You essentially use it whenever you need to do fast low-level I/O. If you were going to implement a TCP/IP protocol or if you were writing a database (DBMS) this class would come in handy. 回答2: The ByteBuffer class is important because it forms a basis for the use of channels in Java. ByteBuffer class defines six categories of operations upon byte buffers:

Converting YUV_420_888 to JPEG and saving file results distorted image

匿名 (未验证) 提交于 2019-12-03 01:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've used the ImageUtil class provided in https://stackoverflow.com/a/40152147/2949966 within my git repo: https://github.com/ahasbini/cameraview/tree/camera_preview_imp (note the implementation is in camera_preview_imp branch) to implement a frame preview callback. An ImageReader is set to preview frames in the ImageFormat.YUV_420_888 format which will be converted into ImageFormat.JPEG using the ImageUtil class and send it to the frame callback. The demo app saves a frame from the callback to a file every 50 frames. All of the saved frame

ZooKeeper-客户端连接ServerCnxn

匿名 (未验证) 提交于 2019-12-03 00:34:01
DirectByteBuffer ServerCnxn 代表了一个客户端与一个server的连接,其有两种实现,分别是 NIOServerCnxn 和 NettyServerCnxn ,类图如下: 本文介绍ZooKeeper是如何通过 NIOServerCnxn 实现网络IO的. 当 SocketChannel 上有数据可读时,worker thread调用 NIOServerCnxn.doIO() 进行读操作 处理读事件比较麻烦的问题就是通过TCP发送的报文会出现粘包拆包问题,Zookeeper为了解决此问题,在设计通信协议时将报文分为3个部分: 请求头和请求体的长度(4个字节) 请求头 请求体 注:(1)请求头和请求体也细分为更小的部分,但在此不做深入研究,只需知道请求的前4个字节是请求头和请求体的长度即可.(2)将请求头和请求体称之为 payload 在报文头增加了4个字节的长度字段,表示整个报文除长度字段之外的长度.服务端可根据该长度将粘包拆包的报文分离或组合为完整的报文. NIOServerCnxn 读取数据流程如下: NIOServerCnxn中有两个属性,一个是lenBuffer,容量为4个字节,用于读取长度信息.一个是incomingBuffer,其初始化时即为lenBuffer,但是读取长度信息后

t-io初学

匿名 (未验证) 提交于 2019-12-03 00:32:02
1、t-io是神马? <dependency> <groupId>org.t-io</groupId> <artifactId>tio-core</artifactId> <version>3.0.2.v20180612-RELEASE</version> </dependency> ② TioServer 不知道要用什么,就先造一个TioServer,它就会告诉接下来该干什么。 TioServer tioServer = new TioServer(serverGroupContext); 需要一个ServerGroupContext。还是老样子,造一个ServerGroupContext,就知道接下来的步骤。 ServerGroupContext serverGroupContext = new ServerGroupContext("tio-server", new ServerAioHandler(), new ServerAioListener()); 需要一个字符串String,表示的是服务端的名称。之后是ServerAioHandler和ServerAioListener,这两个是接口,需要自己实现。 先是ServerAioHandler的实现类,看其中的方法就知道是用来编码和解码,还有就是处理业务逻辑(handler方法) package com.xxjsll

byte collection based similar with ByteBuffer from java

一曲冷凌霜 提交于 2019-12-03 00:23:58
I need a C# implementation of something similar with ByteBuffer from Java. Methods of interest - .remaining() - returns the number of elements between the current position and the limit. - .array() - .clear() - .put(byte[], int, int) I started something with MemoryStream .. but no clear() , and a lot of improvisation Also, i found a c# implementation on Koders: http://www.koders.com/csharp/fid2F8CB1B540E646746D3ADCB2B0AC867A0A8DCB06.aspx?s=socket#L2 .. which I will use.. but maybe you guys know something better MemoryStream can do everything you want: .array() => .ToArray() .clear() =>

使用NIO实现一个echo服务器

匿名 (未验证) 提交于 2019-12-03 00:21:02
一个简单的echo服务器,客户端向服务端发送消息,服务端进行响应,当客户端发送quit字符串时,断开客户端和服务端的连接。使用NIO实现 public class EchoServer_NIO { private Selector selector; private ByteBuffer sendBuf = ByteBuffer.allocate(1024); private ByteBuffer recBuf = ByteBuffer.allocate(1024); public EchoServer_NIO(){ try { System.out.println("服务器正在启动"); selector = Selector.open(); ServerSocketChannel ssc = ServerSocketChannel.open(); //设置非阻塞模式 ssc.configureBlocking(false); //绑定端口 ssc.bind(new InetSocketAddress(8777)); ssc.register(selector, SelectionKey.OP_ACCEPT); System.out.println("服务器启动成功"); } catch (Exception e) { e.printStackTrace(); System

javaNio Buffer简介

好久不见. 提交于 2019-12-02 21:54:06
一,简介Buffer是java nio使用的缓冲区,所有的数据都是通过缓冲区处理的,在读取数据时,他是直接读到缓冲区中的,在写数据时,写入缓冲区,任何时候使用nio的数据,都是通过缓冲区进行操作的 缓冲区实质上是一个数组。通常它是一个字节数组(ByteBuffer),也可以使用其他种类的数组。但是一个缓冲区不仅仅是一个数组,缓冲区提供了对数据的结构化访问以及维护读写位置(limit) 等信息。 最常用的缓冲区是ByteBuffer,一个ByteBuffer提供了一组功能用于操作byte数组。 除了ByteBuffer,还有其他的一些缓冲区,事实上,每一种Java基本类型(除了Boolean 类型)都对应有一种缓冲区,具体如下。 ByteBuffer:字节缓冲区 CharBuffer:字符缓冲区 ShortBuffer:短整型缓冲区 IntBuffer:整形缓冲区 LongBuffer:长整形缓冲区 FloatBuffer:浮点型缓冲区 DoubleBuffer:双精度浮点型缓冲区 二,类主要成员变量   mark:此字段可备份读写地址   position:当前buffer读写的位置   limit:写模式-->当前最大可写长度,读模式-->当前最大可读长度   capacity:buffer当前容量   address:buffer在内存中的地址 来源: https://www

JVM(八)java堆分析

匿名 (未验证) 提交于 2019-12-02 21:53:52
上一节介绍了针对JVM的监控工具,包括JPS可以查看当前所有的java进程,jstack查看线程栈可以帮助你分析是否有死锁等情况,jmap可以导出java堆文件在MAT工具上进行分析等等。这些工具都非常有用,但要用好他们需要不断的进行实践分析。本文将介绍使用MAT工具进行java堆分析的案例。 内存溢出(OOM)的原因 堆内存溢出 只是OOM其中一种情况,OOM还可能发生在 元空间、线程栈、直接内存 。 下面演示在各个区发生OOM的情况: public static void main(String[] args) { List <Byte[]> list= new ArrayList<Byte[]> (); for ( int i=0;i<100;i++ ){ // 构造1M大小的byte数值 Byte[] bytes= new Byte[1024*1024 ]; // 将byte数组添加到list列表中,因为存在引用关系所以bytes数组不会被GC回收 list.add(bytes); } } 以上程序设置最大堆内存50M,执行: 显然程序通过循环将占用100M的堆空间,超过了设置的50M,所以发生了堆内存的OOM。 针对这种OOM,解决办法是增加堆内存空间,在实际开发中必要的时候去掉引用关系,使垃圾回收器尽快对无用对象进行回收。 元空间OOM public static