W
Will Clark
Nope, I'm afraid using the code you have is probably the best...
there is a really convoluted way that uses Serializable to clone an entire
object, if that's what you'd like?...
public static final Serializable clone(Serializable src)
{
try {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.ibjectOutputStream oos = new java.ibjectOutputStream(baos);
oos.writeObject(src);
oos.flush();
oos.close();
java.io.ByteArrayInputStream bais = new
java.io.ByteArrayInputStream(baos.toByteArray());
java.ibjectInputStream ois = new java.ibjectInputStream(bais);
Object dest = ois.readObject();
ois.close();
return (Serializable)dest;
} catch (Throwable th) { return src; }
}
This function does a so-called "deep clone" of most objects in Java (all
that support the Serializable interface) including String, or returns the
same object by reference in case of failure. Not particularily elegant, but
it works well for me!
there is a really convoluted way that uses Serializable to clone an entire
object, if that's what you'd like?...
public static final Serializable clone(Serializable src)
{
try {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.ibjectOutputStream oos = new java.ibjectOutputStream(baos);
oos.writeObject(src);
oos.flush();
oos.close();
java.io.ByteArrayInputStream bais = new
java.io.ByteArrayInputStream(baos.toByteArray());
java.ibjectInputStream ois = new java.ibjectInputStream(bais);
Object dest = ois.readObject();
ois.close();
return (Serializable)dest;
} catch (Throwable th) { return src; }
}
This function does a so-called "deep clone" of most objects in Java (all
that support the Serializable interface) including String, or returns the
same object by reference in case of failure. Not particularily elegant, but
it works well for me!