bytebuffer

NIO - Buffer、Channel 和 Selector

孤人 提交于 2019-11-30 23:19:45
转载 https://javadoop.com/post/java-nio 本文将介绍 Java NIO 中三大组件 Buffer、Channel、Selector 的使用。 本来要一起介绍 非阻塞 IO 和 JDK7 的 异步 IO 的,不过因为之前的文章真的太长了,有点影响读者阅读,所以这里将它们放到另一篇文章中进行介绍。 Buffer 一个 Buffer 本质上是内存中的一块,我们可以将数据写入这块内存,之后从这块内存获取数据。 java.nio 定义了以下几个 Buffer 的实现,这个图读者应该也在不少地方见过了吧。 其实核心是最后的 ByteBuffer ,前面的一大串类只是包装了一下它而已,我们使用最多的通常也是 ByteBuffer。 我们应该将 Buffer 理解为一个数组,IntBuffer、CharBuffer、DoubleBuffer 等分别对应 int[]、char[]、double[] 等。 MappedByteBuffer 用于实现内存映射文件,也不是本文关注的重点。 我觉得操作 Buffer 和操作数组、类集差不多,只不过大部分时候我们都把它放到了 NIO 的场景里面来使用而已。下面介绍 Buffer 中的几个重要属性和几个重要方法。 position、limit、capacity 就像数组有数组容量,每次访问元素要指定下标,Buffer

A simple rule of when I should use direct buffers with Java NIO for network I/O?

有些话、适合烂在心里 提交于 2019-11-30 16:54:15
问题 Can someone with the natural gift to explain complex things in an easy and straightforward way address this question? To acquire the best performance when should I use direct ByteBuffers versus regular ByteBuffers when doing network I/O with Java NIO? For example: Should I read into a heap buffer and parse it from there, doing many get() (byte by byte) OR should I read it into a direct buffer and parse from the direct buffer? 回答1: To acquire the best performance when should I use direct

How to initialize a ByteBuffer if you don't know how many bytes to allocate beforehand?

那年仲夏 提交于 2019-11-30 14:52:19
问题 Is this: ByteBuffer buf = ByteBuffer.allocate(1000); ...the only way to initialize a ByteBuffer ? What if I have no idea how many bytes I need to allocate..? Edit: More details: I'm converting one image file format to a TIFF file. The problem is the starting file format can be any size, but I need to write the data in the TIFF to little endian. So I'm reading the stuff I'm eventually going to print to the TIFF file into the ByteBuffer first so I can put everything in Little Endian, then I'm

Deep copy duplicate() of Java's ByteBuffer

℡╲_俬逩灬. 提交于 2019-11-30 13:44:28
问题 java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer? 回答1: I think the deep copy need not involve byte[] . Try the following: public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original); original.rewind

3. Netty源码阅读之Channel

﹥>﹥吖頭↗ 提交于 2019-11-30 12:20:42
一、Channel介绍   Channel中的NioServerSocketChannel 和 NioSocketChannel 分别于 NIO中的 ServerSocketChannel、SocketChannel对应。不同的是,Netty的Channel将NIO中的Channel聚合在自己对象内部,并提供其他的功能操作。 二、Channel源码介绍 1. 常用方法介绍 eventLoop() Channel需要注册到EventLoop上的多路复用器上,通过该方法可获取到Channel注册的EventLoop(EventLoop本质就是处理网络读写事件的Reactor线程) metadata() 获取当前Channel的TCP参数配置 parent() 对于服务端而言,它的parent为空;对于客户端而言,它的父Channel就是创建它的ServerSocketChannel id() 获取Channel唯一标识对象 2. NioServerSocketChannel 和 NioSocketChannel 继承关系图 3. AbstractChannel源码分析   3.1 成员变量 private final Channel parent;//父类channel private final ChannelId id; //Channel唯一标识 private final

1. Netty准备知识:Java NIO

≡放荡痞女 提交于 2019-11-30 12:20:35
前言:我们知道,Netty是基于NIO开发的一套框架,在学习Netty之前,我们先学习下Java NIO。 一、IO多路复用模型   IO多路复用模型使用了Reactor设计模式,主要有三种实现:Reacotr单线程、Reactor多线程、Reactor主从模式。 1. Reactor单线程   在Reactor单线程模式中,所有客户端的请求处理都交给一个线程,串行化处理,效率较低。 2. Reactor多线程   在Reactor多线程模式中,acceptor线程负责接受客户端请求并将请求处理任务交给线程池,提升了请求处理速度。但是当client数量过多时,单线程就无法同时处理那么多的请求,造成瓶颈问题。 3. Reactor主从模式   为了解决Reactor多线程请求转发瓶颈问题,Reactor主从模式将acceptor设计为线程池,用以处理客户端请求。 二、NIO使用示例 1. NIO服务端通讯示例 public class TimeServer { public static void main(String[] args) throws IOException { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch

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

試著忘記壹切 提交于 2019-11-30 11:18:17
问题 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. 回答1: Look at Channels.newChannel(OutputStream). It will give you a channel given an

[Android 进阶]MediaCodec简介

两盒软妹~` 提交于 2019-11-30 10:30:10
[Android 进阶]MediaCodec简介 由于前段时间单位处理过录屏直播之类的需求,这边首推的一个系列着力于MediaCodec,该系列包含下面几片文章: [Android 进阶]MediaCodec系列之MediaCodec简介 [Android 进阶]MediaCodec系列之MediaCodec+MediaEctractor播放器 [Android 进阶]MediaCodec+MediaProjection实现录制屏幕 [Android 进阶]MediaCodec+Camera API实现摄像头录制视频(上) [Android 进阶]MediaCodec+Camera API实现摄像头录制视频(下) 本篇文章是Google官方文档的翻译,其中生命周期的概念是我重新整理过的,原文地址: https://developer.android.google.cn/reference/android/media/MediaCodec.html MediaCodec是用来访问系统底层编解码器的一个类,通常与MediaExtractor, MediaSync, MediaMuxer, MediaCrypto, MediaDrm, Image, Surface, AudioTrack等一起使用。作为与底层编解码器交流的工作类,MediaCodec的一般数据流图如下[官网图]:

Get the pointer of a Java ByteBuffer though JNI

走远了吗. 提交于 2019-11-30 09:20:15
How can I get a pointer to the inner array of a Java ByteBuffer? JNIEXPORT void JNICALL test(JNIEnv *env, jobject thiso) { jclass cls = env->FindClass("java/nio/ByteBuffer"); jmethodID aloc = env->GetStaticMethodID(cls, "allocateDirect", "(I)Ljava/nio/ByteBuffer;"); jobject obj = env->CallStaticObjectMethod(cls, aloc, 1000); } PS: I'm doing that to share the memory used by Java and C++. void * data = env->GetDirectBufferAddress(obj); The ByteBuffer must be a direct one for this to work. If you're trying to return the address of the first element within m_buffer , then you can just do: return m

Deep copy duplicate() of Java's ByteBuffer

吃可爱长大的小学妹 提交于 2019-11-30 08:04:45
java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer? mingfai I think the deep copy need not involve byte[] . Try the following: public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original); original.rewind(); clone.flip(); return clone; } jdmichal As this question still comes up as one of the first hits