When one needs some very simple java client that reads some data from socket, then it might be a bit annoying to effectively detect that there is no more data to read, aka “End of File”. Here I show an approach to solve some issues to avoid spurious IOExceptions.
Read the rest of this entry »
How to prevent EOFException when socket is closed
November 9, 2010Java: Class.forName() to retrieve inner class
August 19, 2010Sounds trivial, but was not obvious because Eclipse IDE mislead me: How do I get a reference to a inner class by calling Class.forName() ?
package a.b.c;
public class Outer {
...
public static class Inner {
...
}
...
}
I first tried to call Class.forName() passing as parameter the fully qualified name generated by Eclipse:
Class innerClass = Class.forName("a.b.c.Outer.Inner");
But that was wrong! After wondering for some time, I found the (obvious solution):
Class innerClass = Class.forName("a.b.c.Outer$Inner");
Looks like as Eclipse “fully qualified” name is not exactly the same fully qualified name that the Java compiler uses to identify inner classes.
Serializing Hibernate objects as XML using XStream
July 16, 2010People have been looking for serializing Hibernate as XML using XStream, a task that is not straightforward.
I designed a solution that I would like to share with the open source community, called xstream-for-pojo.
Running Eclipse on a machine without Java
July 3, 2010As “portable applications” are getting more and more popular, it is important to say that Eclipse has always been portable: just unzip it anywhere and run it. The real issue is not having Java Runtime Environment installed on the machine where you want to run Eclipse. Unfortunately, installing Java on Windows requires full administrator rights.
Criticism on SWT
April 20, 2010The Standard Widget Toolkit (SWT) is a graphical widget toolkit for the Java platform. It conceived as an alternative to the AWT or Swing toolkits provided by Sun Microsystems. It has the intent for better performance (to run faster and and to consume less memory), native look and feel and deep platform integration.
As there is already a lot of discussion about Swing and SWT on the web and I will not compare them again. I personally prefer SWT because I felt more productive with this toolkit. Nevertheless, I still did not yet feel productive enough to consider SWT valuable. Let me explain why.
Java and incorrect timezone on Windows XP
April 15, 2010How reliable is Java date, time and timezone API? In this article I discuss a relevant issue that may compromise application correctness when relying not only on date/time calculations, but also on reporting date/time according to local timezone and locale.
When Java code becomes funny
January 4, 2010Eventually, some fun happens while writing Java code.
Apache Commons: Error or Success?
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. (see Java API docs). Does it make sense to call such subclass “Error“?
Found at org.apache.commons.configuration.plist.PropertyListParser
Two way names
Particularly, the Eclipse naming convention resulted this interesting symmetric method declaration.
This documentation is available at Eclipse JFace documentation.
JFace table sorting
January 4, 2010Nowadays, when the application displays as a table, the modern user expects to click on the the column header to change the ordering criteria. Unfortunately, SWT/JFace API have not yet implemented this feature transparently. The programmer is required to write a bunch of code to implement this feature on each table.
For this purpose, I have written a single class that listens to table columns and applies ordering to the table viewer.
Printing java logger configuration
December 10, 2009I have been wondering for some time how to print out a list with details how loggers were configured.
In order to work, one requires to create at least one logger instance of each logger that shall be listed.
I will provide further details in future.
public void print(PrintStream out) {
out.println("Configuração de log: ");
LogManager logManager = LogManager.getLogManager();
Enumeration<String> loggerNames = logManager.getLoggerNames();
Set<String> names = new TreeSet<String>();
Set<Handler> handlerSet = new HashSet<Handler>();
while (loggerNames.hasMoreElements()) {
String name = loggerNames.nextElement();
names.add(name);
}
for (String name : names) {
Logger logger = logManager.getLogger(name);
String nome = logger.getName();
if (nome.length() == 0) nome = "<root>";
// boolean inheritedLevel = false;
Level level = logger.getLevel();
// boolean inheritedHandler = false;
Handler[] handlers = logger.getHandlers();
Logger parentLevelLogger = null;
if (level == null) {
parentLevelLogger = logger.getParent();
while (parentLevelLogger != null && level == null) {
Level parentLevel = parentLevelLogger.getLevel();
if (parentLevel != null) {
level = parentLevel;
} else {
parentLevelLogger = parentLevelLogger.getParent();
}
}
}
Logger parentHandlerLogger = null;
if (handlers == null) {
parentHandlerLogger = logger.getParent();
while (parentHandlerLogger != null && handlers == null) {
Handler[] parentHandlers = parentHandlerLogger.getHandlers();
if (parentHandlers != null) {
handlers = parentHandlers;
} else {
parentHandlerLogger = parentHandlerLogger.getParent();
}
}
}
String nivel = "?";
if (level != null) nivel = level.getName();
if (parentLevelLogger != null) {
if (parentLevelLogger.getName().length() == 0)
nivel = "<root>:"+nivel;
else
nivel = parentLevelLogger.getName()+":"+nivel;
}
String lista = "?";
if (handlers != null) {
List<Handler> list = Arrays.asList(handlers);
if (list.size() == 0) {
lista = "";
} else {
lista = null;
for (Handler handler : list) {
if (lista == null) lista = handler.getClass().getName();
else lista += ", " + handler.getClass().getSimpleName();
}
}
}
if (parentHandlerLogger != null) {
if (parentHandlerLogger.getName().length() == 0)
nivel = "<root>:"+lista;
else
nivel = parentHandlerLogger.getName()+":"+lista;
}
out.format(" - %s (%s): %s\n", nome, nivel, lista);
}
for (Handler handler : handlerSet) {
String nome = handler.getClass().getSimpleName();
String nivel = handler.getLevel().getName();
out.format(" - %s (%s)\n", nome, nivel);
}
}
How to correclty calculate days between two days
April 13, 2009The standard Java API has no solution to calculate the period between two days, although this is a common request for someone who is doing calendar arithmetic.
Googling around, the are many people who expose their own solutions (see some of them 1, 2, 3, 4). All them suggest getting difference between the two dates, measured in number of milliseconds, and divide by 1000 * 60 * 60 * 24 (that means, the number of milliseconds in a day typical day).
However, are these results really reliable? I think not. All those that I have read did not yet consider correctly both daylight savings time changes and leap years.
And looking at the forums, I found many people arguing about their solutions, nevertheless, I suspect that all them are wrong.
Fortunately, one may resort on Joda Time, small and reliable Java package that replaces the Java date and time API and provides tools for all calendar arithmetic. I really recommend getting to know with this package.
Calculating the number of days between two days becomes as simple as (see more):
Days d = Days.daysBetween(startDate, endDate); int days = d.getDays();
And all calendar details and pitfalls are correctly handled by Joda Time. Much metter than relaying on self-, handmade solutions that one cannot easily guarantee to be correct for all situations.
Posted by Daniel Ferber 