JavaプログラミングTips
詳細
1シーケンス化によるオブジェクトの深度コピー
2デフォルトのCharsetを取得
Use the I/O system to get the encoding (if possible), to avoid security restrictions on System.getProperty("file.encoding") in applets (why is that restricted?)
1シーケンス化によるオブジェクトの深度コピー
public static T deepCloneBySerialize(T src) {
T o = null;
try {
if (src != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(src);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
o = (T)ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return o;
}
2デフォルトのCharsetを取得
Use the I/O system to get the encoding (if possible), to avoid security restrictions on System.getProperty("file.encoding") in applets (why is that restricted?)
OutputStreamWriter outWriter = null;
try {
outWriter = new OutputStreamWriter(new ByteArrayOutputStream());
jvmPlatformCharset = outWriter.getEncoding();
} finally {
try {
if (outWriter != null) {
outWriter.close();
}
} catch (IOException ioEx) {
// ignore
}
}