Java FileInputStream Read readFile(String path, long offset)

Here you can find the source of readFile(String path, long offset)

Description

read File

License

Apache License

Declaration

public static byte[] readFile(String path, long offset) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

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

public class Main {

    public static byte[] readFile(String path) throws IOException {
        return readFile(path, 0);
    }/*from ww w. ja  v  a2 s. c om*/

    public static byte[] readFile(String path, long offset) throws IOException {
        return readFile(path, offset, -1);
    }

    public static byte[] readFile(String path, long offset, int size) throws IOException {
        InputStream is = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            is = new FileInputStream(path);
            is.skip(offset);
            byte[] buff = new byte[4096];
            int bufLength = -1;
            while ((bufLength = is.read(buff)) >= 0) {
                if (size > 0 && bufLength > size - baos.size()) {
                    baos.write(buff, 0, size - baos.size());
                    break;
                } else {
                    baos.write(buff, 0, bufLength);
                }
            }
            return baos.toByteArray();
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String path, Properties store)
  7. readFile(String path, String encoding)
  8. readFile(String path, String root, OutputStream out)
  9. readFile(String pPathToFile)