Coverage Report - org.seasar.teeda.core.render.Base64EncodeConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
Base64EncodeConverter
100%
28/28
100%
4/4
2.333
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.render;
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.InputStream;
 21  
 import java.io.ObjectInputStream;
 22  
 import java.io.ObjectOutputStream;
 23  
 import java.util.zip.GZIPOutputStream;
 24  
 
 25  
 import javax.faces.internal.FacesConfigOptions;
 26  
 
 27  
 import org.seasar.framework.util.Base64Util;
 28  
 import org.seasar.framework.util.InputStreamUtil;
 29  
 import org.seasar.framework.util.OutputStreamUtil;
 30  
 import org.seasar.teeda.core.util.GZIPInputStreamUtil;
 31  
 import org.seasar.teeda.core.util.GZIPOutputStreamUtil;
 32  
 import org.seasar.teeda.core.util.ObjectInputStreamUtil;
 33  
 import org.seasar.teeda.core.util.ObjectOutputStreamUtil;
 34  
 
 35  
 /**
 36  
  * @author shot
 37  
  */
 38  6
 public class Base64EncodeConverter implements EncodeConverter {
 39  
 
 40  
     public String getAsEncodeString(Object target) {
 41  5
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
 42  5
         GZIPOutputStream gzipOut = null;
 43  5
         ObjectOutputStream oos = null;
 44  
         try {
 45  5
             if (isCompressRequested()) {
 46  1
                 gzipOut = GZIPOutputStreamUtil.getOutputStream(bos);
 47  1
                 oos = ObjectOutputStreamUtil.getOutputStream(gzipOut);
 48  1
                 ObjectOutputStreamUtil.writeObject(oos, target);
 49  1
                 GZIPOutputStreamUtil.finish(gzipOut);
 50  
             } else {
 51  4
                 oos = ObjectOutputStreamUtil.getOutputStream(bos);
 52  4
                 ObjectOutputStreamUtil.writeObject(oos, target);
 53  
             }
 54  5
             return Base64Util.encode(bos.toByteArray());
 55  
         } finally {
 56  5
             OutputStreamUtil.close(oos);
 57  5
             OutputStreamUtil.close(gzipOut);
 58  5
             OutputStreamUtil.close(bos);
 59  
         }
 60  
     }
 61  
 
 62  
     public Object getAsDecodeObject(String state) {
 63  5
         final byte[] bytes = Base64Util.decode(state);
 64  5
         final InputStream bis = new ByteArrayInputStream(bytes);
 65  5
         InputStream is = null;
 66  5
         ObjectInputStream ois = null;
 67  
         try {
 68  5
             if (isCompressRequested()) {
 69  1
                 is = GZIPInputStreamUtil.getInputStream(bis);
 70  1
                 ois = ObjectInputStreamUtil.getInputStream(is);
 71  
             } else {
 72  4
                 ois = ObjectInputStreamUtil.getInputStream(bis);
 73  
             }
 74  5
             return ObjectInputStreamUtil.readObject(ois);
 75  
         } finally {
 76  5
             InputStreamUtil.close(ois);
 77  5
             InputStreamUtil.close(is);
 78  5
             InputStreamUtil.close(bis);
 79  
         }
 80  
     }
 81  
 
 82  
     protected boolean isCompressRequested() {
 83  8
         return FacesConfigOptions.getCompressState();
 84  
     }
 85  
 
 86  
 }