Java ByteBuffer Put inputStreamToByteBuffer(InputStream is)

Here you can find the source of inputStreamToByteBuffer(InputStream is)

Description

input Stream To Byte Buffer

License

Open Source License

Declaration

static ByteBuffer inputStreamToByteBuffer(InputStream is)
            throws IOException 

Method Source Code

//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;
            }
        }
    }
}

Related

  1. extractString(ByteArrayOutputStream byteBuffer)
  2. flushOutputStreamWriter(OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock)
  3. getZeroTerminatedStringBytesArray( ByteBuffer inputBuffer)
  4. inputStream(ByteBuffer bytes)
  5. inputStreamToByteBuffer(InputStream input)
  6. makeInputStream(final ByteBuffer buffer)
  7. moveBufferToStream(OutputStream out, ByteBuffer in, int length)
  8. outputBuffer(ByteBuffer buffer)
  9. put(ByteBuffer bb, byte[] bytes, int from)