Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, April 8, 2009

Process.getInputStream()

The concept of an InputStream always refers to data coming IN to the thread which is invoking the InputStream method. Similarly, an OutputStream always refers to data pushed OUT by the thread which is invoking the OutputStream method.

The only place where this is really tricky is when you are working with Process objects. The getInputStream() method of Process returns a Java InputStream that reads from the standard output stream of the running Process.

The key thing to remember when using Runtime.exec() is you must consume everything from the child process' input stream. Otherwise, the child process may hang due to the buffer filling up.


Process proc = Runtime.getRuntime().exec(cmd);

StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());
errorGobbler.start();
outputGobbler.start();

if (proc.waitFor() != 0) {
System.out.println("RestoreMain: Migration failed.");
result = false;
}


StreamGobbler

public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
// ------------------------------------------------------------
// Exhaust Stream
// ------------------------------------------------------------
log.debug("StreamGobbler: " + line);
}
} catch (Exception e) {
log.debug("Failed to exhaust stream.");
}
}

Friday, March 20, 2009

Inner Classes

Inner Classes gives us a good introductions. Inner classes come in four flavors:: Static member class, Member class, Local class, Anonymous class. And the advantages of inner classes can be divided into three categories: an object-oriented advantage, an organizational advantage, and a call-back advantage.

The OO advantage is not well known for me compared with call-back advantage. One good example is to separate search algorithm as inner class in Tree class. Another is to nest MuListElement class inside BeastGetActiveMuRespMsg - composition relationship in UML.


public class BeastGetActiveMuRespMsg {
private int retCode;
private MuListElement[] muList;
....
public static class MuListElement {
private String macAddress;
private String ipAddress;
...
}
}

A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

Like a static member class, a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.

Friday, January 2, 2009

Mutablity


private MutableLocation loc = new MutableLocation();
public Location getLocation() {
return loc;
}

Internally, you store a mutable object, but return it typed as a simple interface when you want to allow only read-only access.This is done to preserve encapsulation and prevents the location fields from being modified by external code.

Tuesday, June 12, 2007

Java 5 Enum

Java 5 enum is a special type of class - subclass of java.lang.Enum. Java compiler transparently translate it. Each symbolic constant of enum is an instance of the class. The constructor must be private to avoid programmatically instantiation. It can also have private attribute and methods.


enum TrafficLight {RED, GREEN, BLUE; }
<=>
public class TrafficLight {
private final String color;
private TrafficLight(String color) { this.color = color; }

public static final TrafficLight GREEN = new TrafficLight(“GREEN”);
...
}

So,on the caller side:

TrafficLight tl = TrafficLight.RED;

Alternatively,

enum TrafficLight {RED("stop"), GREEN("go"), BLUE("warn"); }

where the string is passed in as constructor parameter.

Also, since it is normally inside a file with other class, it cannot be declared as public in that case.

-Some common methods

public enum Shape { RECTANGLE, CIRCLE, LINE }
System.out.println(drawing); // Prints RECTANGLE, toString() will return name
System.out.println(drawing.ordinal()); // Prints 0 for RECTANGLE.
Shape drawing = Shape.valueOf("RECTANGLE"); // convert a string to Enum object

Reference Link