📄 functions.java
字号:
/*
******************************************************************************
Contains general functions for the application
Copyright (C) 2007 Timothy Lim Sheng Hwee
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author: Timothy Lim Sheng Hwee
Email : LimShengHwee@gmail.com
******************************************************************************
*/
package Twittering;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Vector;
import java.io.ByteArrayInputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
*
* @author Administrator
*/
public class Functions {
////////////////////////////////////////////////////////////
// modified from javascript code - 1
// This code was written by Tyler Akins and has been placed in the
// public domain. It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
// used for http authentication
private static final String keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
public static String encode64(String input) {
String output = "";
char chr1, chr2, chr3;
char chr2V, chr3V;
boolean chr2Exception, chr3Exception;
int enc1, enc2, enc3, enc4;
int i = 0;
do {
chr1 = input.charAt(i++);
try {
chr2Exception = false;
chr2 = input.charAt(i++);
chr2V = chr2;
} catch (Exception e) {
chr2V = 0;
chr2Exception = true;
}
try {
chr3Exception = false;
chr3 = input.charAt(i++);
chr3V = chr3;
} catch (Exception e) {
chr3V = 0;
chr3Exception = true;
}
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2V >> 4);
enc3 = ((chr2V & 15) << 2) | (chr3V >> 6);
enc4 = chr3V & 63;
if (chr2Exception) {
enc3 = enc4 = 64;
} else if (chr3Exception) {
enc4 = 64;
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
keyStr.charAt(enc3) + keyStr.charAt(enc4);
} while (i < input.length());
return output;
}
// modified from javascript code - 0
////////////////////////////////////////////////////////////
// http://www.java2s.com/Code/Java/J2ME/UseGETorPOSTtocommunicatewithaJavaservlet.htm
public static String fetchPage(String uri, String POST_OR_GET, Vector parameters, String authenticationString) {
String page = "";
HttpConnection connection = null;
InputStream inputstream = null;
OutputStream outputstream = null;
try {
try {
if (POST_OR_GET == HttpConnection.GET) {
String getVar = "";
if (! uri.endsWith("?")) uri += "?";
for (int i = 0; (parameters != null) && (i < parameters.size()); i++) {
uri += ((UriVariable) parameters.elementAt(i)).getVariable() +
"=" +
((UriVariable) parameters.elementAt(i)).getValue() +
"&";
}
if (!getVar.equals("")) getVar = "?" + getVar;
}
connection = (HttpConnection) Connector.open(uri);
connection.setRequestMethod(POST_OR_GET);
if ((authenticationString != null) && (!authenticationString.equals("")))
connection.setRequestProperty("Authorization", "Basic " + authenticationString);
if (POST_OR_GET == HttpConnection.POST) {
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String getVar = "";
String dataStr = "";
for (int i = 0; (parameters != null) && (i < parameters.size()); i++) {
if (i > 0) dataStr += "&";
dataStr += ((UriVariable) parameters.elementAt(i) ).getVariable() +
"=";
String tmp = ( (UriVariable) parameters.elementAt(i) ).getValue();
// do url encoding
dataStr += UrlEncode(( (UriVariable) parameters.elementAt(i) ).getValue());
}
if (!dataStr.trim().equals(""))
{
outputstream = connection.openOutputStream();
// do utf encoding
//System.out.println(dataStr);
//outputstream.write(dataStr.getBytes());
outputstream.write(new String(dataStr.getBytes(), Constants.ENCODING).getBytes());
outputstream.close();
}
}
inputstream = connection.openInputStream();
// 1) Get status Line
if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
// 2) Get header information - none
// 3) Get body (data)
int length = (int) connection.getLength();
String str;
if (length != -1) {
byte servletData[] = new byte[length];
inputstream.read(servletData);
page = new String(servletData);
} else // Length not available...
{
byte bytes[] = new byte[1];
String msg = "";
int ch;
while ((bytes[0] = (byte) inputstream.read()) != -1) {
msg += new String(bytes);
}
page = msg;
}
} else {
page = new String( "HTTP response error:" + connection.getResponseMessage());
}
} catch(Exception e) {
page = "HTTP error: " + e.toString() + page;
} finally {
if (connection != null)
connection.close();
if (inputstream != null)
inputstream.close();
if (outputstream != null)
outputstream.close();
}
} catch (Exception e) {
page = "Closing connection / stream error: " + e.toString() + page;
}
return page;
}
public static final String[][] ENCODED_CHARS =
{ {"%", "%25"},
{"!", "%21"},
{"*", "%2a"},
{"'", "%27"},
{"(", "%28"},
{")", "%29"},
{";", "%3B"},
{":", "%3A"},
{"@", "%40"},
{"&", "%26"},
{"=", "%3D"},
{"+", "%2B"},
{"$", "%24"},
{",", "%2C"},
{"/", "%2F"},
{"?", "%3F"},
{"#", "%23"},
{"[", "%5B"},
{"]", "%5D"}
};
//*/
public static String UrlEncode(String str) {
int pos = 0;
int x,y;
for(x = 0; x < ENCODED_CHARS.length; x++) {
while ((pos = str.indexOf(ENCODED_CHARS[x][0], pos)) != -1) {
str = str.substring(0, pos) + ENCODED_CHARS[x][1] + str.substring(pos+1);
pos += ENCODED_CHARS[x][1].length();
}
}
return str;
}
public static final String[][] XML_DATA =
{ {"&", "&"},
{"<", "<"},
{">", ">"},
{""", "\""},
{"'", "'"}
};
public static String encodeXMLData(String str) {
int pos = 0;
int x,y;
for(x = 0; x < XML_DATA.length; x++) {
while ((pos = str.indexOf(XML_DATA[x][1], pos)) != -1) {
str = str.substring(0, pos) + XML_DATA[x][0] + str.substring(pos+1);
pos += XML_DATA[x][0].length();
}
}
return str;
}
public static String decodeXMLData(String str)
{
Hashtable ht = new Hashtable();
int start, end, pos;
int x,y;
for (x = 0; x < XML_DATA.length; x++)
ht.put(XML_DATA[x][0], XML_DATA[x][1]);
start = 0; end = 0;
str = " " + str + " ";
while ((pos = str.indexOf("&", start)) != -1)
{
if( (end = str.indexOf(";", pos)) != -1)
{
String sub = str.substring(pos,end+1);
String val = (String) ht.get(sub);
if (val != null)
{
String a = str.substring(0, pos);
String b = str.substring(end+1);
str = str.substring(0, pos) + val + str.substring(end+1);
start = pos + val.length();
}
else
{
start = pos + 1;
}
}
else
{
start = pos+1;
}
}
str = str.substring(1, str.length()-1);
return str;
}
public static String[] getXMLEntry(String tag, String page)
{
String[] msg = new String[2];
int posStart, posEnd;
posStart = page.indexOf("<" + tag + ">");
if (posStart == -1) return null;
posEnd = page.indexOf("</" + tag + ">", posStart);
if (posEnd== -1) return null;
msg[0] = Functions.decodeXMLData(page.substring(posStart + tag.length() + 2, posEnd));
msg[1] = page.substring(posEnd + tag.length() + 3);
return msg;
}
// binary insertion
public static void insertAlpha(Vector v, String str, int start, int stop, boolean ignoreCase)
{
if(v.size() == 0)
{
v.addElement(str);
return;
}
int pos = (start + stop) / 2;
//System.out.println(str + "|" + start +"|" + pos + "|" + stop + "|"+v.size());
// in left side
String tmpStr, tmpCompareStr;
tmpStr = str;
tmpCompareStr = (String) v.elementAt(pos);
if (ignoreCase)
{
tmpStr = tmpStr.toLowerCase();
tmpCompareStr = tmpCompareStr.toLowerCase();
}
if (tmpStr.compareTo(tmpCompareStr) < 0)
{
//System.out.println("left");
if(pos == start)
v.insertElementAt(str, pos);
else
insertAlpha(v, str, start, pos, ignoreCase);
}
// in right side
else if (tmpStr.compareTo(tmpCompareStr) > 0)
{
//System.out.println("right");
if(pos+1 > stop)
v.insertElementAt(str, pos+1);
else
insertAlpha(v, str, pos+1, stop, ignoreCase);
}
else
{
v.insertElementAt(str, pos);
}
}
public static int insertAlphaGetPos(Vector v, String str, int start, int stop, boolean ignoreCase)
{
if(v.size() == 0)
{
return 0;
}
int pos = (start + stop) / 2;
//System.out.println(str + "|" + start +"|" + pos + "|" + stop + "|"+v.size());
// in left side
String tmpStr, tmpCompareStr;
tmpStr = str;
tmpCompareStr = v.elementAt(pos).toString();
if (ignoreCase)
{
tmpStr = tmpStr.toLowerCase();
tmpCompareStr = tmpCompareStr.toLowerCase();
}
if (tmpStr.compareTo(tmpCompareStr) < 0)
{
//System.out.println("left");
if(pos == start)
return pos;
else
return insertAlphaGetPos(v, str, start, pos, ignoreCase);
}
// in right side
else if (tmpStr.compareTo(tmpCompareStr) > 0)
{
//System.out.println("right");
if(pos+1 > stop)
return pos+1;
else
return insertAlphaGetPos(v, str, pos+1, stop, ignoreCase);
}
else
return pos;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -