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:thv.th.reader.SavegameReader.java

private static void readObjectInfo(FileInputStream saveStream, Tile tile, FileInputStream frameStream,
        FileInputStream listStream, FileInputStream elementStream, File tabFile, File chunksFile,
        THPalette palette) throws IOException {
    if (tile.getAnim() != 0) {
        int base = 131085 + tile.getAnim() * 175;
        ObjectInfo oi = new ObjectInfo(base);

        saveStream.getChannel().position(8 + base);
        int frameIndex = EndianUtils.readSwappedShort(saveStream);

        saveStream.skip(6);//from w  ww .j a va 2 s  .  c  om
        int byte16 = saveStream.read();

        saveStream.getChannel().position(base + 29);
        oi.setLayerId(saveStream.read(), 0);
        oi.setLayerId(saveStream.read(), 1);
        oi.setLayerId(saveStream.read(), 2);
        oi.setLayerId(saveStream.read(), 3);
        oi.setLayerId(saveStream.read(), 4);
        oi.setLayerId(saveStream.read(), 5);
        oi.setLayerId(saveStream.read(), 6);
        oi.setLayerId(saveStream.read(), 7);
        oi.setLayerId(saveStream.read(), 8);
        oi.setLayerId(saveStream.read(), 9);
        oi.setLayerId(saveStream.read(), 10);
        oi.setLayerId(saveStream.read(), 11);
        oi.setLayerId(saveStream.read(), 12);

        THFrame frame = FramesReader.readByIndex(frameIndex, frameStream, listStream, elementStream, tabFile,
                chunksFile, palette);

        oi.setFrame(frame);
        oi.setByte16(byte16);

        tile.setObjectInfo(oi);
    }
}

From source file:com.todotxt.todotxttouch.util.Util.java

public static void copyFile(File origFile, File newFile, boolean overwrite) {
    if (!origFile.exists()) {
        Log.e(TAG, "Error renaming file: " + origFile + " does not exist");

        throw new TodoException("Error copying file: " + origFile + " does not exist");
    }/*from  w  ww . j  ava  2 s.  c o  m*/

    createParentDirectory(newFile);

    if (!overwrite && newFile.exists()) {
        Log.e(TAG, "Error copying file: destination exists: " + newFile);

        throw new TodoException("Error copying file: destination exists: " + newFile);
    }

    try {
        FileInputStream fis = new FileInputStream(origFile);
        FileOutputStream fos = new FileOutputStream(newFile);
        FileChannel in = fis.getChannel();
        fos.getChannel().transferFrom(in, 0, in.size());
        fis.close();
        fos.close();
    } catch (Exception e) {
        Log.e(TAG, "Error copying " + origFile + " to " + newFile);

        throw new TodoException("Error copying " + origFile + " to " + newFile, e);
    }
}

From source file:com.taobao.android.tpatch.utils.MD5Util.java

public synchronized static String getFileMD5String(File file) throws IOException {
    FileInputStream in = null;
    try {/*from  w  ww  . j ava  2  s  . co m*/
        in = new FileInputStream(file);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        messagedigest.update(byteBuffer);
        return bufferToHex(messagedigest.digest());
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.akubraproject.fs.FSBlob.java

private static void nioCopy(File source, File dest) throws IOException {
    FileInputStream f_in = null;
    FileOutputStream f_out = null;

    log.debug("Performing force copy-and-delete of source '" + source + "' to '" + dest + "'");
    try {/*  ww w.ja v  a  2 s.  c o  m*/
        f_in = new FileInputStream(source);

        try {
            f_out = new FileOutputStream(dest);

            FileChannel in = f_in.getChannel();
            FileChannel out = f_out.getChannel();
            in.transferTo(0, source.length(), out);
        } finally {
            IOUtils.closeQuietly(f_out);
        }
    } finally {
        IOUtils.closeQuietly(f_in);
    }

    if (!dest.exists())
        throw new IOException("Failed to copy file to new location: " + dest);
}

From source file:net.darkmist.alib.io.BufferUtil.java

public static ByteBuffer map(File file) throws IOException {
    FileInputStream fin = null;
    FileChannel fc = null;//from   w  w w  .j  av a 2  s  .co  m

    try {
        fin = new FileInputStream(file);
        fc = fin.getChannel();
        return fc.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    } finally {
        fc = Closer.close(fc);
        fin = Closer.close(fin);
    }
}

From source file:Main.java

/**
 * Effective way to copy files by file channel.
 * @return Return the number of copied files.
 *//*from   www .  ja  v  a2  s . com*/
public static int fileChannelCopy(File[] sources, File[] targets) {
    int result = 0;
    if (sources == null || targets == null) {
        return result;
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel fc_in = null;
    FileChannel fc_out = null;
    try {
        for (int i = 0, len_s = sources.length, len_t = targets.length; i < len_s && i < len_t; ++i) {
            if (sources[i] == null || targets[i] == null) {
                continue;
            }

            fis = new FileInputStream(sources[i]);
            fos = new FileOutputStream(targets[i]);
            fc_in = fis.getChannel();
            fc_out = fos.getChannel();
            fc_in.transferTo(0, fc_in.size(), fc_out);

            ++result;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fc_out != null) {
            try {
                fc_out.close();
            } catch (IOException e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
        if (fc_in != null) {
            try {
                fc_in.close();
            } catch (IOException e) {
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }

    return result;
}

From source file:com.weihuoya.bboo._G.java

public static boolean copyFile(File src, File dst) {
    FileInputStream instream = null;
    FileOutputStream outstream = null;
    FileChannel inchannel = null;
    FileChannel outchannel = null;

    try {/*  www .  j  a va2s . co m*/
        // channel
        instream = new FileInputStream(src);
        outstream = new FileOutputStream(dst);
        inchannel = instream.getChannel();
        outchannel = outstream.getChannel();
        // transfer
        inchannel.transferTo(0, inchannel.size(), outchannel);
        // close
        inchannel.close();
        outchannel.close();
        instream.close();
        outstream.close();
        //
        return true;
    } catch (IOException e) {
        _G.log(e.toString());
    }
    return false;
}

From source file:org.jab.docsearch.utils.FileUtils.java

/**
 * Copy file//from  ww  w.  jav  a 2 s . co m
 *
 * @param sourceFilename       source file
 * @param destinationFilename  destination file
 */
public static boolean copyFile(String sourceFilename, String destinationFilename) {
    if (sourceFilename == null) {
        logger.warn("copyFile() failed because sourceFilename is null");
        return false;
    }
    if (destinationFilename == null) {
        logger.warn("copyFile() failed because destinationFilename is null");
        return false;
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel fcin = null;
    FileChannel fcout = null;
    try {
        // open stream and channel from input
        fis = new FileInputStream(sourceFilename);
        fcin = fis.getChannel();

        // open stream and channel from output
        fos = new FileOutputStream(destinationFilename);
        fcout = fos.getChannel();

        fcin.transferTo(0, fcin.size(), fcout);

        return true;
    } catch (IOException ioe) {
        logger.fatal("copyFile() failed", ioe);
        return false;
    } catch (SecurityException se) {
        logger.fatal("copyFile() failed", se);
        return false;
    } finally {
        try {
            if (fcin != null) {
                fcin.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileChannel");
        }

        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileInputStream");
        }

        try {
            if (fcout != null) {
                fcout.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileChannel");
        }

        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileOutputStream");
        }
    }
}

From source file:thv.th.reader.LevelReader.java

public static THMap read(FileInputStream mapStream, ABuffer tabStream, FileInputStream chunkStream,
        THPalette palette, Color background) throws IOException {
    Vector<BufferedImage> tiles = ChunksReader.readAll(chunkStream, tabStream, palette, background);
    THMap result = new THMap();

    result.setPlayers(mapStream.read());
    mapStream.skip(33);/*from ww w . j  ava  2 s. c  o  m*/

    for (int y = 0; y < 128; ++y) {
        for (int x = 0; x < 128; ++x) {
            int pos = (int) mapStream.getChannel().position();
            int anim = EndianUtils.readSwappedShort(mapStream);

            int layer0id = mapStream.read();
            int layer1id = mapStream.read();
            int layer2id = mapStream.read();

            layer0id = tileMap[layer0id];
            layer1id = tileMap[layer1id];
            layer2id = tileMap[layer2id];

            BufferedImage l0 = tiles.elementAt(layer0id);
            BufferedImage l1 = null;
            BufferedImage l2 = null;
            if (layer1id > 0) {
                l1 = tiles.elementAt(layer1id);
            }

            if (layer2id > 0) {
                l2 = tiles.elementAt(layer2id);
            }

            mapStream.skip(3);

            Tile tile = new Tile(anim, l0, l1, l2, pos);
            result.setTile(x, y, tile);
        }
    }

    for (int y = 0; y < 128; ++y) {
        for (int x = 0; x < 128; ++x) {
            int pid = EndianUtils.readSwappedShort(mapStream);

            result.getTile(x, y).setParcel(pid);
        }
    }

    mapStream.close();
    return result;
}

From source file:org.rapidandroid.ApplicationGlobals.java

/**
 * /*from w w  w. ja v  a 2 s  .c om*/
 */
public static JSONObject loadSettingsFromFile(Context context) {
    FileInputStream fin = null;
    InputStreamReader irdr = null;
    JSONObject readobject = null;
    try {

        fin = context.openFileInput(SETTINGS_FILE);

        irdr = new InputStreamReader(fin); // promote

        int size = (int) fin.getChannel().size();
        char[] data = new char[size]; // allocate char array of right
        // size
        irdr.read(data, 0, size); // read into char array
        irdr.close();

        String contents = new String(data);
        readobject = new JSONObject(contents);

        if (!readobject.has(KEY_ACTIVE_ALL)) {
            //dmyung hack to keep compatability with new version
            readobject.put(KEY_ACTIVE_ALL, false);
        }

        if (!readobject.has(KEY_ACTIVE_LOGGING)) {
            //dmyung hack to keep compatability with new version
            readobject.put(KEY_ACTIVE_LOGGING, false);
        }

        //         mParseCheckbox.setChecked(readobject.getBoolean(KEY_PARSE_REPLY));
        //         mParseReplyText.setText(readobject.getString(KEY_PARSE_REPLY_TEXT));
        //         mNoparseCheckBox.setChecked(readobject.getBoolean(KEY_FAILED_REPLY));
        //         mNoparseReplyText.setText(readobject.getString(KEY_FAILED_REPLY_TEXT));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (irdr != null) {
                irdr.close();
            }
            if (fin != null) {
                fin.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return readobject;
}