Recent Notes
Displaying notes 41 - 50
Created by woolf on February 03, 2010 04:15:33
The replace method for JavaScript strings can either replace a string or a regular expression: // replace string with string 'ActionScript'.replace('Action', 'Java'); // replace regex with string, case insensitive match 'ActionScript'.replace(/ACTION/i, 'Java'); // regex, case insensitive, back reference 'ActionScript'.replace(/(ACtion)/i, 'Java$1')
Created by woolf on February 03, 2010 04:02:07
Last update: February 03, 2010 04:03:18
Redirect without delay: <html> <body> <script type="text/javascript"> window.location='http://www.google.com'; </script> You won't see this if redirecting without delay! </body> </html> Redirect with delay: <html> <body> <script type="text/javascript"> window.setTimeout("window.location='http://www.google.com'", 5000); </script> But you will see this if you delay 5 seconds. </body> </html>
Created by Dr. Xi on February 03, 2010 03:48:00
The ORA-06512 code is not the actual error. It merely indicates on which line the error occurred. The real error precedes ORA-06512 . For example: ERROR at line 1: ORA-29400: data cartridge error IMG-00714: internal error ORA-28579: network error during callback from external procedure agent ORA-06512: at "ORDSYS.ORDIMGEXTCODEC_PKG", line 164 ORA-06512: at "ORDSYS.ORDIMGEXTCODEC_PKG", line 160 ORA-06512: at line 1 ORA-06512: at "ORDSYS.ORDIMG_PKG", line 525 ORA-06512: at "ORDSYS.ORDIMAGE", line 59 ORA-06512: at "TKIDIUSER.IMG_PROCESSCOPY", line 12 ORA-06512: at "TKIDIUSER.TKIDIPCU_MAIN", line 57 ORA-06512: at line 1
Created by Dr. Xi on February 03, 2010 03:29:42
Last update: February 03, 2010 03:31:02
You can use the java.util.Calendar.add method to do date arithmetics. All calculations are straigntforward except for adding or subtracting months. import java.text.*; import static java.util.Calendar.*; public class TestCalendar { private static final DateFormat FORMAT = new SimpleDateFormat("MM/dd/yyyy"); public static void main(String[] args) throws Exception { Calendar c = Calendar.getInstance(); c.set(DAY_OF_MONTH, 1); System.out.println("Initial Date: " + FORMAT.format(c.getTime())); c.add(DAY_OF_MONTH, -1); System.out.println("One Day Ago: " + FORMAT.format(c.getTime())); c.add(MONTH, 1); System.out.println("One Month Later: " + FORMAT.format(c.getTime())); c.add(MONTH, 1); System.out.println("Another Month Later: " + FORMAT.format(c.getTime())); } } The output is: C:\>java TestCalendar Initial Date: 02/01/2010 One Day Ago: 01/31/2010 One Month Later: 02/28/2010 Another Month Later: 03/28/2010 You started from January month end, advancing two months, you are no longer at month end. Use this trick to advance from onth ...
Created by Dr. Xi on February 01, 2010 23:56:25
This simple class demonstrates the use of java.text.SimpleDateFormat . The strict option can be used to validate a date string. The lenient option takes all kinds of junk and is not suitable for validating date strings. import java.util.*; import java.text.*; public class TestDateParser { private static String[][] testCases = { // strict parsing {"MM/dd/yyyy", "11/2/2009", "false"}, {"MM/dd/yyyy", "11/2/2009 11:23:00", "false"}, {"MM/dd/yyyy HH:mm:ss", "11/12/2009 11:98:66", "false"}, {"MM/dd/yyyy", "15/2/2009 11:23:00", "false"}, {"MM/dd/yyyy", "11/32/2009 11:23:00", "false"}, {"MM/dd/yyyy", "11/0/2009 11:23:00", "false"}, {"MM/dd/yyyy", "11//2009 11:23:00", "false"}, {"MM/dd/yyyy", "11/2009 11:23:00", "false"}, // lenient parsing {"MM/dd/yyyy", "11/2/2009", "true"}, {"MM/dd/yyyy", "11/2/2009 11:23:00", "true"}, {"MM/dd/yyyy HH:mm:ss", "11/12/2009 11:98:66", "true"}, {"MM/dd/yyyy", "15/2/2009 11:23:00", "true"}, {"MM/dd/yyyy", "11/32/2009 11:23:00", "true"}, {"MM/dd/yyyy", "11/0/2009 11:23:00", "true"}, {"MM/dd/yyyy", "11/-1/2009 11:23:00", "true"}, {"MM/dd/yyyy", "11//2009 11:23:00", "true"}, {"MM/dd/yyyy", "11/2009 11:23:00", "true"} }; ...
Created by Dr. Xi on January 29, 2010 23:40:11
Load from specified PATH // import java.util.Properties Properties p = new Properties(); in = new FileInputStream("prog.properties"); // relative path // or absolute path // in = new FileInputStream("C:/tmp/prog.properties"); try { p.load(in); } finally { in.close(); } Load from CLASSPATH Properties p = new Properties(); // Load from classpath // This method delegates to this object's class loader. If this // object was loaded by the bootstrap class loader, the method // delegates to ClassLoader.getSystemResourceAsStream in = this.getClass().getResourceAsStream("prog.properties"); try { p.load(in); } finally { in.close(); } Write Properties object to a file // Write properties file. out = new FileOutputStream("prog.properties"); try { p.store(out, "add a comment"); } finally { out.close(); }
Created by Dr. Xi on January 29, 2010 22:07:44
This class demonstrates how to read a binary file in Java. This is the Java version of cat , probably one of the first programs you ever use on a *NIX platform. import java.io.*; public class JavaCat { private static final int BUFFER_SIZE = 4096; public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("Usage: java " + JavaCat.class.getName() + " <input file>"); System.exit(1); } String fileName = args[0]; // nested initialization, // or in design pattern jargon, decorate FileInputStream with // BufferedInputStream. BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName)); byte[] buffer = new byte[BUFFER_SIZE]; try { int n = in.read(buffer, 0, BUFFER_SIZE); while (n >= 0) { processBuffer(buffer, 0, n); n = in.read(buffer, 0, BUFFER_SIZE); } } finally { // always ...
Created by Dr. Xi on January 29, 2010 21:08:43
This class reads a text file line by line and echos the contents to standard out. import java.io.*; public class ReadTextFile { public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("Usage: java " + ReadTextFile.class.getName() + " <input file>"); System.exit(1); } String fileName = args[0]; // nested initialization, // or in design pattern jargon, decorate FileInputStream with // BufferedInputStream. BufferedReader in = new BufferedReader(new FileReader(fileName)); try { // read file line by line String line = in.readLine(); while (line != null) { processLine(line); line = in.readLine(); } } finally { // always close input stream in.close(); } } private static void processLine(String line) { System.out.println(line); } }
Created by voodoo on January 26, 2010 04:56:46
Last update: January 26, 2010 04:57:09
The ping command should give a rough estimate of round trip time (RTT): Pinging google.navigation.opendns.com [208.69.36.231] with 32 bytes of data: Reply from 208.69.36.231: bytes=32 time=47ms TTL=56 Reply from 208.69.36.231: bytes=32 time=46ms TTL=56 Reply from 208.69.36.231: bytes=32 time=46ms TTL=56 Reply from 208.69.36.231: bytes=32 time=47ms TTL=56 Ping statistics for 208.69.36.231: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 46ms, Maximum = 47ms, Average = 46ms In case ping is blocked, you can use synack .
Created by voodoo on January 26, 2010 04:41:31
If no packets are dropped, TCP throughput can be calculated from the TCP window size and network latency: throughput = TCP window size / RTT (roud trip time) Assume that the TCP window size is 64KB and the RTT is 40ms, the throughput is: 65936 Bytes / 40 ms = 1.6 MBytes/s = 13 Mbits/s Note that the throughput has nothing to do with the bandwidth, which is the result of assuming that no packets are dropped, basically assuming that the bandwidth is infinite. Therefore, the bandwidth shown above is the maximum possible bandwidth given TCP window size and latency. But in reality the bandwidth is limited and throughput won't grow infinitely with the TCP window size - it can only grow to as big ...