Example usage for java.nio.channels FileChannel map

List of usage examples for java.nio.channels FileChannel map

Introduction

In this page you can find the example usage for java.nio.channels FileChannel map.

Prototype

public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;

Source Link

Document

Maps a region of this channel's file directly into memory.

Usage

From source file:Main.java

static public String getFileContent(String filename) throws IOException {
    File file = new File(filename);
    FileInputStream stream = new FileInputStream(file);
    try {//from  w w w.j a  v a 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: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. ja va  2 s .co  m
    return bufferToHex(messagedigest.digest());
}

From source file:Main.java

public static byte[] encryptMD5File(File file) {
    FileInputStream in = null;/*from  w ww .j a  v  a 2  s.  co  m*/
    try {
        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;
}

From source file:com.viddu.handlebars.HandlebarsFileUtil.java

/**
 * Utility method to get contents of the File.
 * //from  w ww.  j a va2s.  co  m
 * @param inputFile
 * @return
 * @throws IOException
 */
public static String getFileContents(File inputFile) throws IOException {
    FileInputStream stream = new FileInputStream(inputFile);
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }

}

From source file:GrepNIO.java

static void process(Pattern pattern, String fileName) throws IOException {

    // Get a FileChannel from the given file.
    FileChannel fc = new FileInputStream(fileName).getChannel();

    // Map the file's content
    ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    // Decode ByteBuffer into CharBuffer
    CharBuffer cbuf = Charset.forName("ISO-8859-1").newDecoder().decode(buf);

    Matcher m = pattern.matcher(cbuf);
    while (m.find()) {
        System.out.println(m.group(0));
    }/*from   w  w  w . j  a v a  2s .  c o m*/
}

From source file:Main.java

/**
 *Valid plugin  md5//from  w ww.  j a v  a 2 s. co 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 String findTitle(File f) {
    if (titlePattern == null) {
        initPattern();//from w  w  w.  ja  v  a 2  s.com
    }

    try {
        FileChannel fc = new FileInputStream(f).getChannel();

        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

        CharBuffer cb = Charset.forName("8859_1").newDecoder().decode(bb); //$NON-NLS-1$

        Matcher m = titlePattern.matcher(cb);
        String title = null;
        if (m.find()) {
            title = m.group(1);
        }

        return title;
    } catch (IOException e) {
        return null;
    }
}

From source file:edu.kit.cockpit.valuationserver.sfmpersistency.FileUtil.java

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

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();/*from  w w  w . ja  v a2  s .co m*/
    fis.close();
    return data;
    //      return readFileToMemory(new File(file));
}

From source file:fm.moe.android.util.JSONFileHelper.java

private static String readFile(final File file) throws IOException {
    final FileInputStream stream = new FileInputStream(file);
    try {/*from w w  w .  j  av a  2 s.co m*/
        final FileChannel fc = stream.getChannel();
        final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}