Java File Read via ByteBuffer streamRead(SocketChannel in)

Here you can find the source of streamRead(SocketChannel in)

Description

stream Read

License

Open Source License

Declaration

public static byte[] streamRead(SocketChannel in) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main {
    public static byte[] streamRead(InputStream in) throws IOException {
        byte buf[] = new byte[4096];
        int len = in.read(buf);

        if (len == -1)
            return null;

        byte ret[] = new byte[len];
        System.arraycopy(buf, 0, ret, 0, len);
        return (ret);
    }/*from  w  ww  .j a  v  a  2  s. co m*/

    public static byte[] streamRead(SocketChannel in) throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(4096);
        //byte buf[] = new byte[4096];
        int len = in.read(buf);

        if (len == -1)
            return null;

        byte ret[] = new byte[len];
        buf.get(ret);
        //System.arraycopy(buf, 0, ret, 0, len);
        return (ret);
    }
}

Related

  1. readWaveFile44100_16Bit_Mono(String dir, String name)
  2. readWavHeader(DataInputStream inStrm, FileChannel fc)
  3. readWholeFile(File file, StringBuilder stringBuilder)
  4. readZipComment(File file)
  5. skip(final ReadableByteChannel input, final long toSkip)