/** * Encode a string to the "x-www-form-urlencoded" form, enhanced with the UTF-8-in-URL proposal. * This is what happens: * * <ul> * <li> * <p>The ASCII characters 'a' through 'z', 'A' through 'Z', and '0' through '9' remain the * same. * <li> * <p>The space character ' ' is converted into a plus sign '+'. * <li> * <p>All other ASCII characters are converted into the 3-character string "%xy", where xy * is the two-digit hexadecimal representation of the character code * <li> * <p>All non-ASCII characters are encoded in two steps: first to a sequence of 2 or 3 * bytes, using the UTF-8 algorithm; secondly each of these bytes is encoded as "%xx". * </ul> * * @param s The string to be encoded * @return The encoded string */ public static String URLencode(String s) { StringBuffer sbuf = new StringBuffer(); int len = s.length(); for (int i = 0; i < len; i++) { int ch = s.charAt(i); if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' sbuf.append((char) ch); } else if ('a' <= ch && ch <= 'z') { // 'a'..'z' sbuf.append((char) ch); } else if ('0' <= ch && ch <= '9') { // '0'..'9' sbuf.append((char) ch); } else if (ch == ' ') { // space sbuf.append('+'); } else if (ch <= 0x007f) { // other ASCII sbuf.append(hex[ch]); } else if (ch <= 0x07FF) { // non-ASCII <= 0x7FF sbuf.append(hex[0xc0 | (ch >> 6)]); sbuf.append(hex[0x80 | (ch & 0x3F)]); } else { // 0x7FF < ch <= 0xFFFF sbuf.append(hex[0xe0 | (ch >> 12)]); sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]); sbuf.append(hex[0x80 | (ch & 0x3F)]); } } return sbuf.toString(); }
/** Modified by Gabriele Bianchi 04/01/2006 */ public void run() { StringBuffer connectorStringBuffer; if (Datas.isSSL) { connectorStringBuffer = new StringBuffer("ssl://"); } else { connectorStringBuffer = new StringBuffer("socket://"); } connectorStringBuffer.append(_hostname); connectorStringBuffer.append(":"); connectorStringBuffer.append(_port); connectorStringBuffer.append(""); String connectorString = connectorStringBuffer.toString().trim(); System.out.println(connectorString); try { if (Datas.isSSL) { connection = (SecureConnection) Connector.open(connectorString); } else { connection = (StreamConnection) /*Connector.open*/ getConnectionForRequest(connectorString); } if (connection != null) { SocketConnection sc = (SocketConnection) connection; sc.setSocketOption(SocketConnection.KEEPALIVE, 2); } _cm.notifyConnect(connection, this.openInputStream(), this.openOutputStream()); } catch (Exception e) { e.printStackTrace(); System.out.println("Connessione non riuscita:" + e.getMessage()); _cm.notifyNoConnectionOn("I can't connect, server is unreachable"); DebugStorage.getInstance().Log(0, "Can't connect, server is unreachable", e); } return; }
private void getTrafficSpots() { controller.showProgressBar(); String uploadWebsite = "http://" + controller.selectedCity.URL + "/php/trafficstatus.cache?dummy=ert43"; String[] ArrayOfData = null; StreamConnection c = null; InputStream s = null; StringBuffer b = new StringBuffer(); String url = uploadWebsite; System.out.print(url); try { c = (StreamConnection) Connector.open(url); s = c.openDataInputStream(); int ch; int k = 0; while ((ch = s.read()) != -1) { System.out.print((char) ch); b.append((char) ch); } // System.out.println("b"+b); try { JSONObject ff1 = new JSONObject(b.toString()); String data1 = ff1.getString("locations"); JSONArray jsonArray1 = new JSONArray(data1); Vector TrafficStatus = new Vector(); for (int i = 0; i < jsonArray1.length(); i++) { System.out.println(jsonArray1.getJSONArray(i).getString(3)); double aDoubleLat = Double.parseDouble(jsonArray1.getJSONArray(i).getString(1)); double aDoubleLon = Double.parseDouble(jsonArray1.getJSONArray(i).getString(2)); System.out.println(aDoubleLat + " " + aDoubleLon); TrafficStatus.addElement( new LocationPointer( jsonArray1.getJSONArray(i).getString(3), (float) aDoubleLon, (float) aDoubleLat, 1, true)); } controller.setCurrentScreen(controller.TrafficSpots(TrafficStatus)); } catch (Exception E) { controller.setCurrentScreen(traf); new Thread() { public void run() { controller.showAlert("Error in network connection.", Alert.FOREVER, AlertType.INFO); } }.start(); } } catch (Exception e) { controller.setCurrentScreen(traf); new Thread() { public void run() { controller.showAlert("Error in network connection.", Alert.FOREVER, AlertType.INFO); } }.start(); } }
public void setText(String text, int x, int y) { if (text.indexOf('\n') == -1) { if (!addToLine(x, y, new StringBuffer(text))) { return; } setCursorPosition(x + text.length(), y); updateCaretPosition(); } else { if (!divideLine(x, y)) { return; } String firstLine = text.substring(0, text.indexOf('\n')); if (!addToLine(x, y, new StringBuffer(firstLine))) { return; } text = text.substring(text.indexOf('\n') + 1); char[] chars = text.toCharArray(); StringBuffer stringBuffer = new StringBuffer(""); for (int i = 0; i < chars.length; i++) { if (chars[i] == '\n' || chars[i] == '\r') { if (chars[i] == '\n') { y++; if (!divideLine(0, y)) { return; } setLine(y, stringBuffer); stringBuffer = new StringBuffer(); } continue; } stringBuffer.append(chars[i]); if (i == chars.length - 1) { y++; addToLine(0, y, stringBuffer); setCursorPosition(stringBuffer.toString().length(), y); } } updateCaretPosition(); } }
boolean divideLine(int x, int y) // fire <=> enter + insert when # pressed { try { // getLine StringBuffer currentLine = (StringBuffer) vectorLines.elementAt(y); String newLine = currentLine.toString().substring(x, currentLine.toString().length()); currentLine.delete(x, currentLine.length()); // setLine vectorLines.setElementAt(currentLine, y); if (shiftDown(y)) { vectorLines.setElementAt(new StringBuffer(newLine), y + 1); } return true; } catch (Exception ex) { System.out.println(ex.toString() + " in divideLine()"); return false; } }
public static String URLdecode(String str) { StringBuffer result = new StringBuffer(); int l = str.length(); for (int i = 0; i < l; ++i) { char c = str.charAt(i); if (c == '%' && i + 2 < l) { char c1 = str.charAt(i + 1); char c2 = str.charAt(i + 2); if (isHexit(c1) && isHexit(c2)) { result.append((char) (hexit(c1) * 16 + hexit(c2))); i += 2; } else result.append(c); } else result.append(c); } return result.toString(); }
public void removeCharacter() { int x = getCursorX(); int y = getCursorY(); if (x != 0) // not first cursor position { if (removeFromLine(x, y)) { if (y != 0) // not first line { /* if(isEmptyLine(y)) { if(removeLine(y)) { int maxX = getLineLength(y-1); setCursorPosition(maxX, y-1); } } else { */ setCursorPosition(x - 1, y); /* } */ } else { setCursorPosition(x - 1, y); } } } else { if (y != 0) // not beginning of the document { if (isEmptyLine(y)) { if (removeLine(y)) { int maxX = getLineLength(y - 1); setCursorPosition(maxX, y - 1); } } else { StringBuffer currentLine = getLine(y); // (StringBuffer)vectorLines.elementAt(y); if (removeLine(y)) { int maxX = getLineLength(y - 1); addToLine(maxX, y - 1, currentLine.toString()); setCursorPosition(maxX, y - 1); } } } } }
private static String getTimeAsString(long millis) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date(millis)); StringBuffer sb = new StringBuffer(); appendTwoDigit(sb, cal.get(Calendar.YEAR)); sb.append('/'); appendTwoDigit(sb, cal.get(Calendar.MONTH) + 1); sb.append('/'); appendTwoDigit(sb, cal.get(Calendar.DAY_OF_MONTH)); sb.append(' '); appendTwoDigit(sb, cal.get(Calendar.HOUR_OF_DAY)); sb.append(':'); appendTwoDigit(sb, cal.get(Calendar.MINUTE)); sb.append(':'); appendTwoDigit(sb, cal.get(Calendar.SECOND)); return sb.toString(); }
private String[] GetDataFromSite() { String uploadWebsite = "http://" + controller.selectedCity.URL + "/cameras/mobile_cam_list.php"; String[] ArrayOfData = null; StreamConnection c = null; InputStream s = null; StringBuffer b = new StringBuffer(); String url = uploadWebsite; System.out.print(url); try { c = (StreamConnection) Connector.open(url); s = c.openDataInputStream(); int ch; int k = 0; while ((ch = s.read()) != -1) { System.out.print((char) ch); b.append((char) ch); } String result = b.toString(); if (!result.equals("")) { ArrayOfData = StringUtil.split(result.toString().trim(), "~~"); if (ArrayOfData.length == 0) { controller.MainMenu(); new Thread() { public void run() { controller.showAlert("Network Error", 0, AlertType.ERROR); } }.start(); } } } catch (Exception e) { System.out.print(e); new Thread() { public void run() { controller.showProgressBar(); } }.start(); // controller.getDisp().setCurrent(this); } return ArrayOfData; }
public static String urlEncode(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { int b = bytes[i] & 0xff; if (b == ' ') { sb.append('+'); } else if (((b >= '0') && (b <= '9')) || ((b >= 'a') && (b <= 'z')) || ((b >= 'A') && (b <= 'Z'))) { sb.append((char) b); } else { sb.append('%'); sb.append(HEX.charAt(b >> 4)); sb.append(HEX.charAt(b & 0xf)); } } return sb.toString(); }
private static String doubleToString(double d, int decimal) { boolean neg = d < 0; d = Math.abs(d); long intp = (long) Math.floor(d); double rest = d - intp; StringBuffer sb = new StringBuffer(); if (neg) sb.append('-'); sb.append(intp); if (decimal > 0) { sb.append('.'); for (int i = 0; i < decimal; i++) { rest *= 10; long digit = ((long) rest) % 10; sb.append(digit); } } return sb.toString(); }
/** * Install a suite. * * @param selectedSuite index into the installList */ private void installSuite(int selectedSuite) { MIDletStateHandler midletStateHandler = MIDletStateHandler.getMidletStateHandler(); MIDletSuite midletSuite = midletStateHandler.getMIDletSuite(); SuiteDownloadInfo suite; String displayName; suite = (SuiteDownloadInfo) installList.elementAt(selectedSuite); midletSuite.setTempProperty(null, "arg-0", "I"); midletSuite.setTempProperty(null, "arg-1", suite.url); midletSuite.setTempProperty(null, "arg-2", suite.label); displayName = Resource.getString(ResourceConstants.INSTALL_APPLICATION); try { midletStateHandler.startMIDlet("com.sun.midp.installer.GraphicalInstaller", displayName); /* * Give the create MIDlet notification 1 second to get to * AMS. */ Thread.sleep(1000); notifyDestroyed(); } catch (Exception ex) { StringBuffer sb = new StringBuffer(); sb.append(displayName); sb.append("\n"); sb.append(Resource.getString(ResourceConstants.ERROR)); sb.append(": "); sb.append(ex.toString()); Alert a = new Alert( Resource.getString(ResourceConstants.AMS_CANNOT_START), sb.toString(), null, AlertType.ERROR); a.setTimeout(Alert.FOREVER); display.setCurrent(a, urlTextBox); } }
private static String base64Encode(byte[] in) { StringBuffer sb = new StringBuffer(); int acc = 0; int stack = 0; int count = in.length; for (int i = 0; i < count; i++) { int b = in[i] & 0xff; acc <<= 8; acc |= b; stack += 8; while (stack > 6) { int index = (acc >> (stack - 6)) & 0x3f; sb.append(BASE64_CHARS.charAt(index)); stack -= 6; } } if (stack > 0) sb.append(BASE64_CHARS.charAt((acc << (6 - stack)) & 0x3f)); if (sb.length() % 4 != 0) { int padding = 4 - (sb.length() % 4); for (int i = 0; i < padding; i++) sb.append('='); } return sb.toString(); }