bytebuffer

ByteBuffer用法小结

只愿长相守 提交于 2019-12-05 01:05:14
在NIO中,数据的读写操作始终是与缓冲区相关联的.读取时信道(SocketChannel)将数据读入缓冲区,写入时首先要将发送的数据按顺序填入缓冲区.缓冲区是定长的,基本上它只是一个列表,它的所有元素都是基本数据类型.ByteBuffer是最常用的缓冲区,它提供了读写其他数据类型的方法,且信道的读写方法只接收ByteBuffer.因此ByteBuffer的用法是有必要牢固掌握的. 1.创建ByteBuffer 1.1 使用allocate()静态方法 ByteBuffer buffer=ByteBuffer.allocate(256); 以上方法将创建一个容量为256字节的ByteBuffer,如果发现创建的缓冲区容量太小,唯一的选择就是重新创建一个大小合适的缓冲区. 1.2 通过包装一个已有的数组来创建 如下,通过包装的方法创建的缓冲区保留了被包装数组内保存的数据. ByteBuffer buffer=ByteBuffer.wrap(byteArray); 如果要将一个字符串存入ByteBuffer,可以如下操作: String sendString="你好,服务器. "; ByteBuffer sendBuffer=ByteBuffer.wrap(sendString.getBytes("UTF-16")); 2.回绕缓冲区 buffer.flip();

Fast ByteBuffer to CharBuffer or char[]

血红的双手。 提交于 2019-12-05 00:07:55
问题 What is the fastest method to convert a java.nio.ByteBuffer a into a (newly created) CharBuffer b or char[] b . By doing this it is important, that a[i] == b[i] . This means, that not a[i] and a[i+1] together make up a value b[j] , what getChar(i) would do, but the values should be "spread". byte a[] = { 1,2,3, 125,126,127, -128,-127,-126 } // each a byte (which are signed) char b[] = { 1,2,3, 125,126,127, 128, 129, 130 } // each a char (which are unsigned) Note that byte:-128 has the same

How to put data from an OutputStream into a ByteBuffer?

怎甘沉沦 提交于 2019-12-04 22:32:48
In Java I need to put content from an OutputStream (I fill data to that stream myself) into a ByteBuffer. How to do it in a simple way? DJClayworth You can create a ByteArrayOutputStream and write to it, and extract the contents as a byte[] using toByteArray() . Then ByteBuffer.wrap(byte []) will create a ByteBuffer with the contents of the output byte array. mark There is a more efficient variant of @DJClayworth's answer. As @seh correctly noticed, ByteArrayOutputStream.toByteArray() returns a copy of the backing byte[] object, which may be inefficient. However, the backing byte[] object as

How to get data from TCP socket into a ByteBuffer

回眸只為那壹抹淺笑 提交于 2019-12-04 19:21:09
I need to get incoming data from a socket into a ByteBuffer and I do not know how to do it. I am new to this field and therefore not sure of the best way to start. I found the following but that is not what I want as it gets the data in line but I need to have all of my data in bytebuffer for other purposes. ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(ibmPort)); while (true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new

Equivalent of Java's “ByteBuffer.putType()” in C#

会有一股神秘感。 提交于 2019-12-04 14:08:19
I am trying to format a byte array in C#, by porting a code from Java. In Java, the methods "buf.putInt(value);", buf.putShort, buf.putDouble, (and so forth) are used. However I don't know how to port this to C#. I have tried the MemoryStream class, but there is no method to put a specific type at the end of the byte array. Question: What is the equivalent of Java's "ByteBuffer.putType(value)" in C#? Thanks! You can use a BinaryWriter and your MemoryStream: MemoryStream stream = new MemoryStream(); using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(myByte); writer.Write

浅尝JavaNIO

百般思念 提交于 2019-12-04 11:57:57
本文将通过JavaNIO实现文件的局域网发送功能。 目的: 1.为了体验零拷贝 2.为了体验NIO的Channel,Buffer,Selector 3.为了体验NIO方式网络传输文件和传统网络方式传输文件的差异(性能差异)。 什么是零拷贝:   那先来说说传统的文件传输:在本地读取文件(FileInputStream)通过网络传输(socket.getInputStream,socket.getOutputstream)发送到另一台设备进行存储(FileOutPutStream)过程,以及在操作这些流所用到的缓存(自定义byte数组或者各种BufferdInputStream/BufferdOutputStream)中,这些操作都在我们浅显来看,是这样的:磁盘——网络——磁盘的过程;稍微深一层次的,应该是这样的:磁盘—内存—网络—内存—磁盘的过程;再深一层次的,应该是这样:磁盘—操作系统内存(内核态-从磁盘读取过来)—应用程序内存(用户态)—操作系统内存(内核态-网络准备发送)—网络中传输—操作系统内存(内核态-网络接收)—应用程序内存(用户态)—操作系统内存(内核态-准备存磁盘)—磁盘。   重点来了,零拷贝(需要操作系统支持)就是用来减少甚至杜绝操作系统内存(内核态)到应用程序内存(用户态)的拷贝过程的。让cpu不要浪费在这种内存间拷贝的操作上,而是用在其他高效的计算上

BIO/NIO/AIO

女生的网名这么多〃 提交于 2019-12-04 11:44:22
1. 最简单的网络通信——同步阻塞通信(BIO) 首先来看一个传统简单的网络通信案例,该案例是基于同步阻塞的I/O,服务端代码如下 public class Server extends Thread{ private ServerSocket serverSocket; public Server(int port) throws IOException { serverSocket = new ServerSocket(port, 1000); //端口号,以及运行连接可以保存的最长队列 serverSocket.setSoTimeout(1000000); } public void run() { while(true) { try { System.out.println("等待远程连接,端口号为:" + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept(); System.out.println("远程主机地址:" + server.getRemoteSocketAddress()); DataInputStream in = new DataInputStream(server.getInputStream()); Thread.sleep(2000); System

Java - When does direct buffer released?

感情迁移 提交于 2019-12-04 11:31:47
问题 Since it's out of jvm heap & gc, when does it released? Or, it remain until process termination? I already checked: how to garbage collect a direct buffer java Deallocating Direct Buffer Native Memory in Java for JOGL ByteBuffer.allocate() vs. ByteBuffer.allocateDirect() But all the answers are blur, none answered it explicitly, does there have a clear answer? At least for Java 8 on 64-bit Linux . 回答1: DirectByteBuffer does not use old Java finalizers. Instead, it uses internal sun.misc

How can I determine the length of received bytes of UsbRequest.queue(..) method?

被刻印的时光 ゝ 提交于 2019-12-04 06:38:36
I have troubles with UsbRequest class in Android 3.1. This is my code: ByteBuffer buffer = ByteBuffer.allocate(4096); buffer.order(ByteOrder.LITTLE_ENDIAN); UsbRequest request = new UsbRequest(); request.initialize(mConnection, mEndpointIn); request.queue(buffer, 4096); if (mConnection.requestWait() == request) { byte[] data = buffer.array(); } The size of array data is 4096, but the length of really received bytes is much more smaller. How can i determine the size of really received bytes? Thanks. This was a bug in Android . On afflicted versions, there's no workaround, because the

How write big endian ByteBuffer to little endian in Java

房东的猫 提交于 2019-12-04 04:03:13
I currently have a Java ByteBuffer that already has the data in Big Endian format. I then want to write to a binary file as Little Endian. Here's the code which just writes the file still in Big Endian: public void writeBinFile(String fileName, boolean append) throws FileNotFoundException, IOException { FileOutputStream outStream = null; try { outStream = new FileOutputStream(fileName, append); FileChannel out = outStream.getChannel(); byteBuff.position(byteBuff.capacity()); byteBuff.flip(); byteBuff.order(ByteOrder.LITTLE_ENDIAN); out.write(byteBuff); } finally { if (outStream != null) {