James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
commit | author | age
fe24a0 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.utils;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24 import java.io.PipedInputStream;
25 import java.io.PipedOutputStream;
26
27 public class DeepCopier {
28
29     /**
30      * Produce a deep copy of the given object. Serializes the entire object to
31      * a byte array in memory. Recommended for relatively small objects.
32      */
33     @SuppressWarnings("unchecked")
34     public static <T> T copy(T original) {
35         T o = null;
36         try {
37             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
38             ObjectOutputStream oos = new ObjectOutputStream(byteOut);
39             oos.writeObject(original);
40             ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
41             ObjectInputStream ois = new ObjectInputStream(byteIn);
42             try {
43                 o = (T) ois.readObject();
44             } catch (ClassNotFoundException cex) {
45                 // actually can not happen in this instance
46             }
47         } catch (IOException iox) {
48             // doesn't seem likely to happen as these streams are in memory
49             throw new RuntimeException(iox);
50         }
51         return o;
52     }
53
54     /**
55      * This conserves heap memory!!!!! Produce a deep copy of the given object.
56      * Serializes the object through a pipe between two threads. Recommended for
57      * very large objects. The current thread is used for serializing the
58      * original object in order to respect any synchronization the caller may
59      * have around it, and a new thread is used for deserializing the copy.
60      * 
61      */
62     public static <T> T copyParallel(T original) {
63         try {
64             PipedOutputStream outputStream = new PipedOutputStream();
65             PipedInputStream inputStream = new PipedInputStream(outputStream);
66             ObjectOutputStream ois = new ObjectOutputStream(outputStream);
67             Receiver<T> receiver = new Receiver<T>(inputStream);
68             try {
69                 ois.writeObject(original);
70             } finally {
71                 ois.close();
72             }
73             return receiver.getResult();
74         } catch (IOException iox) {
75             // doesn't seem likely to happen as these streams are in memory
76             throw new RuntimeException(iox);
77         }
78     }
79
80     private static class Receiver<T> extends Thread {
81
82         private final InputStream inputStream;
83         private volatile T result;
84         private volatile Throwable throwable;
85
86         public Receiver(InputStream inputStream) {
87             this.inputStream = inputStream;
88             start();
89         }
90
91         @SuppressWarnings("unchecked")
92         public void run() {
93
94             try {
95                 ObjectInputStream ois = new ObjectInputStream(inputStream);
96                 try {
97                     result = (T) ois.readObject();
98                     try {
99                         // Some serializers may write more than they actually
100                         // need to deserialize the object, but if we don't
101                         // read it all the PipedOutputStream will choke.
102                         while (inputStream.read() != -1) {
103                         }
104                     } catch (IOException e) {
105                         // The object has been successfully deserialized, so
106                         // ignore problems at this point (for example, the
107                         // serializer may have explicitly closed the inputStream
108                         // itself, causing this read to fail).
109                     }
110                 } finally {
111                     ois.close();
112                 }
113             } catch (Throwable t) {
114                 throwable = t;
115             }
116         }
117
118         public T getResult() throws IOException {
119             try {
120                 join();
121             } catch (InterruptedException e) {
122                 throw new RuntimeException("Unexpected InterruptedException", e);
123             }
124             // join() guarantees that all shared memory is synchronized between
125             // the two threads
126             if (throwable != null) {
127                 if (throwable instanceof ClassNotFoundException) {
128                     // actually can not happen in this instance
129                 }
130                 throw new RuntimeException(throwable);
131             }
132             return result;
133         }
134     }
135 }