bytebuffer

Getting string from netty ByteBuf

你。 提交于 2019-12-04 03:15:27
How can I get the string from a netty ByteBuf ? As of now I am able to get it character by character. Is there a way to get the string object directly? // message is of type ByteBuf for (int i = 0; i < message.capacity(); i++) { byte b = message.payload().getByte(i); System.out.print((char) b); } Use the provided method ByteBuf.toString(Charset) . use ByteBuf.readCharSequence() example is here: // here we use utf-8 decoding, you can choose the charset you want String s = ByteBuf.readCharSequence(length, Charset.forName("utf-8")).toString() note that ByteBuf.readCharSequence() returns java.lang

How to expose raw byte buffers with Boost::Python?

末鹿安然 提交于 2019-12-04 02:53:00
I've got third party C++ library in which some class methods use raw byte buffers. I'm not quite sure how to deal in Boost::Python with it. C++ library header is something like: class CSomeClass { public: int load( unsigned char *& pInBufferData, int & iInBufferSize ); int save( unsigned char *& pOutBufferData, int & iOutBufferSize ); } In stuck with the Boost::Python code... class_<CSomeClass>("CSomeClass", init<>()) .def("load", &CSomeClass::load, (args(/* what do I put here??? */))) .def("save", &CSomeClass::save, (args(/* what do I put here??? */))) How do I wrap these raw buffers to

JAVA中的NIO (New IO)

孤者浪人 提交于 2019-12-03 23:07:45
简介 标准的IO是基于字节流和字符流进行操作的,而JAVA中的NIO是基于Channel和Buffer进行操作的。 传统IO graph TB; 字节流 --> InputStream; 字节流 --> OutputStream; 字符流 --> Reader; 字符流 --> Writer; NIO graph TB; A[Channel] --> B[Buffer..]; C[Channel] --> D[Buffer..]; E[Channel] --> F[Buffer..]; 核心模块 NIO主要有三个核心部分:Selector、Channel、Buffer 数据总是从Channel读取到Buffer或者从Buffer写入到Channel中。 Selector可以监听多个Channel的多个事件。 graph TB; Selector --> A[Channel]; Selector --> B[Channel]; Selector --> C[Channel]; A --> E1[Event...]; B --> E2[Event...]; C --> E3[Event...]; 传统的IO与Channel的区别 1.传统的IO是BIO的,而Channel是NIO的。 *当流调用了read()、write()方法后会一直阻塞线程直到数据被读取或写入完毕。 2

ByteBuffer Little Endian insert not working

我怕爱的太早我们不能终老 提交于 2019-12-03 22:27:31
I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the receiving part, however sending not works. Strange because for me it seems that the ByteBuffer class malfunctions (I can hardly believe that) ByteBuffer byteBuffer = ByteBuffer.allocate(4); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); byteBuffer = ByteBuffer.allocate(4); byteBuffer.putInt(88); byte[] result = byteBuffer.array(); Results: [0, 0, 0, 88] ByteBuffer byteBuffer = ByteBuffer.allocate(4); byteBuffer.order(ByteOrder

Hashing raw bytes in Python and Java produces different results

一个人想着一个人 提交于 2019-12-03 22:17:18
I'm trying to replicate the behavior of a Python 2.7 function in Java , but I'm getting different results when running a (seemingly) identical sequence of bytes through a SHA-256 hash. The bytes are generated by manipulating a very large integer (exactly 2048 bits long) in a specific way (2nd line of my Python code example). For my examples, the original 2048-bit integer is stored as big_int and bigInt in Python and Java respectively, and both variables contain the same number. Python2 code I'm trying to replicate: raw_big_int = ("%x" % big_int).decode("hex") buff = struct.pack(">i", len(raw

Is there a way to create a direct ByteBuffer from a pointer solely in Java?

六眼飞鱼酱① 提交于 2019-12-03 19:09:18
问题 Or do I have to have a JNI helper function that calls env->NewDirectByteBuffer(buffer, size)? 回答1: What I do is create a normal DirectByteBuffer and change it's address. Field address = Buffer.class.getDeclaredField("address"); address.setAccessible(true); Field capacity = Buffer.class.getDeclaredField("capacity"); capacity.setAccessible(true); ByteBuffer bb = ByteBuffer.allocateDirect(0).order(ByteOrder.nativeOrder()); address.setLong(bb, addressYouWantToSet); capacity.setInt(bb, theSizeOf);

回收DirectByteBuffer

六眼飞鱼酱① 提交于 2019-12-03 18:40:51
为避免GC的负效应, 使用DirectByteBuffer管理原始(Raw)数据会为高负载的系统带来性能的提升. DirectByteBuffer默认是由GC来回收的, 这通常能够满足需求, 若要想自己控制回收的时机, 可以使用这段代码. import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.security.AccessController; import java.security.PrivilegedAction; /** * {@link DirectByteBufferCleaner} * * @author zhongl * @created 2011-1-14 */ public final class DirectByteBufferCleaner { private DirectByteBufferCleaner() {} public static void clean(final ByteBuffer byteBuffer) { if (!byteBuffer.isDirect()) return; try { Object cleaner = invoke(byteBuffer, "cleaner"); invoke(cleaner, "clean"); }

Fast ByteBuffer to CharBuffer or char[]

[亡魂溺海] 提交于 2019-12-03 15:41:18
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 (lower 8) bits as char:128 . Therefore I assume the "best" interpretation would be as I noted it above,

javaNio DirectByteBuffer常用方法解析

与世无争的帅哥 提交于 2019-12-03 10:27:22
一,简介    DirectByteBuffer是ByteBuffer关于堆外内存使用的实现,堆外内存直接使用unsafe方法请求堆外内存空间,读写数据,直接使用内存地址,效率非常的高 使用ByteBuffer提供的工厂类可获得实例:    ByteBuffer sendBuf1 = ByteBuffer.allocateDirect(10); 1,常用类putInt sendBuf1.putInt(1); 此方法使用4个字节保存int类型的数据 实现方法: public ByteBuffer putInt(int x) { putInt(ix(nextPutIndex((1 << 2))), x); return this; } nextPutIndex 写入数据时通过position 计算下一个写入的数据事第几个 final int nextPutIndex(int nb) { // package-private if (limit - position < nb) throw new BufferOverflowException(); int p = position; position += nb; return p; } ix 方法计算下一个数据写入的内存地址 address 在创建实例时,申请内存地址后保存的,申请到内存地址的首地址address+position

How to use ByteBuffer in the MediaCodec context in android

独自空忆成欢 提交于 2019-12-03 10:12:20
问题 So far I am able to setup a MediaCodec to encode a video stream. The aim is to save my user generated artwork into a video file. I use android Bitmap objects of the user artwork to push frames into the stream. See the code snippet I use at the bottom of this post (it is the full code nothing is trimmed): MediaCodec uses ByteBuffer to deal with video/audio streams. Bitmaps are based on int[] which if converted to byte[] will require x4 the size of the int[] I did some research to figure out