bytebuffer

网络编程之NIO

↘锁芯ラ 提交于 2019-12-08 23:36:28
一、Buffer缓冲区 package com.itbac.net.NIO.BIO; import java.nio.ByteBuffer; /** * Buffer缓冲区 */ public class BufferDemo { public static void main(String[] args) { //构建一个byte 字节缓冲区,容量是4 // allocate分配内存(在堆中)堆中内存要复制到堆外才能用于网络通信,因为堆中有GC管理 // allocateDirect 分配内存(在堆外),减少一次内存复制操作,提高性能,但是要自己回收内存 ByteBuffer byteBuffer = ByteBuffer.allocate(4); System.out.println(String.format("初始化:capacity容量:%s,position位置:%s,limit限制:%s", byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit())); //写入3字节的数据 byteBuffer.put((byte) 1); byteBuffer.put((byte) 2); byteBuffer.put((byte) 3); System.out.println(String.format(

How can I get short[] from a ByteBuffer

青春壹個敷衍的年華 提交于 2019-12-08 16:43:50
问题 I am using JNI code in an Android project in which the JNI native function requires a short[] argument. However, the original data is stored as a ByteBuffer. I'm trying the convert the data format as follows. ByteBuffer rgbBuf = ByteBuffer.allocate(size); ... short[] shortArray = (short[]) rgbBuf.asShortBuffer().array().clone(); But I encounter the following problem when running the second line of code shown above: E/AndroidRuntime(23923): Caused by: java.lang.UnsupportedOperationException E

Send data in multiple ways depending on how you want to send it

我怕爱的太早我们不能终老 提交于 2019-12-08 16:37:32
问题 I have bunch of keys and values that I want to send to our messaging queue by packing them in one byte array. I will make one byte array of all the keys and values which should always be less than 50K and then send to our messaging queue. Packet class : public final class Packet implements Closeable { private static final int MAX_SIZE = 50000; private static final int HEADER_SIZE = 36; private final byte dataCenter; private final byte recordVersion; private final long address; private final

Efficient way to convert io.netty.buffer.ByteBuf to java.nio.ByteBuffer

 ̄綄美尐妖づ 提交于 2019-12-08 15:53:58
问题 I came across this query: Create a ByteBuf in Netty 4.0 about conversion from byte[] to ByteBuf and ByteBuffer to ByteBuf. I was curious to know about the conversion the other way: io.netty.buffer.ByteBuf to java.nio.ByteBuffer and how to do it efficiently, with minimal/no copying? I did some reading and with some trial and error I found this inefficient way of converting it (with two copies): // io.netty.handler.codec.http.FullHttpRequest fullHttpRequest; ByteBuf conByteBuf = fullHttpRequest

NIO 聊天室代码实现

烂漫一生 提交于 2019-12-08 13:58:26
服务器端 package com.ronnie.nio.groupChat; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; public class GroupChatServer { private Selector selector; private ServerSocketChannel listenChannel; private static final int PORT = 9999; /* 构造器 初始化任务 */ public GroupChatServer(){ try { // 得到选择器 selector = Selector.open(); // serverSocketChannel listenChannel = ServerSocketChannel.open(); // 绑定端口 listenChannel.socket().bind(new InetSocketAddress(PORT)); // 设置非阻塞模式 listenChannel.configureBlocking(false); //

NIO组件Channel

徘徊边缘 提交于 2019-12-07 13:04:33
基本介绍 NIO的通道类似于流, 但有些区别: 通道可以 同时 进行读写, 而流只能读或者只能写 通道可以实现 异步 读写数据 通道可以从缓冲区(Buffer)读数据, 也可以写数据到缓冲区 BIO中的stream是单向的, 例如 FileInputStream 对象只能进行读取数据的操作, 而NIO中的通道(Channel)是双向的, 可以读操作, 也可以写操作。 Channel在NIO中是一个接口: public interface Channel extends Closeable{} 常用的 Channel类有: FileChannel、DatagramChannel、ServerSocketChannel 和 SocketChannel。 FileChannel 用于文件的数据读写, DatagramChannel 用于UDP的数据读写, ServerSocketChannel 和 SocketChannel 用于 TCP 的数据读写。 FileChannel类 FileChannel主要用于对本地文件进行IO操作, 常见方法有: public int read(ByteBuffer dst): 从通道读取数据并放到缓冲区中 public int write(ByteBuffer src): 把缓冲区的数据写到通道中 public long transferFrom

FTP 上传下载 进度条

。_饼干妹妹 提交于 2019-12-06 10:32:46
11 /// <summary> /// 文件上传 /// </summary> /// <param name="filePath">原路径(绝对路径)包括文件名</param> /// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param> public static bool FileUpLoad(string filePath, string objPath = "") { bool isOk = false; string url = path; if (objPath != "") url += objPath + "/"; FtpWebRequest reqFTP = null; ProgressBarForm progressBarForm = null; try { FileInfo fileInfo = new FileInfo(filePath); reqFTP = getFtpWebRequest(url, fileInfo.Name); progressBarForm = new ProgressBarForm(); progressBarForm.Show(); using (FileStream fs = fileInfo.OpenRead()) using (Stream stream =

Java NIO:Buffer、Channel 和 Selector

久未见 提交于 2019-12-06 09:59:46
本文将介绍 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 中也有几个重要属性:position、limit、capacity。 最好理解的当然是

JAVA NIO Bytebuffer.allocateDirect() size limit over the int

风格不统一 提交于 2019-12-06 08:31:51
I am trying to make off-heap memory buffer. I want very big size(like 10GB) buffer. I heard that jvm heap sometimes freeze because full GC. So, I try to make buffer with java.nio.ByteBuffer. But, I met great difficulties! java.nio.ByteBuffer.allocateDirect(int size) function only support integer. but I want bigger size. what can I do? what should I do? please help me stack overflow gurus. my development environment is macbook pro, i7 2.4ghz, 16gb ddr3, 250ssd, osx 10.9, eclipse kepler x64. I try something for solve problem: ByteBuffer buffer = ByteBuffer.allocateDirect(1024*1024*2000);

NIO编程之多客户端聊天系统

这一生的挚爱 提交于 2019-12-06 05:40:07
1. 服务端 import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; import java.util.Set; public class GroupChatServer { private Selector selector; private ServerSocketChannel listenChannel; private static final int PORT = 8888; // 构造 器 public GroupChatServer() { try { selector = Selector.open(); listenChannel = ServerSocketChannel.open(); // 绑定端口 listenChannel.socket().bind(new InetSocketAddress(PORT)); // 设置非阻塞模式 listenChannel.configureBlocking(false); // 将listenChannel注册到selector listenChannel.register