bytebuffer

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

醉酒当歌 提交于 2019-12-14 00:23:17
问题 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?

How write big endian ByteBuffer to little endian in Java

萝らか妹 提交于 2019-12-13 12:05:12
问题 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();

Buffer和Channel

那年仲夏 提交于 2019-12-13 06:40:19
    在细讲Buffer和Channel前,我们先对几个概念梳理一下。我们在做网络编程的时候时常听到三种东西:BIO,NIO,AIO。 BIO是java中普通的IO包,jdk1.4时出现了NIO,N表时New,所以其实就是新版的IO,jdk1.7时出现了AIO,也叫NIO2.0。那这三种IO有什么区别呢?IO是同步阻塞IO,NIO是同步非阻塞IO,AIO是异步非阻塞IO。这里有几个名词,同步和异步,阻塞和非阻塞,我们举个例子:     我们在烧水,当水还没开这一段时间里,我们有两种选择,站在旁边等水烧开或者先去做别的事(比如刷个剧),如果我们选择等水烧开,就是阻塞,如果我们先去刷个剧,等会儿再来看一眼水有没有烧开,就是非阻塞。但是你会觉得,看会儿剧又要回来瞅一眼水开没开是一件很烦的事情,要是水开了能够告诉我水开了就好了,比如现在的智能家居,你尽管去刷剧,等水开了通过app给弹个消息告诉你水开了,这就是异步非阻塞。     放到我们网络编程的场景下,现在有AB两个系统,B调A系统一个接口,如果该系统是一个耗时操作(像烧水那样), BIO 模式下,B系统的当前线程就会等在那里(等水烧开),而NIO模式下,B系统的这个线程就会先去做别的事情,等会儿回来看接口有没有返回,如果返回了继续处理,没返回再去做别的事情(就跟你看会儿剧去看一眼水开没有一样)。而在AIO模式下

Identifying socket messages

微笑、不失礼 提交于 2019-12-13 04:01:31
问题 I have a code snippet below that process a socket message, and I would like to know what should be the message sent in order not to result in a return. Where SocketPacket is a class which stores the received socket, and DataLength would be the length of the received message, dataBuffer stores the message. int num3; byte num6 = 0; SocketPacket workSocket; int DataLength; if (workSocket.dataBuffer[0] == 0x33) { if (DataLength < 0xbb) { return false; } for (num3 = 0; num3 < 0xba; num3++) { num6

JNA ByteBuffer statvfs

老子叫甜甜 提交于 2019-12-13 03:45:25
问题 I am trying to get the free space on the / folder using statvfs call from java, I have check the size of statvfs struct from c it shows 44 bytes, I have allocated a byte buffer using java.nio.ByteBuffer.allocateDirect 44 bytes, and it's order is set to 44 bytes. when i call statvfs i get a return value of 0, so i am assuming call is successful, but i can't seem to get information out of ByteBuffer using buffer.getInt returns 512 f_bsize which is correct but after that i can't read. buffer

Is put-ing to a ByteBuffer then writing it to a file more efficient than writing the individual field

Deadly 提交于 2019-12-13 03:37:04
问题 I want to write ONLY the values of the data members of an object into a file, so here I can can't use serialization since it writes a whole lot other information which i don't need. Here's is what I have implemented in two ways. One using byte buffer and other without using it. Without using ByteBuffer: 1st method public class DemoSecond { byte characterData; byte shortData; byte[] integerData; byte[] stringData; public DemoSecond(byte characterData, byte shortData, byte[] integerData, byte[]

聊聊rocketmq的HAClient

你离开我真会死。 提交于 2019-12-13 00:04:34
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 序 本文主要研究一下rocketmq的HAClient HAClient rocketmq-all-4.6.0-source-release/store/src/main/java/org/apache/rocketmq/store/ha/HAService.java class HAClient extends ServiceThread { private static final int READ_MAX_BUFFER_SIZE = 1024 * 1024 * 4; private final AtomicReference<String> masterAddress = new AtomicReference<>(); private final ByteBuffer reportOffset = ByteBuffer.allocate(8); private SocketChannel socketChannel; private Selector selector; private long lastWriteTimestamp = System.currentTimeMillis(); private long currentReportedOffset = 0; private int

What is the safest way to use direct byte buffers in Java?

陌路散爱 提交于 2019-12-11 21:18:55
问题 Lets sat I have an object i'd like to store in a direct byte buffer. I'd like to able access parts of the object from the direct byte buffer without de-serializing the whole object. Is there a safe way to do this? I'm thinking you could somehow capture the byte array offsets when serializing the object, then once its been written to the direct byte buffer you would adjust these offsets according to the offset of the direct byte buffer. I'm not sure if its possible to do this... 回答1: The real

what if we exceed the capacity of allocating buffer in ByteBuffer.allocate(48) NIO package class in java

﹥>﹥吖頭↗ 提交于 2019-12-11 18:19:57
问题 file = new RandomAccessFile("xanadu.txt", "rw"); FileChannel channel = file.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(48); int byteReads = channel.read(buffer); SO I am allocating 48 as a capacity in the Buffer. Now consider the txt file I am reading is of about 10MB , so logically it is crossing the buffer allocation size. But when we try to read, we will be able to read all the contents of the file despite the size. SO how this thing is possible. I am new to this streaming field

Getting unexpected Pixels from Raw Image

半世苍凉 提交于 2019-12-11 18:09:27
问题 I am trying to catch R, G and B from some pixels on a game scene. For this I have created a Bitmap image in Black & White. This image is first loaded on Init(), afterwards, every sprite movement is checked for it is really an available spot. The thing is that I am getting unexpected data at R, G and B. I tried two Bitmap images (8bit and 24bit). They both have only black and white pixels. But the r, g and b keep telling me these pixels are any other color. I think that the "no_of_channels"