bytebuffer

Java: Converting String to and from ByteBuffer and associated problems

随声附和 提交于 2019-11-27 17:06:05
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{ return encoder.encode(CharBuffer.wrap(msg)); }catch(Exception e){e.printStackTrace();} return null; }

Netty 学习笔记

这一生的挚爱 提交于 2019-11-27 16:54:46
       Netty是由JBOSS提供的一个java开源框架。Netty提供 异步的 、 事件驱动 的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。     Netty基于java的NIO核心: 缓存区、通道、选择器 。 这里推荐一本书:java NIO,出版社是O'REILLY。   一、缓存区。     缓冲区的工作与通道紧密联系,通道是I/O传输发生时通过的入口,而 缓冲区是这些数据传输的来源或目标 。对于离开缓冲区的传输,你想传递出去的数据被置于一个缓冲区,被传送到通道。对于传回缓冲区的传输,一个通道将数据放置在你所提供的缓冲区中。这种在协同对象之间进行的缓冲区数据传递是高效数据处理的关键。     缓冲区的属性: 容量 (capacity能够容纳的数据元素的最大数量)、 上界 (limit现存元素的计数)、 标记 (mark一个备忘位置)、 位置 (Posistion下一个要被读或写的元素的索引),还有缓冲区操作:翻转、压缩等。     这里只简介那个位置指针就好了,一切从它开始。     初始状态:一个空的字节缓冲区,position指向0              接着我们写入一些数据,例如:     byteBuffer.put(110);     byteBuffer.put(120);     现在的状态是

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

*爱你&永不变心* 提交于 2019-11-27 16:45:27
What are example applications for a ByteBuffer in Java? Please list any example scenarios where this is used. Thank you! kelloti 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. ykombinator 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, as stated in the Java 7 documentation : Absolute

NIO网络编程

懵懂的女人 提交于 2019-11-27 15:51:50
1、创建服务端代码 public class NioServer { private static Map<String, SocketChannel> clientMap = new HashMap<>(); public static void main(String[] args) throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); ServerSocket serverSocket = serverSocketChannel.socket(); serverSocket.bind(new InetSocketAddress(8899)); Selector selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true){ try { selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); selectionKeys

NIO网络访问模式实践

北慕城南 提交于 2019-11-27 15:34:38
1、创建NioNest12类 一个线程监听5个端口的事件 public class NioTest12 { public static void main(String[] args) throws Exception { int[] ports = new int[5]; ports[0] = 5000; ports[1] = 5001; ports[2] = 5002; ports[3] = 5003; ports[4] = 5004; Selector selector = Selector.open(); for(int i = 0; i < ports.length; ++i){ ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); ServerSocket serverSocket = serverSocketChannel.socket(); InetSocketAddress address = new InetSocketAddress(ports[i]); serverSocket.bind(address); serverSocketChannel.register(selector,

How to convert a String array to a Byte array? (java)

谁都会走 提交于 2019-11-27 14:51:56
问题 I have a one dimensional String array that I want to convert into a one dimensional byte array. How do I do this? Does this require ByteBuffer? How can I do this? (The strings can be any length, just want to know how to go about doing such an act. And after you convert it into a byte array how could I convert it back into a String array? -Dan 回答1: Array to Array you should convert manually with parsing into both sides, but if you have just a String you can String.getBytes() and new String

Java NIO MappedByteBuffer OutOfMemoryException

为君一笑 提交于 2019-11-27 14:12:49
问题 I am really in trouble: I want to read HUGE files over several GB using FileChannel s and MappedByteBuffer s - all the documentation I found implies it's rather simple to map a file using the FileChannel.map() method. Of course there is a limit at 2GB as all the Buffer methods use int for position, limit and capacity - but what about the system implied limits below that? In reality, I get lots of problems regarding OutOfMemoryException s! And no documentation at all that really defines the

18_网络编程

我只是一个虾纸丫 提交于 2019-11-27 12:52:42
百知教育 - 孙帅 - 18_网络编程 01_网络基础 网络: 若干主机(host) 形成的有机整体; 按提供的服务不同,可以分为 客户端 、 服务器 IP地址 标识网络主机的 逻辑地址 (192.168.0.1 四分十进制) 端口号 用来 标识 主机中的 进程 。 进程会预先绑定唯一的端口号,用来对外部 监听 。 端口号范围 : 0-65535 ,其中 0-1024 为 预留端口 。 四层协议 应用层 HTTP FTP SMTP TELNET 传输层 TCP ( 传输控制协议 ): 面向连接、安全可靠 UDP ( 用户数据报协议 ): 无连接、不可靠 网络层 IP协议 :约定了网络如何 寻址 和 路由 网络接口 02_基于BIO的网络编程 BioServer代码: package day24 ; import java . net . * ; import java . io . * ; import java . util . concurrent . Executors ; import java . util . concurrent . ExecutorService ; public class TestBioServer { public static void main ( String [ ] args ) { ServerSocket ss = null ;

ByteBuffer not releasing memory

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 12:14:45
On Android, a direct ByteBuffer does not ever seem to release its memory, not even when calling System.gc(). Example: doing Log.v("?", Long.toString(Debug.getNativeHeapAllocatedSize())); ByteBuffer buffer = allocateDirect(LARGE_NUMBER); buffer=null; System.gc(); Log.v("?", Long.toString(Debug.getNativeHeapAllocatedSize())); gives two numbers in the log, the second one being at least LARGE_NUMBER larger than the first. How do I get rid of this leak? Added: Following the suggestion by Gregory to handle alloc/free on the C++ side, I then defined JNIEXPORT jobject JNICALL Java_com_foo_bar

Are Java DirectByteBuffer wrappers garbage collected?

狂风中的少年 提交于 2019-11-27 11:21:08
问题 I understand that when a directbytebuffer is allocated, its not subject to garbage collection, but what I'm wondering is if the wrapping object is garbage collected. For example, if I allocated a new DirectByteBuffer dbb, and then duplicated(shallow copied) it using dbb.duplicate(), I'd have two wrappers around the same chunk of memory. Are those wrappers subject to garbage collection? If I did while(true){ DirectByteBuffer dbb2 = dbb.duplicate(); } Would I eventually OOM myself? 回答1: In the