Java ByteBuffer Put readFully(InputStream is, ByteBuffer buf)

Here you can find the source of readFully(InputStream is, ByteBuffer buf)

Description

read Fully

License

Open Source License

Declaration

public static void readFully(InputStream is, ByteBuffer buf) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright (c) 1996-2015, OPC Foundation. All rights reserved.
   The source code in this file is covered under a dual-license scenario:
 - RCL: for OPC Foundation members in good-standing
 - GPL V2: everybody else/*from  w  w  w .j  a v a 2  s .co  m*/
   RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
   GNU General Public License as published by the Free Software Foundation;
   version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
   This source code 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.
*/

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Reads entire resource from an input stream
     * 
     * @param is
     * @return resource
     * @throws IOException
     */
    public static byte[] readFully(InputStream is) throws IOException {
        byte[] buf = new byte[4096];
        ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
        for (;;) {
            int bytesRead = is.read(buf);
            if (bytesRead == -1)
                break;
            os.write(buf, 0, bytesRead);
        }

        return os.toByteArray();
    }

    public static void readFully(InputStream is, ByteBuffer buf) throws IOException {
        while (buf.hasRemaining()) {
            int n = is.read(buf.array(), buf.position(), buf.remaining());
            if (n < 0)
                throw new EOFException();
            buf.position(buf.position() + n);
        }
    }

    public static void readFully(InputStream is, byte[] b) throws IOException {
        readFully(is, b, 0, b.length);
    }

    public static void readFully(InputStream is, byte[] b, int off, int len) throws IOException {
        while (len > 0) {
            int n = is.read(b, off, len);
            if (n < 0)
                throw new EOFException();
            off += n;
            len -= n;
        }
    }

    public static void read(InputStream is, ByteBuffer buf, int bytes) throws IOException {
        while (bytes > 0 & buf.hasRemaining()) {
            int n = is.read(buf.array(), buf.position(), bytes);
            if (n < 0)
                throw new EOFException();
            buf.position(buf.position() + n);
            bytes -= n;
        }
    }
}

Related

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