Notes by Dr. Xi

Displaying keyword search results 1 - 10
Created by Dr. Xi on June 22, 2011 13:23:19    Last update: April 30, 2012 11:40:06
This is a utility to convert a byte array to hex string, and to convert a hex string back to the original. To convert a String to hex, you must call String.getBytes() first. The utility does not pretend to know your string encoding. public class StringHexUtil { public static ... This also works: String binaryToHex(byte[] data) { StringBui...
Created by Dr. Xi on November 11, 2011 10:05:22    Last update: November 11, 2011 10:12:01
This is an HTML image tag filter using Java regex. It takes a string, finds the img tags, replaces the src attribute with one provided by the filter, then adds a class name to the class attribute. import java.util.regex.*; import java.io.*; ... Test file: <div id="HTML snippet"> <img src="img/big/txt-m...
Created by Dr. Xi on September 30, 2011 15:34:47    Last update: September 30, 2011 15:34:47
A naive try would be something like this: $ nc -l 8082 | nc remote_host 80 Yes, it does forward the request from local port 8082 to remote_host:80 , but the response is dumped to stdout , not routed back to the client as expected. Using a named pipe makes it work: $ mkfifo backpipe $ nc -l 8082 0<backpipe | nc ... Use tee to get a glimpse of the response through the pipe (I wasn't able to find a way to dump the request): $ nc -k -l 8082 0<backpipe | nc localhost 80 | tee... The GNU netcat has a different syntax than the stock nc . It also supports different switches. To listen to port 1234: $ netcat -l -p 1234...
Created by Dr. Xi on September 19, 2011 11:56:42    Last update: September 19, 2011 11:57:26
It is well known that with the -D switch you can turn an ssh session into a socks proxy: ssh -D localhost:8080 remote_user@remote_host Now configure your browser to use " localhost:8080 " as a socks proxy and your web traffic is routed through remote_host via an ssh tunnel. But sometimes you encounter an error message like this: Disconnecting: Bad packet length - 1416586337 This is because sometimes, the ssh session outputs some "welcome" message into the tunnel, polluting the protocol stream between the client and the remote host. A safer way to establish an ssh socks proxy would be: ssh -N -D localhost:8080 remote_user@remote_host
Created by Dr. Xi on February 17, 2011 12:34:58    Last update: August 10, 2011 09:04:32
String comparison operators: Operator Meaning -n str the length of str is nonzero. -z str the length of str is zero (0). str1 = str2 str1 and str2 are the same (note one equal sign, not two!). str1 != str2 str1 and str2 are not the same. str str is not a null string Examples: # empty string is not null if [ '' ]; then ... Numerical comparisons ( integer expressions only! ): Operator Meaning int1 -eq int2 int1 and int2 are numerically equal int1 -ne int2 int1 and int2 are numerically NOT equal int1 -gt int2 int1 is greater than int2 int1 -ge int2 int1 is greater than or equal to int2 int1 -lt int2 int1 is less than int2 int1 -le...
Created by Dr. Xi on July 14, 2011 11:50:28    Last update: July 14, 2011 11:50:28
This is not to make the size of the array smaller, but to remove the leading and trailing spaces in each element. This works: String[] a = { " 1 ", " 2 " }; for (int i = 0; ... This does not work: for (String s: a) { s = s.trim(); }
Created by Dr. Xi on July 14, 2011 08:26:05    Last update: July 14, 2011 08:26:05
You can iterate through an array of String like this: String[] a = { "one", "two", "three" }; for (in... or like this: for (String s: a) { System.out.println(s); ... For a string Collection , iterate like this: Collection<String> c = Arrays.asList("one", "two",... or like this: Collection<String> c = Arrays.asList("one", "two",...
Created by Dr. Xi on September 04, 2008 18:34:25    Last update: July 14, 2011 07:31:48
The one argument version of String.split omits trailing empty strings. To get all parts, including trailing empty strings, you need to use the two argument version with a negative limit : public class TestStringSplit { public stati... Output: Length of array: 8 Part[0]: '1' Part[1]: '2'...
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 Dr. Xi on June 22, 2011 07:33:45    Last update: June 22, 2011 11:57:54
Demo code for CSV parsing with OpenCSV . Java code: import java.io.*; import au.com.bytecode.opencs... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The parser: Escaped quote and backslash correctly Ignored spaces before the quotation mark - sometimes (see below) Counted spaces after the right quotation mark till the comma as content, including the right quotation mark (bug). Ignored improperly quoted item - silently (third line) Indeed, the OpenCSV parser has a problem with spaces: Input: "Smith, Jack", "210-345-8888" "Smith, J... Result: Line 1 has 2 values: | Smith, Jack| |210-3......
Previous  1 2 3 4 5 6 7 Next