I have two ways to generate the buffer. The first is from the file
File file = new File(pdfFilePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
(As you may guess the buffer is generated to display a pdf file. The example works ok.)
I want to replace the code to instantiate the buffer from the string as follows
ByteBuffer buf = ByteBuffer.wrap(pdfContent.getBytes("UTF-8"));
Both the content of the file in the first example and the content of the string look identical. However the two buffers are different (have different capacity). When I use the buffer in the second example to display a pdf file an exception is thrown. The question is what is different between the two examples? What should I fix in the second case?