bytebuffer

网络编程之NIO

牧云@^-^@ 提交于 2019-11-26 19:32:59
概述: 网络编程IO主要分为:同步、异步、阻塞、非阻塞。 在Java1.4之前的I/O系统中,提供的都是面向流的I/O系统,系统一次一个字节地处理数据,一个输入流产生一个字节的数据,一个输出流消费一个字节的数据,面向流的I/O速度非常慢,而在Java 1.4中推出了NIO,这是一个面向块的I/O系统,系统以块的方式处理处理,每一个操作在一步中产生或者消费一个数据库,按块处理要比按字节处理数据快的多。 在NIO中有几个核心对象需要掌握:缓冲区(Buffer)、通道(Channel)、选择器(Selector)。 Channel channel是个双向通道和BIO的Stream不同,Stream是单向通道。但他们都用于数据的读写。 Channel的实现 FileChannel (从文件中) DatagramChannel (从UDP中) SocketChannel (通过TCP读写) ServerSocketChannel (监听新进来的TCP连接) 打开 SocketChannel: SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80)); 从 SocketChannel 读取数据:

Java: Converting String to and from ByteBuffer and associated problems

谁说胖子不能爱 提交于 2019-11-26 18:51:32
问题 I am using Java NIO for my socket connections, and my protocol is text based, so I need to be able to convert Strings to ByteBuffers before writing them to the SocketChannel, and convert the incoming ByteBuffers back to Strings. Currently, I am using this code: public static Charset charset = Charset.forName("UTF-8"); public static CharsetEncoder encoder = charset.newEncoder(); public static CharsetDecoder decoder = charset.newDecoder(); public static ByteBuffer str_to_bb(String msg){ try{

ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()

心已入冬 提交于 2019-11-26 12:01:56
To allocate() or to allocateDirect() , that is the question. For some years now I've just stuck to the thought that since DirectByteBuffer s are a direct memory mapping at OS level, that it would perform quicker with get/put calls than HeapByteBuffer s. I never was really interested in finding out the exact details regarding the situation until now. I want to know which of the two types of ByteBuffer s are faster and on what conditions. Ron Hitches in his excellent book Java NIO seems to offer what I thought could be a good answer to your question: Operating systems perform I/O operations on

How to convert a double into a byte array in swift?

女生的网名这么多〃 提交于 2019-11-26 10:53:37
问题 I know how to do it in java (see here), but I couldn\'t find a swift equivalent for java\'s ByteBuffer, and consequently its .putDouble(double value) method. Basically, I\'m looking for a function like this: func doubleToByteArray(value: Double) -> [UInt8]? { . . . } doubleToByteArray(1729.1729) // should return [64, 155, 4, 177, 12, 178, 149, 234] 回答1: typealias Byte = UInt8 func toByteArray<T>(var value: T) -> [Byte] { return withUnsafePointer(&value) { Array(UnsafeBufferPointer(start:

ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()

被刻印的时光 ゝ 提交于 2019-11-26 03:36:14
问题 To allocate() or to allocateDirect() , that is the question. For some years now I\'ve just stuck to the thought that since DirectByteBuffer s are a direct memory mapping at OS level, that it would perform quicker with get/put calls than HeapByteBuffer s. I never was really interested in finding out the exact details regarding the situation until now. I want to know which of the two types of ByteBuffer s are faster and on what conditions. 回答1: Ron Hitches in his excellent book Java NIO seems

Byte array to image conversion

蓝咒 提交于 2019-11-26 01:36:22
问题 I want to convert a byte array to an image. This is my database code from where I get the byte array: public void Get_Finger_print() { try { using (SqlConnection thisConnection = new SqlConnection(@\"Data Source=\" + System.Environment.MachineName + \"\\\\SQLEXPRESS;Initial Catalog=Image_Scanning;Integrated Security=SSPI \")) { thisConnection.Open(); string query = \"select pic from Image_tbl\";// where Name=\'\" + name + \"\'\"; SqlCommand cmd = new SqlCommand(query, thisConnection); byte[]

converting Java bitmap to byte array

回眸只為那壹抹淺笑 提交于 2019-11-26 00:50:13
问题 Bitmap bmp = intent.getExtras().get(\"data\"); int size = bmp.getRowBytes() * bmp.getHeight(); ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); byte[] bytes = new byte[size]; try { b.get(bytes, 0, bytes.length); } catch (BufferUnderflowException e) { // always happens } // do something with byte[] When I look at the buffer after the call to copyPixelsToBuffer the bytes are all 0... The bitmap returned from the camera is immutable... but that shouldn\'t matter since it\'s

Netty学习笔记

不打扰是莪最后的温柔 提交于 2019-11-25 22:59:18
Netty简单认识:   1) Netty 是由JBOSS 提供的一个Java 开源框架。   2) Netty 是一个异步的、基于事件驱动的网络应用框架,用以快速开发高性能、高可靠性的网络I0 程序。   3) Netty 主要针对在TCP协议下的使用   4)Netty本质是- 个NIO框架,适用于服务器通讯相关的多种应用场景 Netty应用:    https://netty.io/wiki/related-projects.html 这里面是和netty有关的框架   Netty应用于网络间的通信,如阿里的dubbo框架,应用于服务之间的调用;谷歌的grpc框架; I/O模型:    netty是基于nio开发,所以我们需要了解java中的I/O模型: Bio:同步阻塞io,服务器实现模式为一 一个连接-一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理   如果这个连接不做任何事情会造成不必要的线程开销,适用于连接数小的和固定的,编程简单。 Nio:同步非阻塞,JavaNIO :同步非阻塞, 服务器实现模式为- -个线程处理多个请求(连接),即客户端发送的连接请求   都会注册到多路复用器上,多路复用器轮询到连接有I/O请求就进行处理,适用于连接数多的连接时间短的,编程复杂。(1.4以后版本) Aio:异步非阻塞,有效的请求才启动线程

converting Java bitmap to byte array

巧了我就是萌 提交于 2019-11-25 20:22:26
Bitmap bmp = intent.getExtras().get("data"); int size = bmp.getRowBytes() * bmp.getHeight(); ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); byte[] bytes = new byte[size]; try { b.get(bytes, 0, bytes.length); } catch (BufferUnderflowException e) { // always happens } // do something with byte[] When I look at the buffer after the call to copyPixelsToBuffer the bytes are all 0... The bitmap returned from the camera is immutable... but that shouldn't matter since it's doing a copy. What could be wrong with this code? Mezm Try something like this: Bitmap bmp = intent