[libbluray-devel] Deep copy implementation
hpi1
git at videolan.org
Sat Mar 30 01:39:08 CET 2013
libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Sun Mar 17 10:12:02 2013 +0200| [8b03feb53f6117d581ab831781ff01f6dda6b7f5] | committer: hpi1
Deep copy implementation
(with xlet classloader to resolve classes)
> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=8b03feb53f6117d581ab831781ff01f6dda6b7f5
---
src/libbluray/bdj/java/org/videolan/Copy.java | 72 +++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/src/libbluray/bdj/java/org/videolan/Copy.java b/src/libbluray/bdj/java/org/videolan/Copy.java
new file mode 100644
index 0000000..ea48ae0
--- /dev/null
+++ b/src/libbluray/bdj/java/org/videolan/Copy.java
@@ -0,0 +1,72 @@
+/*
+ * This file is part of libbluray
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package org.videolan;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamClass;
+import java.io.Serializable;
+
+public class Copy {
+
+ public static Serializable deepCopy(ClassLoader cl, Serializable obj) throws IOException, ClassNotFoundException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(bos);
+ try {
+ oos.writeObject(obj);
+ } catch (Exception e) {
+ System.err.println("deepCopy write failed: " + e);
+ } finally {
+ oos.close();
+ }
+
+ ClObjectInputStream ios = new ClObjectInputStream(cl, new ByteArrayInputStream(bos.toByteArray()));
+ return (Serializable)ios.readObject();
+ }
+
+ /* ObjectInputStream with xlet class loader */
+ private static class ClObjectInputStream extends ObjectInputStream {
+ private ClassLoader classLoader = null;
+
+ public ClObjectInputStream(ClassLoader cl, InputStream in) throws IOException {
+ super(in);
+ classLoader = cl;
+ }
+
+ protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
+ try {
+ return Class.forName(desc.getName(), false, classLoader);
+ } catch (ClassNotFoundException e) {
+ Class cl = super.resolveClass(desc);
+ if (cl != null) {
+ return cl;
+ }
+ System.err.println("deepCopy: failed to resolve class " + desc.getName());
+ throw e;
+ } catch (Throwable t) {
+ System.err.println("deepCopy: failed to resolve class " + desc.getName() + ": " + t);
+ return null;
+ }
+ }
+ }
+}
\ No newline at end of file
More information about the libbluray-devel
mailing list