Notes by Dr. Xi
Displaying notes 51 - 60
Created by Dr. Xi on June 16, 2011 13:51:53
Last update: June 16, 2011 14:02:10
With an absolute path to a file, this is the way to construct a URL :
URL url = new URL("file:///absolute/path/to/file.e...
What if I want a URL for a file relative to the current working directory?
URL url = new URL("file:///./file.ext"); // this ...
Or, you can use the File class to help you out:
URL url = new URL(String.format("file:///%s/file.e...
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 Dr. Xi on May 17, 2011 21:14:58
Last update: May 17, 2011 21:17:58
Read certificate one at a time from BufferedInputStream :
// import java.security.cert.*;
public void rea...
Read all certificates from a file as a collection:
public void readCertificates(File f) throws Except...
Created by Dr. Xi on May 03, 2011 14:27:15
Last update: May 03, 2011 14:28:06
The XML schema for a contact might look like this:
<?xml version="1.0" encoding="UTF-8"?>
<schema ...
With XML digital signatures , a Signature element is inserted inside the contact element after the contact file is signed. Like this:
<?xml version="1.0" encoding="UTF-8" standalone="n...
which no longer validates with the original schema.
The schema should be updated to (with the addition of digital signature namespace, schema import and Signature ref ):
<?xml version="1.0" encoding="UTF-8"?>
<schema ...
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...
Created by Dr. Xi on May 02, 2011 15:59:37
Last update: May 02, 2011 15:59:37
This code snippet gets the default keystore used by the Java keytool and displays the list of aliases along with the key type (certificate or private key).
import java.io.File;
import java.io.FileInputSt...
Created by Dr. Xi on May 02, 2011 15:38:24
Last update: May 02, 2011 15:38:57
When you run a Java application, the default place it looks for trusted certificates is $JAVA_HOME/jre/lib/security/cacerts , which is a keystore that contains a list of trusted CA certificates. You can use the keytool to list the certificates:
C:\>keytool -list -keystore C:\jdk1.6.0_20\jre\lib...
Simply hit return when asked for password, no password is required to list trusted certificates in a keystore.
Created by Dr. Xi on April 26, 2011 20:12:01
Last update: April 28, 2011 15:28:12
An XML schema is a definition of XML files, in XML. It plays the same role as old-time DTDs. Overall, an XML schema file looks like this:
<schema attributeFormDefault = (qualified | u... The attribute meanings: targetNamespace : The name space targeted by the current schema definition. It can be any URI. id and version : For user convenience, the W3C spec defines no semantics for them. xml:lang : Natural language identifier defined by RFC 3306 . attributeFormDefault and elementFormDefault : Set default values for the form attribute for attribute and element declarations. blockDefault and finalDefault : Set default values for the block and final attributes for attribute and element declarations. The W3C defined some built-in datatypes . Examples of primitive datatypes are: string ,...
Created by Dr. Xi on April 28, 2011 11:37:23
Last update: April 28, 2011 11:37:23
The Future interface represents the result of an asynchronous computation. Future provides methods to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. You call one of the three submit methods of ExecutorService to get a Future object:
<T> Future<T> submit(Callable<T> task)
<T> Future<T> submit(Runnable task, T result)
Future<?> submit(Runnable task)
Use the first two to retrieve usable results from the computation. The third option returns a Future that returns null upon successful completion. It is used to simply wait for the task to complete, much like Thread.join() .
import java.util.concurrent.Future;
import java...
Created by Dr. Xi on April 28, 2011 11:06:03
Last update: April 28, 2011 11:06:03
The Java Executor interface replaces the call new Thread(new RunnableTask()).start() with executor.execute(new RunnableTask()) . The concurrent package provides built-in utility classes to manage threads so that you don't need to worry about them.
import java.util.concurrent.TimeUnit;
import ja...