Coverage Report - org.apache.tapestry.util.io.SerializableAdaptor
 
Classes in this File Line Coverage Branch Coverage Complexity
SerializableAdaptor
0%
0/32
0%
0/10
2.8
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util.io;
 16  
 
 17  
 import org.apache.commons.codec.binary.Base64;
 18  
 import org.apache.hivemind.ApplicationRuntimeException;
 19  
 import org.apache.hivemind.ClassResolver;
 20  
 import org.apache.tapestry.services.DataSqueezer;
 21  
 
 22  
 import java.io.*;
 23  
 import java.util.zip.GZIPInputStream;
 24  
 import java.util.zip.GZIPOutputStream;
 25  
 
 26  
 /**
 27  
  * The most complicated of the adaptors, this one takes an arbitrary serializable object, serializes
 28  
  * it to binary (possibly compressing the stream along the way), and encodes it in a Base64
 29  
  * encoding. The first character of the squeezed stream indicates whether it is or is not encoded.
 30  
  * 
 31  
  * @author Howard Lewis Ship
 32  
  */
 33  
 
 34  0
 public class SerializableAdaptor implements SqueezeAdaptor
 35  
 {
 36  
     private static final char BYTESTREAM_PREFIX = 'O';
 37  
 
 38  
     private static final char GZIP_BYTESTREAM_PREFIX = 'Z';
 39  
 
 40  
     // O is for an object stream rendered as MIME
 41  
     // Z is for on object stream, compressed, rendered as MIME
 42  
 
 43  
     private static final String PREFIX = "OZ";
 44  
 
 45  
     private ClassResolver _resolver;
 46  
     
 47  
     public String getPrefix()
 48  
     {
 49  0
         return PREFIX;
 50  
     }
 51  
 
 52  
     public Class getDataClass()
 53  
     {
 54  0
         return Serializable.class;
 55  
     }
 56  
 
 57  
     public String squeeze(DataSqueezer squeezer, Object data)
 58  
     {
 59  
         try
 60  
         {
 61  0
             ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
 62  0
             ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
 63  
 
 64  0
             GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
 65  
 
 66  0
             TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
 67  
 
 68  0
             ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
 69  
 
 70  0
             oos.writeObject(data);
 71  
 
 72  0
             oos.close();
 73  
 
 74  0
             boolean useCompressed = bosCompressed.size() < bosPlain.size();
 75  
 
 76  0
             byte[] byteArray = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
 77  
 
 78  0
             byte[] encoded = Base64.encodeBase64(byteArray);
 79  
 
 80  0
             String prefix = Character.toString(useCompressed ? GZIP_BYTESTREAM_PREFIX  : BYTESTREAM_PREFIX);
 81  
 
 82  0
             return prefix + new String(encoded);
 83  
         }
 84  0
         catch (Exception ex)
 85  
         {
 86  0
             throw new ApplicationRuntimeException(IoMessages.encodeFailure(data, ex), ex);
 87  
         }
 88  
     }
 89  
 
 90  
     public Object unsqueeze(DataSqueezer squeezer, String encoded)
 91  
     {
 92  0
         char prefix = encoded.charAt(0);
 93  
 
 94  
         try
 95  
         {
 96  
             // Strip off the prefix, feed that in as a MIME stream.
 97  
 
 98  0
             byte[] mimeData = encoded.substring(1).getBytes();
 99  
 
 100  0
             byte[] decoded = Base64.decodeBase64(mimeData);
 101  
 
 102  0
             InputStream is = new ByteArrayInputStream(decoded);
 103  
 
 104  0
             if (prefix == GZIP_BYTESTREAM_PREFIX)
 105  0
                 is = new GZIPInputStream(is);
 106  
 
 107  0
             is = new BufferedInputStream(is);
 108  
 
 109  0
             ObjectInputStream ois = new ResolvingObjectInputStream(_resolver, is);
 110  
 
 111  0
             Object result = ois.readObject();
 112  
 
 113  0
             ois.close();
 114  
 
 115  0
             return result;
 116  
         }
 117  0
         catch (Exception ex)
 118  
         {
 119  0
             throw new ApplicationRuntimeException(IoMessages.decodeFailure(ex), ex);
 120  
         }
 121  
     }
 122  
 
 123  
     public void setResolver(ClassResolver resolver)
 124  
     {
 125  0
         _resolver = resolver;
 126  0
     }
 127  
 
 128  
 }