Recent Notes
Displaying keyword search results 41 - 50
Created by Dr. Xi on July 15, 2011 09:25:15
Last update: July 15, 2011 09:25:15
Some methods to search for a substring within a string:
To know that a substring indeed exists within a string:
boolean found = wholeString.contains(substring);
To find where the substring is contained:
int index = wholeString.indexOf(substring);
If the substring is regex:
boolean match = wholeString.matches(".*" + substri...
Case insensitive match: convert both whole string and substring to lowercase, then compare. Or, use case insensitive flag for regex.
Test code:
import java.util.regex.*;
public class Stri...
Created by woolf on July 05, 2011 16:03:48
Last update: July 05, 2011 16:03:48
The DHCP client packaged with BusyBox is udhcpc . It negotiates a lease with the DHCP server and executes a script when it is obtained or lost. There are four possible arguments to this script: deconfig : The script should put the interface in an up but deconfigured state. bound : The script should configure the interface and set any other relevant parameters. renew : This is used when a lease is renewed. IP address does not change but other parameters may change. nak : This happens when a NAK packet is received from the DHCP server. Network parameters are passed to the script via environment variables. Ubuntu documents that the default script is at /etc/udhcpc/default.script . For OpenWRT, it is located at /usr/share/udhcpc/default.script ....
Created by alfa on July 01, 2011 13:16:12
Last update: July 01, 2011 13:16:12
This is a simple doclet that prints all public methods and their parameter names and types.
Code
import com.sun.javadoc.*;
public class List...
Compile
javac -cp $JAVA_HOME/lib/tools.jar:. ListMethodsDo...
Use
javadoc -doclet ListMethodsDoclet -sourcepath /pat...
Created by freyo on June 30, 2011 12:36:26
Last update: July 01, 2011 09:43:54
Code
public void install(View view) {
String url = ...
Screenshots:
Logcat:
D/InstallApp( 338): install: file:///sdcard/GetAp...
Using http://... as the URI for the APK does not work. Android fails to find a handler for the intent (logcat):
No Activity found to handle Intent { act=android.i...
Therefore, download the APK to sdcard then fire the Intent.
Created by freyo on April 01, 2011 14:29:25
Last update: June 29, 2011 13:58:27
Start the emulator ( create an AVD if none exists)
$ tools/emulator -avd Simple8 Create new project $ tools/android create project \ > --package co... where " --target 2 " identifies the target platform as displayed by " tools/android list targets ", which is stored in the properties file default.properties in the project root folder. cd HelloWorld and install debug package onto the running emulator: $ ant install Buildfile: build.xml [set... Launch the Hello World application on the emulator. You'll see something like this: Edit res/values/string.xml , change the contents to: <?xml version="1.0" encoding="utf-8"?> <resourc... Edit res/layout/main.xml , change the contents to: <?xml version="1.0" encoding="utf-8"?> <LinearL... The contents of the text area now refer to a string defined in the resource file strings.xml , instead...
Created by magnum on June 23, 2011 20:15:49
Last update: June 23, 2011 20:29:45
Linux services startup order in general: kernel runs /sbin/init /sbin/init reads /etc/inittab and runs script defined by this line:
si::sysinit:/etc/init.d/rcS switches to runlevel defined by id:3:initdefault: which causes /etc/init.d/rc to be called with the current run level. /etc/init.d/rc calls the scripts under the /etc/rc <current_run_level> .d directory (symbolic links to actual scripts under /etc/init.d/ ) in this order: The KILL scripts first (scripts with name starting with K, i.e., rc?.d/Knn name ): "script_name stop" then, the START scripts (scripts with name starting with S, i.e., rc?.d/Snn name ): "script_name start" Within each group (KILL or START), run scripts from lower priority number (i.e., the nn in the symlink name) to higher priority number. The Upstart init daemon does not use /etc/inittab . Instead, it...
Created by magnum on June 23, 2011 13:44:21
Last update: June 23, 2011 13:59:26
Fedora/Redhat List current status (for httpd):
# chkconfig --list httpd Warning: this works only when one of /etc/rc runlevel .d/ K NN name or /etc/rc runlevel .d/ S NN name exists. It exports wrong status when both K and S scripts exist (in which case the status should be enabled). Enable sshd for run levels 2, 3, 5: # chkconfig --level 2,3,5 sshd on Disable sshd for run levels 2, 3, 5: # chkconfig --level 2,3,5 sshd off By default, the on and off options affect only runlevels 2, 3, 4, and 5. Debian/Ubuntu Add apache2 with defaults : # update-rc.d apache2 defaults If defaults is used then update-rc.d will make links to start the service in runlevels 2, 3, 4, 5 and to stop...
Created by Dr. Xi on June 13, 2011 15:05:27
Last update: June 13, 2011 15:10:24
When you pass parameters from shell to Java, the list arguments may be messed up if there are spaces in the values.
Start with a simple Java test class:
public class EchoParams {
public static voi...
Tests:
$ java EchoParams a b c
Arg: a
Arg: b
Arg...
Now wrap the command in a shell script ( echoparams.sh ):
#!/bin/sh
java EchoParams $*
Tests:
$ ./echoparams.sh a b c
Arg: a
Arg: b
Arg...
The quotes had no effect on the parameters list.
Changing $* to $@ produces the same results.
The correct way to quote the args list is: "$@"
#!/bin/sh
java EchoParams "$@"
Test:
$ ./echoparams.sh a b "c d" "1 2 3 4 5"
Arg: a
...
Created by alfa on June 07, 2011 11:34:26
Last update: June 07, 2011 11:36:37
This is an example that uses dynamic proxies to trace method calls (in logging) and print out elapsed times for them. Because dynamic proxies can only be generated for interfaces, the service classes must be implemented with interface-implementation pairs. Create services A and B. A.java :
public interface A { public void service1()... AImpl.java : import java.util.Random; public class AImpl... B.java : public interface B { public void service1()... BImpl.java : public class BImpl implements B { public vo... The call trace proxy: import java.lang.reflect.*; class TraceProx... The performance proxy: import java.lang.reflect.*; class Performan... The service factory: import java.lang.reflect.*; public class Se... The test class: public class Test { public static void main... The output: Entering AImpl.service1 Entering BImpl.service1... The above example has no information...
Created by alfa on June 03, 2011 09:41:03
Last update: June 03, 2011 09:41:03
Dynamic proxy can be used to eliminate the need to stub out unused interface methods. This is an example for a simple SAX content handler for XML parsing.
The org.xml.sax.ContentHandler interface requires 11 methods be implemented but we only need three:
import java.io.IOException;
import java.util.Ar...
With a dynamic proxy, we don't need the empty blocks for the unused methods:
import java.io.IOException;
import java.util.Ar...
Equivalently (with anonymous inner class):
import java.io.IOException;
import java.util.Ar...
demo.xml :
<breakfast-menu>
<food>
<name>Belgian W...