例如Q读取www.kingdart.cn/jaccount/imobile.png 转换为Image
又例如:dwww.kingdart.cn/jaccount/readme.txt 转换为String
只在模拟器上成功我也会,要求是真Z成功Q?br />
/** * -------------------------------------------------- * 字段输入? * -------------------------------------------------- * 从DataInputStreaml承 * 主要增加了从文本格式输入中d数据字段的能? * -------------------------------------------------- * * @author iwinyeah 李永? * @version 1.0.0 * */ import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; public class FieldInputStream extends DataInputStream { public final static int BIN_MODE = 0; public final static int TXT_MODE = 1; int mode; public FieldInputStream(InputStream in, int mode) { super(in); this.mode = mode; } public boolean getBoolean() throws IOException { if (mode == 0) { return readBoolean(); } else { if ("1".equals(next())) { return true; } return false; } } public byte getByte() throws IOException { if (mode == 0) { return readByte(); } else { return (byte) Integer.parseInt(next()); } } public short getShort() throws IOException { if (mode == 0) { return readShort(); } else { return (short) Integer.parseInt(next()); } } public int getInt() throws IOException { if (mode == 0) { return readInt(); } else { return Integer.parseInt(next()); } } public long getLong() throws IOException { if (mode == 0) { return readLong(); } else { return Long.parseLong(next()); } } public String getString() throws IOException { if (mode == 0) { if (read() == 0) { return null; } else { return readUTF(); } } else { return next(); } } // 取下一标识W? private byte[] buffer = new byte[255]; private int length = 0; private boolean eos = false; private final static int INITIAL = 0; private final static int ESCAPE = 1; private final static int COMMENT_START = 2; private final static int LINE_COMMENT = 3; private final static String WHITESPACE = "\n\r\t"; public String next() throws IOException { length = 0; int c = in.read(); int status = INITIAL; READWHILE: while (c != -1) { // 一直读到文件尾 switch (status) { case INITIAL: if (c == '\n' || c == '\t') { // 如果是分隔符 break READWHILE; } else if (c == '\\') { status = ESCAPE; // 设{义字W标? } else if (c == '/') { status = COMMENT_START; // 设注释标? } else { if (WHITESPACE.indexOf(c) < 0) { append(c); } } break; case ESCAPE: // 处理转义字符 switch (c) { case 'n': append('\n'); break; case 'r': append('\r'); break; case 't': append('\t'); break; case 'b': append('\b'); break; case 'f': append('\f'); break; default: append(c); break; } status = INITIAL; // 设正常情冉|? break; case COMMENT_START: // 处理注释 if (c == '/') { status = LINE_COMMENT; // 是行式注? } else { status = INITIAL; // 如果都不是则把注释v始符和刚d的字W都加入到标识符? append('/'); append(c); } break; case LINE_COMMENT: if (c == '\n') { status = INITIAL; // 如果当前注释状态则要一直读到行才恢复正常情况标志 break READWHILE; } break; } c = in.read(); // d下一字符 } if (c == -1) { eos = true; } // 如果d文g时Q标识符长度大于Ӟ则返回标识符Q否则返回NULL? if (length <= 0) { return null; } else { return new String(buffer, 0, length, "UTF-8"); } } // 读入的字符加入~冲? private void append(int c) { // ~冲Zx自动扩展 if (length >= buffer.length) { byte[] xBuffer = new byte[buffer.length + 16]; System.arraycopy(buffer, 0, xBuffer, 0, buffer.length); buffer = null; buffer = xBuffer; } buffer[length++] = (byte) c; } public boolean eos() { return eos; } }
package util; /** * -------------------------------------------------- * 字段输出? * -------------------------------------------------- * 从DataOutputStreaml承 * 主要增加了向输出写入文本格式的数据字段的能? * 文本格式将由TAB分隔字段Q回车换行符分隔记录 * -------------------------------------------------- * * @author iwinyeah 李永? * @version 1.0.0 * */ import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; public class FieldOutputStream extends DataOutputStream { public final static int BIN_MODE = 0; public final static int TXT_MODE = 1; private final static byte[] fieldSplit = {'\t'}; private final static byte[] recordSplit = {'\r','\n'}; private int mode; private boolean nextEnd = false; public FieldOutputStream(OutputStream out, int mode) { super(out); this.mode = mode; } // 接着写入的是否最后一个字D? // 写第一个字D前以参数false调用? // 写最后一个字D前以参数false调用? public void setNextEnd(boolean end){ nextEnd = end; } public void putBoolean(boolean value) throws IOException { if (mode == 0) { writeBoolean(value); } else { out.write(value ? '1' : '0'); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putByte(byte value) throws IOException { if (mode == 0) { writeByte(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putShort(short value) throws IOException { if (mode == 0) { writeShort(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putInt(int value) throws IOException { if (mode == 0) { writeInt(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putLong(long value) throws IOException { if (mode == 0) { writeLong(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putString(String value) throws IOException { if (mode == 0) { if (value == null) { writeByte(0); } else { writeByte(1); writeUTF(value); } } else { if(value != null){ byte[] b = value.getBytes("UTF-8"); for(int i = 0; i < b.length; i++){ if(b[i] == '\n'){ out.write('\\'); out.write('n'); } else if(b[i] == '\r'){ out.write('\\'); out.write('r'); } else if(b[i] == '\t'){ out.write('\\'); out.write('t');} else if(b[i] == '\b'){ out.write('\\'); out.write('b');} else if(b[i] == '\f'){ out.write('\\'); out.write('f'); }else{ out.write(b[i]); } } } out.write(nextEnd ? recordSplit : fieldSplit); } } }
package util; /** * -------------------------------------------------- * 日期转换对象 * -------------------------------------------------- * 主要提供日期?970-01-01后的天数的{换和到字W串的{? * -------------------------------------------------- * * @author iwinyeah 李永? * @version 1.0.0 * */ import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class DateUtil { private static Calendar _calendar = Calendar.getInstance(); // 用于日期计算 private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒? private static int rawOffset = TimeZone.getDefault().getRawOffset(); /** * 日期{换ؓ1970-01-01后的天数 * * @param Date * theDate 要计天数的日期 * @return int 所传入日期?970-01-01相差的天? */ public static int dateToDay(Date theDate) { return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY); } /** * ?970-01-01后的天数转换为日? * * @param int * 要取的日期与1970-01-01相差的天? * @return Date theDate ?970-01-01相差相应天数的日? */ public static Date dayToDate(int day) { return new Date(day * MSEC_EVERYDAY); } /** * 取今天与1970-01-01相差的天? * * @return int 取今天与1970-01-01相差的天? */ public static int toDay() { return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY); } /** * 日期{换ؓq月日字W串 * * @param int * theDay ?970-01-01相差的天? * @return String 对应日期q月日Ş式的字符? */ public static String getYMD(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.YEAR) % 100 + "/" + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (_calendar.get(Calendar.MONTH) + 1) + "/" + (_calendar.get(Calendar.DATE) > 9 ? "" : "0") + _calendar.get(Calendar.DATE); } /** * 日期{换ؓq月字符? * * @param int * theDay ?970-01-01相差的天? * @return String 对应日期q月形式的字W串 */ public static String getYM(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.YEAR) + "/" + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (_calendar.get(Calendar.MONTH) + 1); } /** * 日期{换ؓ月日字符? * * @param int * theDay ?970-01-01相差的天? * @return String 对应日期月日形式的字W串 */ public static String getMD(int theDay) { _calendar.setTime(dayToDate(theDay)); return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (_calendar.get(Calendar.MONTH) + 1) + "/" + (_calendar.get(Calendar.DATE) > 9 ? "" : "0") + _calendar.get(Calendar.DATE); } /** * 日期{换ؓ当月一? * * @param int * theDay ?970-01-01相差的天? * @return int 对应日期所在月份第一天与1970-01-01相差的天? */ public static int getMonthFirstDay(int theDay) { _calendar.setTime(dayToDate(theDay)); _calendar.set(Calendar.DAY_OF_MONTH, 1); return (int) (dateToDay(_calendar.getTime())); } /** * 取日期所在年? * * @param int * theDay ?970-01-01相差的天? * @return int 对应日期所在年? */ public static int getYear(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.YEAR); } /** * 取日期所在月? * * @param int * theDay ?970-01-01相差的天? * @return int 对应日期所在月? */ public static int getMonth(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.MONTH); } /** * 取日期所在周? * * @param int * theDay ?970-01-01相差的天? * @return int 对应日期所在周? */ public static int getWeek(int theDay) { // 1971-01-03是星期日,从该日开始计周? _calendar.setTime(dayToDate(theDay)); return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L); } }
// public class Action{ String name; // 名称 Command command; // 命o int code; // 代码 (由该View的传递到其Controller使用) Item item; // 数据? boolean defaultAction; // 是否是默认的Action //...省略 }
public abstract class AbstractView{ //...省略 // View增加Action public void addAction( final Action action, final boolean active ) { if( !actions.containsKey( action.getName() ) ) { // Action存入Actions表中 actions.put( action.getName(), action ); if( active ) { activateAction( action ); } } } // 使Action生效可用 private void activateAction( final Action action ) { final Command command = action.getCommand(); activeActions.put( command, action ); final Item item = action.getItem(); if( item == null ) { addCommand( command ); // 该Action是屏q相关的命o } else { item.addCommand( command ); // 该Action是数据项相关的命? if( action.isDefaultAction() ) { item.setDefaultCommand( command ); } } } //...省略 // 用户按下相应的命令键?触发执行与其兌的Action public void commandAction( final Command command, final Displayable displayable ) { if( !handleAction( command ) ) { if( displayable instanceof Choice ) { AbstractController.commandAction( this, command, (Choice) displayable ); } else { AbstractController.commandAction( this, command ); } } } // 用户在某个指定了命o的Item按下了命令按钮时触发执行与其兌的Action public void commandAction( final Command command, final Item item ) { if( !handleAction( command ) ) { AbstractController.commandAction( this, command ); } } // Ҏ所触发的命令查扑օ联的Action,q新它发送到Controllerq行处理 public boolean handleAction( final Command command ) { if( activeActions.containsKey( command ) ) { final Action action = (Action) activeActions.get( command ); // 以Action代码为参数生成ControllerEventq传递到controller处理 final ControllerEvent event = new ControllerEvent( action.getCode(), this ); controller.handle( event ); return true; } else { return false; } } //...省略 }
// Ҏ一 public class firstManager implements Runnable { public void runTask() { (new Thread(this)).start(); } public void run() { System.out.println("\nfirst thread method!"); // Do some thing ... } } // Ҏ? public class secondManager { private BackTask backTask; private Timer timer; public secondManager() { backTask = new BackTask(); timer = new Timer(); } public void runTask() { timer.schedule(backTask, 0); } private class BackTask extends TimerTask { public void run() { System.out.println("\nsecond thread method!"); // Do some thing ... } } } // Ҏ? public class thirdManager { private BackTask backTask; private int cmd = 0; public thirdManager() { backTask = new BackTask(); backTask.start(); } public void runTask() { synchronized (backTask) { cmd = 1; backTask.notify(); } } private class BackTask extends Thread { public void run() { while (true) { try { if (cmd == 0) { synchronized (this) { wait(); } continue; } System.out.println("\nthird thread method!"); // Do some thing ... } catch (Exception e) { } cmd = 0; } } } } // 用例 public void main(){ firstManager man1 = new firstManager(); secondManager man2 = new secondManager(); thirdManager man3 = new thirdManager(); man1.runTask(); man2.runTask(); man3.runTask(); }
//... 省略 public static final String WHITESPACE = "\r\n\t "; public String next(final String delimiters, final boolean keepWhitespace, final boolean allowComments, final boolean reuseDelimiter, final boolean processEscape) throws IOException { try { final StringBuffer token = new StringBuffer(); startLine = endLine; startChar = endChar; int c = in.read(); endChar++; int status = INITIAL; while (c != -1) { // 若还未读到文件尾 if (c == '\n') { endLine++; endChar = 0; } switch (status) { case INITIAL: if (delimiters.indexOf(c) > -1) { // 如果是分隔符 lastDelimiter = (char) c; if (isWhiteSpace(c)) { // 如果同时也是I白Wƈ且标识符长度大于零则q回标识W? if (token.length() > 0) { if (reuseDelimiter) { // 如果要重用分隔符则将它推回输入流? in.revert((char) c); } return token.toString(); } // 如果q未有数据,q要l箋往下读 } else { // 如果不是I白W则无论标识W长度是否ؓӞ都要q回 if (reuseDelimiter) { in.revert((char) c); } return token.toString(); } } else if (processEscape && c == '\\') { status = ESCAPE; // 设{义字W标? } else if (allowComments && c == '/') { status = COMMENT_START; // 设注释标? } else if (isWhiteSpace(c)) { if (keepWhitespace) { // 如果I白W也要用Q把它加入标识符? token.append((char) c); } } else { token.append((char) c); } break; case ESCAPE: // 处理转义字符 switch (c) { case 'n': token.append('\n'); break; case 'r': token.append('\r'); break; case 't': token.append('\t'); break; case 'b': token.append('\b'); break; case 'f': token.append('\f'); break; default: token.append((char) c); break; } status = INITIAL; // 设正常情冉|? break; case COMMENT_START: // 处理注释 if (c == '/') { status = LINE_COMMENT; // 是行式注? } else if (c == '*') { status = BLOCK_COMMENT; // 是块式注? } else { status = INITIAL; // 如果都不是则把注释v始符和刚d的字W都加入到标识符? token.append('/').append((char) c); } break; case LINE_COMMENT: if (c == '\n') { status = INITIAL; // 如果当前注释状态则要一直读到行才恢复正常情况标志 } break; case BLOCK_COMMENT: if (c == '*') { status = COMMENT_END; // 如果当前为块注释状态则要一直读?可为块注释l束状? } break; case COMMENT_END: if (c == '/') { status = INITIAL; // 在块l束状态下d/则ؓ块结? } else { status = BLOCK_COMMENT; // 否则块注释还未结束,恢复为块注释状? } break; } c = in.read(); // d下一字符 } // 如果d文g时Q标识符长度大于Ӟ则返回标识符Q否则返回NULL? return token.length() > 0 ? token.toString() : null; } catch (IOException e) { throw new IOException("Error reading input L=" + startLine + " C=" + startChar); } } //... 省略