Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 136   Methods: 5
NCLOC: 77   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SerializableAdaptor.java 100% 93.3% 100% 95.1%
coverage coverage
 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    public class SerializableAdaptor implements SqueezeAdaptor
 42    {
 43    private ClassResolver _resolver;
 44   
 45    private static final char BYTESTREAM_PREFIX = 'O';
 46   
 47    private static final char GZIP_BYTESTREAM_PREFIX = 'Z';
 48   
 49    // O is for an object stream rendered as MIME
 50    // Z is for on object stream, compressed, rendered as MIME
 51   
 52    private static final String PREFIX = "OZ";
 53   
 54  78 public String getPrefix()
 55    {
 56  78 return PREFIX;
 57    }
 58   
 59  78 public Class getDataClass()
 60    {
 61  78 return Serializable.class;
 62    }
 63   
 64  36 public String squeeze(DataSqueezer squeezer, Object data)
 65    {
 66  36 try
 67    {
 68  36 ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
 69  36 ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
 70   
 71  36 GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
 72   
 73  36 TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
 74   
 75  36 ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
 76   
 77  36 oos.writeObject(data);
 78   
 79  36 oos.close();
 80   
 81  36 boolean useCompressed = bosCompressed.size() < bosPlain.size();
 82   
 83  36 byte[] byteArray = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
 84   
 85  36 byte[] encoded = Base64.encodeBase64(byteArray);
 86   
 87  36 String prefix = Character.toString(useCompressed ? GZIP_BYTESTREAM_PREFIX
 88    : BYTESTREAM_PREFIX);
 89   
 90  36 return prefix + new String(encoded);
 91    }
 92    catch (Exception ex)
 93    {
 94  0 throw new ApplicationRuntimeException(IoMessages.encodeFailure(data, ex), ex);
 95    }
 96    }
 97   
 98  12 public Object unsqueeze(DataSqueezer squeezer, String encoded)
 99    {
 100  12 char prefix = encoded.charAt(0);
 101   
 102  12 try
 103    {
 104    // Strip off the prefix, feed that in as a MIME stream.
 105   
 106  12 byte[] mimeData = encoded.substring(1).getBytes();
 107   
 108  12 byte[] decoded = Base64.decodeBase64(mimeData);
 109   
 110  12 InputStream is = new ByteArrayInputStream(decoded);
 111   
 112  12 if (prefix == GZIP_BYTESTREAM_PREFIX)
 113  2 is = new GZIPInputStream(is);
 114   
 115  12 is = new BufferedInputStream(is);
 116   
 117  12 ObjectInputStream ois = new ResolvingObjectInputStream(_resolver, is);
 118   
 119  12 Object result = ois.readObject();
 120   
 121  12 ois.close();
 122   
 123  12 return result;
 124    }
 125    catch (Exception ex)
 126    {
 127  0 throw new ApplicationRuntimeException(IoMessages.decodeFailure(ex), ex);
 128    }
 129    }
 130   
 131  78 public void setResolver(ClassResolver resolver)
 132    {
 133  78 _resolver = resolver;
 134    }
 135   
 136    }