Elegant way to read file into byte[] array in Java
A long time ago:
Call any of these
byte[] org.apache.commons.io.FileUtils.readFileToByteArray(File file)
byte[] org.apache.commons.io.IOUtils.toByteArray(InputStream input)
From
http://commons.apache.org/io/
If the library footprint is too big for your Android app, you can just use relevant classes from the commons-io library
Today (Java 7+ or Android API Level 26+)
Luckily, we now have a couple of convenience methods in the nio packages. For instance:
byte[] java.nio.file.Files.readAllBytes(Path path)
Javadoc here
File to byte[] in Java
It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. Which is here FileUtils.readFileToByteArray(File input)
.
To convert the file into byte array
If you just want to modify your existing code to write the image to a byte array instead of a file, then replace the try
block with this code:
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
bytes = out.getBytes();
... where bytes
has type byte[]
, and get rid of the code that generates the filename and deletes the existing file if it exists. Since you writing to a ByteArrayOutputStream, there is not need to call flush()
or close()
on out
. (They won't do anything.)
Convert InputStream to byte array in Java
You can use Apache Commons IO to handle this and similar tasks.
The IOUtils
type has a static method to read an InputStream
and return a byte[]
.
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
Internally this creates a ByteArrayOutputStream
and copies the bytes to the output, then calls toByteArray()
. It handles large files by copying the bytes in blocks of 4KiB.
How to read first and last 64kb of a video file in Java?
try something like this
FileInputStream in = new FileInputStream("d:/1.avi");
byte[] a = new byte[64 * 1024];
in.read(a); //head
long p = in.getChannel().size() - 64 * 1024;
in.getChannel().position(p);
in.read(a); //tail
How to pass a file (read from Java) most effectively to a native method?
Using regular arrays may be inefficient, as the VM may copy the array when passing it to native code, and may also use intermediate memory during I/O.
For the fastest IO, use ByteBuffer.allocateDirect to allocate a byte buffer. The underlying array is "special" in that it is not part of the regular JVM heap. Native code and I/O can access the array directly.
To read data into the buffer use,
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(randomAccessFile.length());
RandomAccessFile.getChannel().read(byteBuffer, 0);
To get the backing array to pass to JNI use
byte[] byteArray = byteBuffer.array();
You can then pass this array and the file length to JNI.
The direct buffers are realtively heavy to create, As all your files are 1MB (or thereabouts) you should be able to reuse the same buffer on multiple files.
Hope this helps!
Related Topics
How to Make an Array of Arrays in Java
The Type Java.Lang.Charsequence Cannot Be Resolved in Package Declaration
Giving Jmenuitem's Name to It's Actionlistener
Why Do I Need Transaction in Hibernate for Read-Only Operations
Find Out If Class Name Contains Certain Text
Import Google Play Services Library in Android Studio
Android: Switch Camera When Button Clicked
Opencv Template Matching Example in Android
How to Use Weakreference in Java and Android Development
Does Java Casting Introduce Overhead? Why
Capture and Log the Response Body
Java.Lang.Illegalstateexception: Scanner Closed
How to Change Webservice Url Endpoint
Javafx - What Is This Weird Container That Holds Context Menu
Pausing/Stopping and Starting/Resuming Java Timertask Continuously