Using dynamic buffers? Java

In Java, I have a method

public int getNextFrame( byte[] buff ) 

which reads from the file to the buffer and returns the number of bytes read. I am reading from .MJPEG, which has a value of 5 bytes, for example "07939", followed by many bytes for jpeg.

The problem is that the JPEG byte size can overflow the buffer. I can't seem to find a neat solution for highlighting. My goal is not to create a new buffer for each image. I tried direct ByteBuffer , so I could use its array() method to get direct access to the base buffer. ByteBuffer not dynamically expanding.

Should I return a parameter reference? How:

 public ByteBuffer getNextFrame( ByteBuffer ref ) 

How to find read bytes? Thanks.

+4
source share
4 answers

java.io.ByteArrayOutputStream is a wrapper around a byte array and if necessary increases it. Perhaps this is what you could use.

Edit:
To reuse, just call reset () and start over ...

+1
source

Just read the required number of bytes. Do not use read(buffer) , but use read(buffer,0,size) . If there are more bytes, just drop them, the JPG will break anyway.

+1
source

EDIT:

Highlighting byte [] is much faster than reading from a file or socket, I would be surprised that this will make a big difference if you only have a system in which microseconds cost money.

Reading time of a file with 64 KB is about 10 ms (if only the file is in memory)

The time taken to allocate a 64 KB [] byte is about 0.001 ms, possibly faster.


You can use apache IO IOBuffer, however this is very expensive.

You can also use ByteBuffer, position() will tell you how much data has been read.

If you don’t know how big the buffer is and you have a 64-bit JVM, you can create a large direct buffer. When used, memory will be allocated (per page). As a result, you can allocate 1 GB, but can only use 4 KB if that's all you need. The direct buffer does not support array() , however you will have to read from ByteBuffer using other methods.

Another solution is to use AtomicReference<byte[]> , the called method can increase the size as necessary, but if it is large enough, it will reuse the previous buffer.

0
source

The usual way to do this in a high-level API is to either provide the user with an OutputStream and populate it with your data (which could be ByteArrayOutputStream or something completely different), or have an InputStream as a return value that the user can read to receive data (that will dynamically load the correct parts from the file and stop at the end).

-1
source

Source: https://habr.com/ru/post/1341861/


All Articles