Recent Notes

Displaying keyword search results 1 - 10
Created by magnum on October 22, 2012 20:03:05    Last update: October 22, 2012 20:03:05
First, the test command that sleeps random number of seconds ( sleeper.sh ): #!/bin/bash stime=$[$RANDOM % 20] sleep $sti... As comparison, synchronous pipe code: #include <sys/wait.h> #include <stdio.h> #in... Asynchronous pipe code: #include <sys/wait.h> #include <stdio.h> #in...
Created by magnum on September 11, 2012 12:38:00    Last update: September 11, 2012 12:38:00
From bind man page: SYNOPSIS #include <sys/types.h> ... The bind call assigns an address and a port to a socket. That's it. There's no mention of client or server, so it can be used on a client socket or a server socket. But it's necessary for a server socket, otherwise clients do not know how to contact the server. For a client socket, the call is optional. When a client connects to a server or sends a message for the first time, a dynamic port is assigned to the client if there's no port assigned to it. If a port is bind beforehand, there's no dynamic assignment. Test code: int print_sock_info(int sockfd) { struct so... Prints: Local addr: 0.0.0.0, port: 0 Local addr: 0.0.0....
Created by magnum on September 27, 2011 21:51:05    Last update: September 28, 2011 18:02:49
Client socket usually does not call bind . But I've seen code that does and it was puzzling to me what bind does to a client socket. Therefore, this little test program. It retrieves a web url and displays info about the socket. You can optionally give a bind host name/ip and port and see what it does. Here are my test results: $ ./client www.google.com Local addr: 172.16.0.... This is the code: #include <stdio.h> #include <stdlib.h> #incl...
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 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......
Created by Dr. Xi on June 22, 2011 09:42:35    Last update: June 22, 2011 11:39:40
Demo code for CSV parsing with Skife CSV . Java code: import java.io.*; import java.util.*; import... 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| | CLAS... The parser worked correctly. But notice that it counts the spaces outside of the quotes significant, and doing so consistently. One more test for spaces: "Smith, Jack" , "210-345-8888" "Smith, Jack"... Result: Line 1 has 2 values: | Smith, Jack | | 210... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | Two| L...
Created by Dr. Xi on June 21, 2011 15:41:51    Last update: June 22, 2011 11:33:36
Demo code for CSV parsing with Apache Commons CSV parser . Java code: import java.io.*; import org.apache.commons.csv... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... Result: Line 1 has 11 values: |psmith01| |CLASS2B|... The parser worked correctly. Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The third line is invalid input, but throwing a Java IOException is a bit grave. Also, the parser is not able to escape a backslash. Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 21, 2011 15:54:00    Last update: June 22, 2011 11:33:09
Demo code for CSV parsing with SuperCSV parser . Java code: import java.io.*; import java.util.List; imp... 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... The parser messed up on all three lines: Line 1 has 5 values: |psmith01 abc| |CLASS... Using two lines input: "psmith01 abc", "CLASS2B " , " Peter... It generates an exception: Line 1 has 5 values: |psmith01 abc| |CLASS... Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 22, 2011 07:12:02    Last update: June 22, 2011 11:32:02
Demo code for CSV parsing with OstermillerUtils . Java code: import java.io.*; import java.util.List; imp... 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... The parser messed up on all three lines: Line 1 has 6 values: |psmith01 abc| | "CLA... Putting a space before the first quote: "Smith, Jack","210-345-8888" It dismissed the quotes: Line 1 has 3 values: | "Smith| | Jack"| ... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | "Two| ...
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...
Previous  1 2 Next