Recent Notes
Displaying keyword search results 21 - 30
Created by Dr. Xi on June 22, 2011 15:15:15
Last update: June 22, 2011 15:15:15
There's no such thing as an immutable byte array in Java. What we are trying to achieve here is to protect the byte array inside the object from which it is being returned. We want to caller to have the contents of the byte array, but not be able to change the contents inside the object. The trick is to return a copy instead of the internal reference:
public class ReturnByteArray {
public stati...
Created by magnum on June 20, 2011 20:32:38
Last update: June 20, 2011 20:32:38
Here, = prints line number followed by new line, N appends the next line of input into the pattern space .
Print line number at beginning of line, followed by a space:
sed = test.txt | sed 'N;s/\n/ /'
Print line number at beginning of line, followed by a tab:
sed = test.txt | sed 'N;s/\n/\t/'
Print line number right adjusted with 6 leading spaces:
sed = test.txt | sed 'N; s/^/ /; s/\(.\{6,\}\...
Created by magnum on June 20, 2011 16:12:59
Last update: June 20, 2011 20:04:26
Two methods to get the lines matching either <Data> or <data> :
[Dd] matches either D or d, p for print
cat myFile | sed -n '/< [Dd] ata>/p'
Capital I for case insensitive match
cat myFile | sed -n '/<data>/Ip'
Created by Dr. Xi on June 20, 2011 15:18:06
Last update: June 20, 2011 15:18:06
Perl does not have built-in functions to get the base name or dir name from a file path. But on the Unix platforms we can rely on the executables basename and dirname .
#!/usr/bin/perl
$f = '/A path with/space "/file...
The quotes, escapes, and chomp are necessary:
# This fails when there's a space in $f
$base =...
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...
Created by alfa on June 02, 2011 13:51:41
Last update: June 02, 2011 13:51:41
This may be helpful in debugging reflection code.
import java.lang.reflect.Modifier;
public c...
Created by woolf on May 15, 2011 15:11:42
Last update: May 15, 2011 15:11:57
On windows, display IPv4 routing table:
route -4 print
On Linux:
$ route
Kernel IP routing table
Destination ...
This works for both Linux and Windows (" -r ": display routing table, " -n ": display address and port numbers in numerical form):
netstat -rn
Created by freyo on April 06, 2011 14:58:43
Last update: May 05, 2011 14:52:49
To view certificate in CERT.RSA :
C:\tmp>openssl pkcs7 -inform DER -in CERT.RSA -noo...
To convert certificate to PEM:
openssl pkcs7 -inform DER -in CERT.RSA -out CERT.p...
Java keytool also works:
$ keytool -printcert -file CERT.RSA
Owner: EMA...
Created by Dr. Xi on April 20, 2011 21:44:15
Last update: May 02, 2011 20:56:58
The String.format() method provides versatile formatting capabilities. This tutorial tries to present these capabilities in a accessible manner. The format string A format string can contain zero, one, or more format specifiers . The general form of a format specifier is:
%[argument_index$] [flags] [width] [.precision]co... where things in square brackets are optional, and conversion is a character indicating the conversion to be applied to the corresponding variable value. The only required characters in the format specifier is the percent sign % and the conversion character. A simple example: public static void simpleFormat() { System.out... The Argument index The argument index is specified by a number, terminated by the dollar sign $ . The same argument may be repeated multiple times in a format string. Unindexed...