Android Open Source - Mpo_File_Parser_Example File Channel Reader






From Project

Back to project page Mpo_File_Parser_Example.

License

The source code is released under:

MIT License

If you think the Android project Mpo_File_Parser_Example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*The MIT License (MIT)
*/*w ww. java 2  s.  c  om*/
*Copyright (c) 2014 Poming Chen
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/

package tw.edu.nccu.cs.vipl.fun.mpo;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;

public class FileChannelReader {

    private ByteBuffer mStrBuffer = ByteBuffer.allocate(4);
    private ByteBuffer mShortBuffer = ByteBuffer.allocate(2);
    private ByteBuffer mIntBuffer = ByteBuffer.allocate(4);
    private ByteBuffer mLongBuffer = ByteBuffer.allocate(8);
    private FileChannel mFileChannel;

    public FileChannelReader(FileChannel fileChannel) {
        this.mStrBuffer = ByteBuffer.allocate(4);
        this.mShortBuffer = ByteBuffer.allocate(2);
        this.mIntBuffer = ByteBuffer.allocate(4);
        this.mLongBuffer = ByteBuffer.allocate(8);
        this.mFileChannel = fileChannel;
    }

    public short readShort() {
        return readShort(false);
    }

    public short readShort(boolean isLittleEndian) {
        mShortBuffer.clear();
        mShortBuffer.order(isLittleEndian ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
        try {
            mFileChannel.read(mShortBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mShortBuffer.flip();
        return mShortBuffer.getShort();
    }

    public int readInt() {
        return readInt(false);
    }

    public int readInt(boolean isLittleEndian) {
        mIntBuffer.order(isLittleEndian ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
        mIntBuffer.clear();
        try {
            mFileChannel.read(mIntBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mIntBuffer.flip();
        return mIntBuffer.getInt();
    }

    public long readLone() {
        return readLone(false);
    }

    public long readLone(boolean isLittleEndian) {
        mLongBuffer.order(isLittleEndian ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
        mLongBuffer.clear();
        try {
            mFileChannel.read(mLongBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mLongBuffer.flip();
        return mLongBuffer.getLong();
    }

    public String readString() {
        try {
            mFileChannel.read(mStrBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mStrBuffer.flip();
        return new String(mStrBuffer.array());
    }

    public long position() {
        try {
            return mFileChannel.position();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public long size() {
        try {
            return mFileChannel.size();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public void position(long offset) {
        try {
            mFileChannel.position(offset);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public byte[] readBytes(int imageSize) {
        ByteBuffer buffer = ByteBuffer.allocate(imageSize);
        try {
            mFileChannel.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.array();
    }

}




Java Source Code List

tw.edu.nccu.cs.vipl.fun.mpo.FileChannelReader.java
tw.edu.nccu.cs.vipl.fun.mpo.MainActivity.java
tw.edu.nccu.cs.vipl.fun.mpo.MpoFileParser.java
tw.edu.nccu.cs.vipl.task.BlockingLinkedList.java
tw.edu.nccu.cs.vipl.task.CancelableRunnable.java
tw.edu.nccu.cs.vipl.task.TaskManager.java