Example usage for java.nio.channels FileChannel size

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

Introduction

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

Prototype

public abstract long size() throws IOException;

Source Link

Document

Returns the current size of this channel's file.

Usage

From source file:org.sleuthkit.autopsy.recentactivity.Util.java

public static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {/*from   ww  w .  j a va  2s  .c  om*/
        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:nl.b3p.applet.local.Shapefiles.java

/** Returns a JSON object with shapefile and DBF metadata.
 * /*from ww w.ja  va2s  .  c  o  m*/
 * @param file the shapefile to read
 * @return a JSON object, read the code to find out which properties
 */
public static String getMetadata(String file) throws IOException, JSONException {

    if (!file.toLowerCase().endsWith(".shp")) {
        throw new IllegalArgumentException("File does not end with .shp: " + file);
    }

    FileChannel channel = new FileInputStream(file).getChannel();
    ShapefileHeader header = new ShapefileHeader();
    ByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    header.read(bb, true);
    channel.close();
    channel = null;
    file = file.substring(0, file.length() - 4) + ".dbf";

    JSONObject j = new JSONObject();
    j.put("type", header.getShapeType().name);
    j.put("version", header.getVersion());
    j.put("minX", header.minX());
    j.put("minY", header.minY());
    j.put("maxX", header.maxX());
    j.put("maxY", header.maxY());

    JSONObject dbf = new JSONObject();
    j.put("dbf", dbf);
    try {
        channel = new FileInputStream(file).getChannel();
        DbaseFileHeader dheader = new DbaseFileHeader();
        bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        dheader.readHeader(bb);
        dbf.put("numRecords", dheader.getNumRecords());
        JSONArray fields = new JSONArray();
        dbf.put("fields", fields);
        for (int i = 0; i < dheader.getNumFields(); i++) {
            JSONObject field = new JSONObject();
            fields.put(field);
            field.put("name", dheader.getFieldName(i));
            field.put("length", dheader.getFieldLength(i));
            field.put("decimalCount", dheader.getFieldDecimalCount(i));
            field.put("class", dheader.getFieldClass(i).getName().toString());
            field.put("type", dheader.getFieldType(i) + "");
        }
    } catch (Exception e) {
        dbf.put("error", e.toString());
    } finally {
        if (channel != null) {
            channel.close();
        }
    }

    file = file.substring(0, file.length() - 4) + ".prj";
    File f = new File(file);
    String prj = null;
    if (f.exists()) {
        Scanner s = new Scanner(f);
        prj = "";
        try {
            while (s.hasNextLine()) {
                if (prj.length() > 0) {
                    prj += "\n";
                }
                prj += s.nextLine();
            }
        } finally {
            s.close();
        }
    }
    j.put("prj", prj);

    return j.toString();
}

From source file:org.apache.solr.util.FileUtils.java

public static void copyFile(File src, File destination) throws IOException {
    FileChannel in = null;
    FileChannel out = null;/* w w w . j  av  a 2 s . c om*/
    try {
        in = new FileInputStream(src).getChannel();
        out = new FileOutputStream(destination).getChannel();
        in.transferTo(0, in.size(), out);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.project.utilities.Utilities.java

public static ArrayList<ViolationDataModel> readViolationJSONFileToArray() {
    ArrayList<ViolationDataModel> violation = new ArrayList<ViolationDataModel>();
    Gson gson = new Gson();
    String jsonStr = "";
    try {//from  w  ww  . j ava  2 s.c om
        File jsonFile = new File(Constants.EXTERNAL_DIRECTORY + "/" + Constants.FILENAME_VIOLATION_JSON);

        if (!jsonFile.exists()) {
            Log.e("VIOLATION_JSON_LIST", "Error reading file");
        } else {
            Log.e("readJsonFile", "File Found");
        }

        FileInputStream stream = new FileInputStream(jsonFile);
        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            jsonStr = Charset.defaultCharset().decode(byteBuffer).toString();
        } finally {
            stream.close();
        }
    } catch (Exception e) {
        Log.e("readJsonFile", e.getMessage());
    }

    try {
        JSONObject jsonContent = new JSONObject(jsonStr);
        violation = gson.fromJson(jsonContent.getString("data"),
                new TypeToken<ArrayList<ViolationDataModel>>() {
                }.getType());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("Error GSON", e.getMessage().toString());
    }
    return violation;
}

From source file:Main.java

private static void copy(File srcFile, File targetFile) throws IOException {
    FileChannel src = null, dst = null;
    try {/*from w w  w  .ja v  a  2 s. com*/
        if (srcFile.exists() && (!targetFile.exists() || targetFile.delete())) {
            src = new FileInputStream(srcFile).getChannel();
            dst = new FileOutputStream(targetFile).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    } finally {
        try {
            if (src != null)
                src.close();
        } catch (IOException e) {
            Log.e("DIARY", "Failed to close resource.", e);
        }
        try {
            if (dst != null)
                src.close();
        } catch (IOException e) {
            Log.e("DIARY", "Failed to close resource.", e);
        }
    }
}

From source file:com.bazaarvoice.seo.sdk.util.BVUtility.java

public static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {/*from   ww w  .  j  a v a  2  s .com*/
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.forName("UTF-8").decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:org.nuclos.common2.File.java

/**
 * Simple Method for copying files with FileChannel
 * FIX ELISA-6498/*  ww w. j av a  2  s  . c  o  m*/
 * 
 * @param in
 * @param out
 * @throws IOException
 */
public static void copyFile(java.io.File in, java.io.File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

From source file:nl.b3p.catalog.arcgis.Shapefiles.java

/** Returns a JSON object with shapefile and DBF metadata.
 *
 * @param file the shapefile to read//from  w  w  w .  j  ava 2s  . c om
 * @return a JSON object, read the code to find out which properties
 */
public static String getMetadata(String file) throws IOException, JSONException {

    if (!file.toLowerCase().endsWith(".shp")) {
        throw new IllegalArgumentException("File does not end with .shp: " + file);
    }

    JSONObject j = new JSONObject();

    String localFilename = new File(file).getName();
    String title = "";
    int dotIndex = localFilename.lastIndexOf(".");
    if (dotIndex > 0) {
        title = localFilename.substring(0, dotIndex);
    } else if (dotIndex == 0) {
        title = localFilename.substring(1);
    }
    j.put("title", title);

    FileChannel channel = new FileInputStream(file).getChannel();
    ShapefileHeader header = new ShapefileHeader();
    ByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    header.read(bb, true);
    channel.close();
    channel = null;
    file = file.substring(0, file.length() - 4) + ".dbf";

    j.put("type", header.getShapeType().name);
    j.put("version", header.getVersion());
    j.put("minX", header.minX());
    j.put("minY", header.minY());
    j.put("maxX", header.maxX());
    j.put("maxY", header.maxY());

    JSONObject dbf = new JSONObject();
    j.put("dbf", dbf);
    try {
        channel = new FileInputStream(file).getChannel();
        DbaseFileHeader dheader = new DbaseFileHeader();
        bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        dheader.readHeader(bb);
        dbf.put("numRecords", dheader.getNumRecords());
        JSONArray fields = new JSONArray();
        dbf.put("fields", fields);
        for (int i = 0; i < dheader.getNumFields(); i++) {
            JSONObject field = new JSONObject();
            fields.put(field);
            field.put("name", dheader.getFieldName(i));
            field.put("length", dheader.getFieldLength(i));
            field.put("decimalCount", dheader.getFieldDecimalCount(i));
            field.put("class", dheader.getFieldClass(i).getName().toString());
            field.put("type", dheader.getFieldType(i) + "");
        }
    } catch (Exception e) {
        dbf.put("error", e.toString());
    } finally {
        if (channel != null) {
            channel.close();
        }
    }

    file = file.substring(0, file.length() - 4) + ".prj";
    File f = new File(file);
    String prj = null;
    if (f.exists()) {
        Scanner s = new Scanner(f);
        prj = "";
        try {
            while (s.hasNextLine()) {
                if (prj.length() > 0) {
                    prj += "\n";
                }
                prj += s.nextLine();
            }
        } finally {
            s.close();
        }
    }
    j.put("prj", prj);

    return j.toString();
}

From source file:edu.clemson.cs.nestbed.server.util.FileUtils.java

public static void copyFile(File in, File out) throws FileNotFoundException, IOException {
    FileChannel sourceChannel = null;
    FileChannel destinationChannel = null;

    try {//from   ww w  .j  a  va2s.  c  o  m
        sourceChannel = new FileInputStream(in).getChannel();
        destinationChannel = new FileOutputStream(out).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
    } finally {
        try {
            sourceChannel.close();
        } catch (Exception ex) {
        }
        try {
            destinationChannel.close();
        } catch (Exception ex) {
        }
    }
}

From source file:Main.java

/**
 * /*from  w  w  w . j a v  a2 s  .com*/
 * copy file
 * 
 * @param src
 *            source file
 * @param dest
 *            target file
 * @throws IOException
 */
public static void copyFile(File src, File dest) throws IOException {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        if (!dest.exists()) {
            dest.createNewFile();
        }
        inChannel = new FileInputStream(src).getChannel();
        outChannel = new FileOutputStream(dest).getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}