Coverage Report - org.apache.tapestry.util.io.SerializableAdaptor
 
Classes in this File Line Coverage Branch Coverage Complexity
SerializableAdaptor
88% 
100% 
2.4
 
 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 java.io.BufferedInputStream;
 18  
 import java.io.BufferedOutputStream;
 19  
 import java.io.ByteArrayInputStream;
 20  
 import java.io.ByteArrayOutputStream;
 21  
 import java.io.InputStream;
 22  
 import java.io.ObjectInputStream;
 23  
 import java.io.ObjectOutputStream;
 24  
 import java.io.Serializable;
 25  
 import java.util.zip.GZIPInputStream;
 26  
 import java.util.zip.GZIPOutputStream;
 27  
 
 28  
 import org.apache.commons.codec.binary.Base64;
 29  
 import org.apache.hivemind.ApplicationRuntimeException;
 30  
 import org.apache.hivemind.ClassResolver;
 31  
 import org.apache.tapestry.services.DataSqueezer;
 32  
 
 33  
 /**
 34  
  * The most complicated of the adaptors, this one takes an arbitrary serializable object, serializes
 35  
  * it to binary (possibly compressing the stream along the way), and encodes it in a Base64
 36  
  * encoding. The first character of the squeezed stream indicates whether it is or is not encoded.
 37  
  * 
 38  
  * @author Howard Lewis Ship
 39  
  */
 40  
 
 41  11
 public class SerializableAdaptor implements SqueezeAdaptor
 42  
 {
 43  
     private static final char BYTESTREAM_PREFIX = 'O';
 44  
 
 45  
     private static final char GZIP_BYTESTREAM_PREFIX = 'Z';
 46  
 
 47  
     // O is for an object stream rendered as MIME
 48  
     // Z is for on object stream, compressed, rendered as MIME
 49  
 
 50  
     private static final String PREFIX = "OZ";
 51  
 
 52  
     private ClassResolver _resolver;
 53  
     
 54  
     public String getPrefix()
 55  
     {
 56  10
         return PREFIX;
 57  
     }
 58  
 
 59  
     public Class getDataClass()
 60  
     {
 61  11
         return Serializable.class;
 62  
     }
 63  
 
 64  
     public String squeeze(DataSqueezer squeezer, Object data)
 65  
     {
 66  
         try
 67  
         {
 68  4
             ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
 69  4
             ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
 70  
 
 71  4
             GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
 72  
 
 73  4
             TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
 74  
 
 75  4
             ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
 76  
 
 77  4
             oos.writeObject(data);
 78  
 
 79  4
             oos.close();
 80  
 
 81  4
             boolean useCompressed = bosCompressed.size() < bosPlain.size();
 82  
 
 83  4
             byte[] byteArray = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
 84  
 
 85  4
             byte[] encoded = Base64.encodeBase64(byteArray);
 86  
 
 87  4
             String prefix = Character.toString(useCompressed ? GZIP_BYTESTREAM_PREFIX
 88  
                     : BYTESTREAM_PREFIX);
 89  
 
 90  4
             return prefix + new String(encoded);
 91  
         }
 92  0
         catch (Exception ex)
 93  
         {
 94  0
             throw new ApplicationRuntimeException(IoMessages.encodeFailure(data, ex), ex);
 95  
         }
 96  
     }
 97  
 
 98  
     public Object unsqueeze(DataSqueezer squeezer, String encoded)
 99  
     {
 100  3
         char prefix = encoded.charAt(0);
 101  
 
 102  
         try
 103  
         {
 104  
             // Strip off the prefix, feed that in as a MIME stream.
 105  
 
 106  3
             byte[] mimeData = encoded.substring(1).getBytes();
 107  
 
 108  3
             byte[] decoded = Base64.decodeBase64(mimeData);
 109  
 
 110  3
             InputStream is = new ByteArrayInputStream(decoded);
 111  
 
 112  3
             if (prefix == GZIP_BYTESTREAM_PREFIX)
 113  1
                 is = new GZIPInputStream(is);
 114  
 
 115  3
             is = new BufferedInputStream(is);
 116  
 
 117  3
             ObjectInputStream ois = new ResolvingObjectInputStream(_resolver, is);
 118  
 
 119  3
             Object result = ois.readObject();
 120  
 
 121  3
             ois.close();
 122  
 
 123  3
             return result;
 124  
         }
 125  0
         catch (Exception ex)
 126  
         {
 127  0
             throw new ApplicationRuntimeException(IoMessages.decodeFailure(ex), ex);
 128  
         }
 129  
     }
 130  
 
 131  
     public void setResolver(ClassResolver resolver)
 132  
     {
 133  10
         _resolver = resolver;
 134  10
     }
 135  
 
 136  
 }