bytebuffer

C++ equivalent of Java ByteBuffer?

不打扰是莪最后的温柔 提交于 2019-12-18 11:06:14
问题 I'm looking for a C++ "equivalent" of Java ByteBuffer. I'm probably missing the obvious or just need an isolated usage example to clarify. I've looked through the iostream family & it looks like it may provide a basis. Specifically, I want to be able to: build a buffer from a byte array/point and get primitives from the buffer, e.g. getByte, getInt build a buffer using primitives e.g. putByte, putInt and then get the byte array/pointer. 回答1: You have stringbuf , filebuf or you could use

Prevent OutOfMemory when using java.nio.MappedByteBuffer

自古美人都是妖i 提交于 2019-12-18 06:39:10
问题 Consider application, which create 5-6 threads, each thread in cycle allocate MappedByteBuffer for 5mb page size. MappedByteBuffer b = ch.map(FileChannel.MapMode.READ_ONLY, r, 1024*1024*5); Sooner or later, when application works with big files, oom is thrown java.io.IOException: Map failed at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:758) Caused by: java.lang.OutOfMemoryError: Map failed at sun.nio.ch.FileChannelImpl.map0(Native Method) at sun.nio.ch.FileChannelImpl.map

Netty学习(3) 使用NIO实现群聊客户端和服务端

送分小仙女□ 提交于 2019-12-18 04:55:55
NIO编写的和客户端和服务端示例 服务端: package com . nettyStudy . nio . groupChat ; import org . omg . Messaging . SyncScopeHelper ; 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 serverSocketChannel ; private static final int port = 5555 ; public GroupChatServer ( ) { try { selector = Selector . open ( ) ; serverSocketChannel = ServerSocketChannel . open ( ) ; serverSocketChannel . configureBlocking

C/C++ Why to use unsigned char for binary data?

会有一股神秘感。 提交于 2019-12-17 17:29:36
问题 Is it really necessary to use unsigned char to hold binary data as in some libraries which work on character encoding or binary buffers? To make sense of my question, have a look at the code below - char c[5], d[5]; c[0] = 0xF0; c[1] = 0xA4; c[2] = 0xAD; c[3] = 0xA2; c[4] = '\0'; printf("%s\n", c); memcpy(d, c, 5); printf("%s\n", d); both the printf's output 𤭢 correctly, where f0 a4 ad a2 is the encoding for the Unicode code-point U+24B62 (𤭢) in hex. Even memcpy also correctly copied the bits

Options to make Java's ByteBuffer thread safe

戏子无情 提交于 2019-12-17 16:34:51
问题 What options do I have to make a ByteBuffer thread safe? It is known that it is not thread safe as it safes position, limit and some(/all?) methods depend on this internal state. For my purposes it will be sufficient if multiple read-threads are safe, but for other future visitors I would like to know what technics/tricks/traps I need to know to make it completely thread safe. What I have in mind: Synchronizing or using ReadWrite locks for all methods. Probably the slowest approach (?)

ByteBuffer not releasing memory

懵懂的女人 提交于 2019-12-17 15:39:38
问题 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

How to garbage collect a direct buffer in Java

痴心易碎 提交于 2019-12-17 15:36:39
问题 I have a memory leak that I have isolated to incorrectly disposed direct byte buffers. ByteBuffer buff = ByteBuffer.allocateDirect(7777777); The GC collects the objects that harbor these buffers but does not dispose of the buffer itself. If I instantiate enough of the transient objects containing buffers, I get this encouraging message: java.lang.OutOfMemoryError: Direct buffer memory I have been searching up this problem and apparently buff.clear(); and System.gc(); do not work. 回答1: I

Gets byte array from a ByteBuffer in java

时光毁灭记忆、已成空白 提交于 2019-12-17 07:16:07
问题 Is this the recommended way to get the bytes from the ByteBuffer ByteBuffer bb =.. byte[] b = new byte[bb.remaining()] bb.get(b, 0, b.length); 回答1: Depends what you want to do. If what you want is to retrieve the bytes that are remaining (between position and limit), then what you have will work. You could also just do: ByteBuffer bb =.. byte[] b = new byte[bb.remaining()] bb.get(b); which is equivalent as per the ByteBuffer javadocs. 回答2: Note that the bb.array() doesn't honor the byte

基于nio的socket编程

ⅰ亾dé卋堺 提交于 2019-12-15 21:11:24
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> io编程中存在两个问题,io是阻塞的,而且保持多个连接的时候需要加入多线程来保持socket连接。这种方式比较浪费资源,因为每个连接都需要一个线程来保持,这在连接比较多的时候是一个浪费的资源是相当严重的。而两个问题在nio编程中就很好的解决这个问题,但是相对来说,对于编程的复杂度也相对有点提高。 这里不去深入的讨论nio编程的原理,只是基于实用的角度上做一些简单的介绍。在nio编程中,主要有三个组件比较重要:channel、buffer、selector。 channel: 通道,它和io中流有点相似,所以的源数据或者目标数据都是直接和channel打交道的。java nio中channel有以下几种实现,这些通道涵盖了TCP和UDP网络io: FileChannel DatagramChannel SocketChannel ServerSocketChannel buffer: 缓冲区 , 他是直接与channel交互的,我们如果要读取channle中的数据时,都是先把数据从channle中读入到buffer中;同样的,如果我们要向channel中写数据的时候,也要先把数据写入到buffer中。java nio中有也有多个实现,他们不要为了针对不同的数据类型: ByteBuffer CharBuffer

聊聊rocketmq的sendBatchMessage

五迷三道 提交于 2019-12-14 12:40:40
序 本文主要研究一下rocketmq的sendBatchMessage SendMessageRequestHeader rocketmq-all-4.6.0-source-release/common/src/main/java/org/apache/rocketmq/common/protocol/header/SendMessageRequestHeader.java public class SendMessageRequestHeader implements CommandCustomHeader { @CFNotNull private String producerGroup; @CFNotNull private String topic; @CFNotNull private String defaultTopic; @CFNotNull private Integer defaultTopicQueueNums; @CFNotNull private Integer queueId; @CFNotNull private Integer sysFlag; @CFNotNull private Long bornTimestamp; @CFNotNull private Integer flag; @CFNullable private String