Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ByteBuffer question

807580Apr 9 2010 — edited Apr 9 2010
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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2010
Added on Apr 9 2010
9 comments
654 views