📄 filesystemreader.java
字号:
package com.ismyway.util;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import com.ismyway.anyview.others.Configure;
public class FileSystemReader {
private FileConnection connection;
private DataInputStream datainputstream;
private String location = null;
private int offset = 0;
private String currentPath = "";
private long filesize = -1l;
private boolean opened = false;
public final static byte DIR_FILES = 1;
public final static byte DIR_ONLY = 2;
public final static byte FILES_ONLY = 3;
public final static short SKIPBUFFER = 16384;
public final static short READBUFFER = 16384;
public FileSystemReader(String s, boolean write) {
location = s;
offset = 0;
opened = false;
if (write) {
open(Connector.READ_WRITE);
} else {
open(Connector.READ);
}
}
public FileSystemReader(String s) {
this(s, false);
}
public void open(int mode) {
if (opened) {
return;
}
try {
currentPath = "file:///" + location;
connection = (FileConnection) Connector.open(currentPath, mode);
if (connection.exists() && !isDirectory()) {
datainputstream = connection.openDataInputStream();
filesize = connection.fileSize();
}
opened = true;
} catch (Exception exception) {
System.out.println("Open connection to '" + location + "' failed: " + exception.getMessage());
exception.printStackTrace();
}
}
public OutputStream openOutputStream() {
try {
return connection.openOutputStream();
} catch (Exception e) {
return null;
}
}
public byte[] read(int len) {
if (isDirectory()) {
return null;
}
byte abyte0[] = null;
long l = filesize;
if (offset + len > (int) l) {
len = (int) (l - offset);
}
if (len == 0) {
return null;
}
try {
abyte0 = new byte[len];
} catch (OutOfMemoryError oome) {
return null;
}
if (READBUFFER > len) { // 一次性读入
try {
datainputstream.read(abyte0);
offset += len;
} catch (IOException ex1) {
return null;
}
} else { // 分段读
int datapos = 0;
int times = len / READBUFFER;
for (int i = 0; i < times; i++) {
byte[] buffer = new byte[READBUFFER];
try {
datainputstream.read(buffer);
System.arraycopy(buffer, 0, abyte0, datapos, READBUFFER);
datapos += READBUFFER;
offset += READBUFFER;
} 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;
}
Configure.gc();
return abyte0;
}
public void skip(int len) {
if (isDirectory()) {
return;
}
offset += len;
if (len < 0) {
try {
datainputstream.close();
datainputstream = connection.openDataInputStream();
//datainputstream.skip(offset);
fastSkip(offset);
} catch (Exception e) {
}
return;
}
/*try {
datainputstream.skip(len);
} catch (Exception e) {
}
return;*/
fastSkip(len);
}
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;
Configure.gc();
}
public void locate(int i) {
if (i < 0 || i > fileSize() - 1) {
return;
}
int len = i - offset;
skip(len);
}
public long fileSize() {
return filesize;
}
public boolean exists() {
boolean flag = false;
flag = connection.exists();
return flag;
}
public boolean isDirectory() {
return currentPath.endsWith("/");
}
public void close() {
try {
if (!isDirectory() && null != datainputstream) {
datainputstream.close();
}
opened = false;
connection.close();
datainputstream = null;
connection = null;
} catch (Exception exception) {
System.out.println("Close connection failed: " + exception.getMessage());
exception.printStackTrace();
}
}
public static String[] listRoots() {
Vector temp = new Vector();
Enumeration rootEnum = FileSystemRegistry.listRoots();
while (rootEnum.hasMoreElements()) {
String root = (String) rootEnum.nextElement();
temp.addElement(root);
}
String[] lists = new String[temp.size()];
for (int i = 0; i < lists.length; i++) {
lists[i] = (String) temp.elementAt(i);
}
return lists;
}
public Enumeration list(String[] suffix, byte type, boolean hidden) {
Enumeration rootEnum = null;
if (connection != null) {
try {
rootEnum = connection.list("*", hidden);
Vector folders = new Vector();
int folder = 0;
while (rootEnum.hasMoreElements()) {
String file = (String) rootEnum.nextElement();
if ((type == DIR_FILES || type == DIR_ONLY) && file.endsWith("/")) {
folders.insertElementAt(file, folder++);
} else if (type == DIR_FILES || type == FILES_ONLY) {
if (null == suffix || FileSystemReader.isAcceptSuffix(suffix, file)) {
folders.addElement(file);
}
}
}
return folders.elements();
} catch (IOException ex) {
}
}
return rootEnum;
}
public static Enumeration list(String path, String[] suffix, byte type, boolean hidden) {
try {
FileConnection conn = (FileConnection) Connector.open("file:///" + path, Connector.READ);
Enumeration rootEnum = conn.list("*", hidden);
conn.close();
Vector folders = new Vector();
int folder = 0;
while (rootEnum.hasMoreElements()) {
String file = (String) rootEnum.nextElement();
if ((type == DIR_FILES || type == DIR_ONLY) && file.endsWith("/")) {
folders.insertElementAt(file, folder++);
} else if (type == DIR_FILES || type == FILES_ONLY) {
if (null == suffix || FileSystemReader.isAcceptSuffix(suffix, file)) {
folders.addElement(file);
}
}
}
return folders.elements();
} catch (Exception e) {
//e.printStackTrace();
return null;
}
}
public boolean isHidden() {
boolean flag = true;
if (connection != null) {
flag = connection.isHidden();
}
return flag;
}
public long lastModified() {
if (connection != null) {
return connection.lastModified();
}
return -1L;
}
public boolean create() {
boolean result = true;
if (connection != null) {
try {
connection.create();
} catch (Exception e) {
e.printStackTrace();
result = false;
}
}
return result;
}
public static boolean mkdir(String path) {
boolean result = true;
try {
String currentPath = "file:///" + path;
FileConnection c = (FileConnection) Connector.open(currentPath, Connector.READ_WRITE);
if (!c.exists()) {
c.mkdir();
}
c.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
}
return result;
}
public static boolean rename(String src, String des) {
boolean result = true;
try {
String currentPath = "file:///" + src;
//String path = src.substring(0, src.lastIndexOf('/'));
des = des.substring(des.lastIndexOf('/') + 1);
FileConnection c = (FileConnection) Connector.open(currentPath, Connector.READ_WRITE);
c.rename(des);
c.close();
} catch (IOException e) {
result = false;
}
return result;
}
public static boolean create(String path) {
boolean result = true;
try {
String currentPath = "file:///" + path;
FileConnection c = (FileConnection) Connector.open(currentPath, Connector.READ_WRITE);
if (!c.exists()) {
c.create();
}
c.close();
} catch (IOException e) {
result = false;
}
return result;
}
public boolean canWrite() {
boolean result = true;
if (connection != null) {
result = connection.canWrite();
}
return result;
}
public boolean setWriteable() {
boolean result = true;
if (connection != null) {
try {
connection.setWritable(true);
} catch (IOException e) {
result = false;
}
}
return result;
}
public boolean write(String str) {
boolean result = false;
if (connection != null) {
try {
connection.openDataOutputStream().writeUTF(str);
result = true;
} catch (IOException ex) {
result = false;
}
}
return result;
}
public static boolean delete(String file) {
boolean result = false;
try {
FileConnection fileconnection;
(fileconnection = (FileConnection) Connector.open("file:///" + file)).delete();
fileconnection.close();
result = true;
} catch (IOException ex) {
result = false;
}
return result;
}
public static byte rewrite(String path, String filename, byte[] abytes) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open("file://" + path);
if (!fc.exists() || !fc.isDirectory()) {
return -1;
}
fc.close();
} catch (IOException ex) {
return -1;
}
try {
fc = (FileConnection) Connector.open("file://" + path + filename);
if (!fc.exists()) {
fc.create();
}
} catch (IOException ioe) {
return -2;
}
try {
DataOutputStream dataoutputstream = fc.openDataOutputStream();
dataoutputstream.write(abytes);
dataoutputstream.flush();
dataoutputstream.close();
fc.close();
} catch (IOException ex1) {
return -3;
}
return 0;
}
public static boolean append(String filename, byte[] abytes) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open("file:///" + filename, Connector.READ_WRITE);
if (!fc.exists()) {
fc.create();
}
OutputStream dos = fc.openOutputStream(fc.fileSize());
dos.write(abytes);
dos.flush();
dos.close();
} catch (IOException ex) {
ex.printStackTrace();
return false;
} finally {
try {
fc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return true;
}
public int getOffset() {
return offset;
}
public DataInputStream getDatainputstream() {
return datainputstream;
}
public long availableSize() {
if (null != connection) {
return connection.availableSize();
}
return -1;
}
public String getFullpath() {
return location;
}
protected static boolean isAcceptSuffix(String[] suffix, String file) {
boolean accept = false;
if (null == suffix) {
accept = true;
}
for (int i = 0; i < suffix.length && !accept; i++) {
if (file.toLowerCase().endsWith(suffix[i])) {
accept = true;
}
}
return accept;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -