Example usage for java.io FileInputStream getChannel

List of usage examples for java.io FileInputStream getChannel

Introduction

In this page you can find the example usage for java.io FileInputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file input stream.

Usage

From source file:Main.java

public static ByteBuffer mapFile(File f, long offset, ByteOrder byteOrder) throws IOException {
    FileInputStream dataFile = new FileInputStream(f);
    try {//  w  w  w  . j av a  2s  .  com
        FileChannel fc = dataFile.getChannel();
        MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, offset, f.length() - offset);
        buffer.order(byteOrder);
        return buffer;
    } finally {
        dataFile.close(); // this *also* closes the associated channel, fc
    }
}

From source file:Main.java

public static String getFileMD5String(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    FileChannel ch = inputStream.getChannel();
    MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    messagedigest.update(byteBuffer);//from w w w.j a  v a2 s . c  o m
    return bufferToHex(messagedigest.digest());
}

From source file:Main.java

public static byte[] readFileToMemory(String file) throws IOException {
    FileInputStream fis = new FileInputStream(file);
    FileChannel fileChannel = fis.getChannel();
    long size = fileChannel.size();
    MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
    byte[] data = new byte[(int) size];
    mappedByteBuffer.get(data, 0, (int) size);
    fileChannel.close();//w  ww .  j  a  v a2s  .c  o  m
    fis.close();
    return data;
    //      return readFileToMemory(new File(file));
}

From source file:Main.java

static public String getFileContent(String filename) throws IOException {
    File file = new File(filename);
    FileInputStream stream = new FileInputStream(file);
    try {// w  w  w .  j  a va  2  s.  c  o m
        FileChannel fc = stream.getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

        return Charset.defaultCharset().decode(mbb).toString();
    } finally {
        stream.close();
    }
}

From source file:z.tool.util.FileUtil.java

/**
 * ?//from w w  w .j a  va 2  s .c  o m
 */
public static void copy(FileInputStream fis, FileOutputStream fos) throws IOException {
    FileChannel channel = fis.getChannel();
    channel.transferTo(0, channel.size(), fos.getChannel());
}

From source file:Main.java

public static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    String str = null;//from  ww  w  .j  a v  a 2  s  .  c  o m
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        str = Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
    return str;
}

From source file:GrepSun.java

private static void grep(File f) throws IOException {

    // Open the file and then get a channel from the stream
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();

    // Get the file's size and then map it into memory
    int sz = (int) fc.size();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);

    // Decode the file into a char buffer
    CharBuffer cb = decoder.decode(bb);

    // Perform the search
    grep(f, cb);//from www . j a va2 s  .  com

    // Close the channel and the stream
    fc.close();
}

From source file:Main.java

public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();/*from  www . j a  va2 s . c o  m*/
    outStream.close();
}

From source file:Main.java

/**
 *Valid plugin  md5/* w  w w . ja  v  a 2 s  .  c  o  m*/
 * @param path   bundle archvie path
 * @param md5Sum   target  file md5
 * @return  if md5 matched,return true
 * ***/
public static boolean validFileMD5(String path, String md5Sum) {

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        File mFile = new File(path);
        if (mFile == null || !mFile.exists() || !mFile.isFile()) {
            return false;
        }
        FileInputStream in = new FileInputStream(mFile);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, mFile.length());
        messageDigest.update(byteBuffer);
        String digest = String.format("%032x", new BigInteger(1, messageDigest.digest()));
        return md5Sum.equals(digest.toString());
    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

    }
    return false;
}

From source file:Main.java

public static byte[] encryptMD5File(File file) {
    FileInputStream in = null;
    try {// w  w w  .  j  a  v  a 2  s . c om
        in = new FileInputStream(file);
        FileChannel channel = in.getChannel();
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(buffer);
        return md.digest();
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
    return null;
}