Loads a file to a ByteBuffer. : Buffer « File « Android

Loads a file to a ByteBuffer.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

class Main {
  static final String defaultCharset = "UTF-8"; // used if not found in header
                          // or meta charset
  private static final int bufferSize = 0x20000; // ~130K.

   * Loads a file to a Document.
   * @param in
   *            file to load
   * @param charsetName
   *            character set of input
   * @param baseUri
   *            base URI of document, to resolve relative links against
   * @return Document
   * @throws IOException
   *             on IO error

  static ByteBuffer readToByteBuffer(InputStream inStream) throws IOException {
    byte[] buffer = new byte[bufferSize];
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);
    int read;
    while (true) {
      read = inStream.read(buffer);
      if (read == -1)
      outStream.write(buffer, 0, read);
    ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
    return byteData;


Related examples in the same category

1.Fast Float Buffer
2.Demonstrate the Frame Buffer Object OpenGL ES extension.
3.Make a direct NIO FloatBuffer from an array of floats
4.Make a direct NIO ByteBuffer from an array of bytes
5.Make Float Buffer From Array
6.Creates a floatbuffer of the given size.
7.Make Float Buffer with ByteBuffer.allocateDirect
8.allocate Float Buffer
9.to Short Buffer
10.to Float Buffer Position Zero
11.allocate Short Buffer
12.allocate Int Buffer
13.Read InputStream with BufferedReader
14.To convert the InputStream to String we use the BufferedReader.readLine() method.
15.make Float Buffer
16.get Buffer From Array
17.Your own float buffer
18.Direct Buffer
19.create Buffer
20.Write String to File with BufferWriter
21.Byte Buffer Stack
22.Compute the SHA-1 hash of the bytes in the given buffer
23.An utility class for java.nio.Buffers