Java ByteBuffer Put readFully(InputStream in, ByteBuffer out)

Here you can find the source of readFully(InputStream in, ByteBuffer out)

Description

read Fully

License

Open Source License

Declaration

public static void readFully(InputStream in, ByteBuffer out)
            throws IOException 

Method Source Code

//package com.java2s;
/* JKTX//w  w  w.  j  a  va  2s.c o  m
 * 
 * Copyright (c) 2011 Timon Bijlsma
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
    
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

public class Main {
    public static void readFully(InputStream in, ByteBuffer out)
            throws IOException {
        out.mark();
        if (out.hasArray()) {
            while (out.hasRemaining()) {
                int r = in
                        .read(out.array(),
                                out.arrayOffset() + out.position(),
                                out.remaining());
                if (r < 0)
                    throw new EOFException();
                out.position(out.position() + r);
            }
        } else {
            byte[] temp = new byte[Math.min(out.remaining(), 8192)];
            while (out.hasRemaining()) {
                int r = in.read(temp, 0,
                        Math.min(temp.length, out.remaining()));
                if (r < 0)
                    throw new EOFException("" + out.position());
                out.put(temp, 0, r);
            }
        }
        out.reset();
    }
}

Related

  1. readBER32(ByteBuffer input)
  2. readByteBuffer(DataInputStream is)
  3. readByteBufferList(DataInputStream is)
  4. readBytes(InputStream is, ByteBuffer buffer)
  5. readByteVector8(ByteBuffer input)
  6. readFully(InputStream is, ByteBuffer buf)
  7. readFully(InputStream is, ByteBuffer buffer, int nrBytes)
  8. readInto(ByteBuffer buf, InputStream inputStream)
  9. readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)