Avoid exception occured while writing to the Java object stream