Java File Content to Byte Array getFileContentByte(InputStream inputStream, long offset, int length)

Here you can find the source of getFileContentByte(InputStream inputStream, long offset, int length)

Description

get File Content Byte

License

Open Source License

Declaration

public static byte[] getFileContentByte(InputStream inputStream, long offset, int length) throws Exception 

Method Source Code

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

import java.io.InputStream;

public class Main {
    public static byte[] getFileContentByte(InputStream inputStream, long offset, int length) throws Exception {
        if (offset < 0 || length < 0) {
            throw new Exception("getFileContent param error");
        }/*from   w  ww.  j a va  2  s .co m*/

        byte[] fileContent = null;
        byte[] tempBuf = new byte[length];

        inputStream.skip(offset);
        int readLen = inputStream.read(tempBuf);
        if (readLen < 0) {
            fileContent = new byte[0];
            return fileContent;
        }
        if (readLen < length) {
            fileContent = new byte[readLen];
            System.arraycopy(tempBuf, 0, fileContent, 0, readLen);
        } else {
            fileContent = tempBuf;
        }
        return fileContent;
    }
}

Related

  1. getFileContentAsByteArray(String fileName)
  2. getFileContentAsByteArray(String fileName)
  3. getFileContentAsBytes(String fileName)
  4. getFileContentAsBytes(String fileName)
  5. getFileContentsAsByteArray(String filename)