Here you can find the source of inputStreamToByteBuffer(InputStream is)
static ByteBuffer inputStreamToByteBuffer(InputStream is) throws IOException
//package com.java2s; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; import java.nio.ByteBuffer; public class Main { static ByteBuffer inputStreamToByteBuffer(InputStream is) throws IOException { final int PAGE_SIZE = 4096; final ArrayList<byte[]> pages = new ArrayList<byte[]>(); for (;;) { byte[] page = new byte[PAGE_SIZE]; int pagePos = 0; int read; do {// ww w . j a v a2 s. co m read = is.read(page, pagePos, PAGE_SIZE - pagePos); if (read <= 0) { break; } pagePos += read; } while (pagePos < PAGE_SIZE); if (pagePos == PAGE_SIZE) { pages.add(page); } else { ByteBuffer fontBuffer = ByteBuffer.allocateDirect(pages .size() * PAGE_SIZE + pagePos); for (int i = 0, n = pages.size(); i < n; i++) { fontBuffer.put(pages.get(i)); } pages.clear(); fontBuffer.put(page, 0, pagePos).flip(); return fontBuffer; } } } }