⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystemreader.java

📁 很好的基于java的手机文本阅读器anyview 2.0源码,
💻 JAVA
字号:
package com.ismyway.anyview;

import com.motorola.io.FileConnection;
import java.io.IOException;
import java.io.DataInputStream;
import com.motorola.io.FileSystemRegistry;
import javax.microedition.io.Connector;

/**
 * <p>Title: AnyView</p>
 *
 * <p>Description: E680(I) Reader</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.ismyway.com</p>
 *
 * @author ZhangJian
 * @version 1.0
 */
public class FileSystemReader {
    private FileConnection connection;
    private DataInputStream datainputstream;
    private String location;
    public static final int DIR_INFO = 0;
    public static final int RD_INFO = 1;
    public static final int NRD_INFO = 2;
    final static int SkipBuffer = 16384;
    final static int BUFFER = 16384;

    public int offset = 0;

    public FileSystemReader(String s) {
        location = s;
        offset = 0;
        open();
    }

    public void open() {
        try {
            String s = "file://" + location;
            connection = (FileConnection) Connector.open(s);
            if (!isDirectory()) {
                datainputstream = connection.openDataInputStream();
            }
        } catch (Exception exception) {
            System.out.println("Open connection  to '" + location +
                               "' failed: " + exception.getMessage());
            exception.printStackTrace();
        }
    }


    public byte[] read(int len) {
        if (isDirectory()) {
            return null;
        }

        byte abyte0[] = null;

        long l = 0L;
        try {
            l = connection.fileSize();
        } catch (IOException ex) {
        }
        if (offset + len > (int) l) {
            len = (int) (l - offset);
        }
        abyte0 = new byte[len];
        if (BUFFER > len) { //一次性读入
            try {
                datainputstream.read(abyte0);
                offset += len;
            } catch (IOException ex1) {
                return null;
            }
        } else { //分段读
            int datapos = 0;
            int times = len / BUFFER;
            for (int i = 0; i < times; i++) {
                byte[] buffer = new byte[BUFFER];
                try {
                    datainputstream.read(buffer);
                    System.arraycopy(buffer, 0, abyte0, datapos, BUFFER);
                    datapos += BUFFER;
                    offset += BUFFER;
                } catch (IOException ex2) {
                    return null;
                }
            }
            int rest = len - datapos;
            byte[] buffer = new byte[rest];
            try {
                datainputstream.read(buffer);
                System.arraycopy(buffer, 0, abyte0, datapos, rest);
                offset += rest;
            } catch (IOException ex3) {
            }
            buffer = null;
        }
        System.gc();
        return abyte0;
    }

    /**
     * 跳过若干位
     * @param len int 需要跳过的数字
     */
    public void skip(int len) {
        if (isDirectory()) {
            return;
        }

        offset += len;
        if (len < 0) {
            //System.out.println("len < 0");
            close();
            open();
            fastSkip((int) offset);
            return;
        }
        fastSkip(len);
        return;
    }

    /**
     * 定位到文档偏移处
     * @param i int 偏移的位置
     */
    public void locate(int i) {
        if (i < 0 || i > fileSize() - 1) {
            return;
        }

        int len = i - offset;
        skip(len);
    }

    /**
     * 快速skip,由于MOTO的skip效率远远低于read,因此将skip改为read
     * @param len int
     */
    private void fastSkip(int len) {
        if (len <= 0) {
            return;
        }

        byte[] b;
        if (SkipBuffer > len) { //一次性读入
            b = new byte[len];
            try {
                datainputstream.read(b);
            } catch (IOException ex) {
            }
        } else { //分段读
            int times = len / SkipBuffer;
            for (int i = 0; i < times; i++) {
                b = new byte[SkipBuffer];
                try {
                    datainputstream.read(b);
                } catch (IOException ex1) {
                }
            }
            int rest = len - SkipBuffer * times;
            b = new byte[rest];
            try {
                datainputstream.read(b);
            } catch (IOException ex2) {
            }
        }
        b = null;
        System.gc();
    }

    public long fileSize() {
        long l = -1L;

        try {
            l = connection.fileSize();
        } catch (IOException ex) {
            return l;
        }
        return l;
    }

    public boolean exists() {
        boolean flag = false;
        flag = connection.exists();
        return flag;
    }


    public boolean isDirectory() {
        boolean flag = false;
        flag = connection.isDirectory();
        return flag;
    }


    public void close() {
        try {
            if (!isDirectory()) {
                datainputstream.close();
            }
            connection.close();
            datainputstream = null;
            connection = null;
        } catch (Exception exception) {
            System.out.println("Close connection failed: " +
                               exception.getMessage());
            exception.printStackTrace();
        }
    }

    public static String[] listRoots() {
        return FileSystemRegistry.listRoots();
    }

    public String[] list() {
        String as[] = new String[0];
        if (connection != null) {
            try {
                as = connection.list();
            } catch (IOException ex) {
            }
        }
        return as;
    }

    public boolean isHidden() {
        boolean flag = false;
        if (connection != null) {
            try {
                flag = connection.isHidden();
            } catch (IOException ex) {
                return false;
            }
        }
        return flag;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -