Coverage Report - org.kuali.rice.kew.util.ByteArrayDataSource
 
Classes in this File Line Coverage Branch Coverage Complexity
ByteArrayDataSource
0%
0/27
0%
0/4
2
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.util;
 18  
 
 19  
 import java.io.BufferedOutputStream;
 20  
 import java.io.ByteArrayInputStream;
 21  
 import java.io.ByteArrayOutputStream;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.io.OutputStream;
 25  
 import java.io.UnsupportedEncodingException;
 26  
 
 27  
 import javax.activation.DataSource;
 28  
 
 29  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 30  
 
 31  
 
 32  
 /**
 33  
  * A simple DataSource for demonstration purposes. This class implements a
 34  
  * DataSource from: an InputStream a byte array a String.
 35  
  */
 36  
 public class ByteArrayDataSource implements DataSource {
 37  
 
 38  
         private byte[] data; // data
 39  
     private String type; // content-type
 40  
 
 41  
     /* Create a DataSource from an input stream */
 42  0
     public ByteArrayDataSource(InputStream is, String type) {
 43  0
         this.type = type;
 44  
         try {
 45  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 46  0
             OutputStream os = new BufferedOutputStream(baos);
 47  
             int byteData;
 48  0
             while ((byteData = is.read()) != -1) {
 49  0
                 os.write(byteData);
 50  
             }
 51  0
             os.flush();
 52  0
             data = baos.toByteArray();
 53  0
         } catch (IOException ioex) {
 54  0
                 throw new WorkflowRuntimeException(ioex);
 55  0
         }
 56  0
     }
 57  
 
 58  
     /* Create a DataSource from a byte array */
 59  0
     public ByteArrayDataSource(byte[] data, String type) {
 60  0
         this.data = data;
 61  0
         this.type = type;
 62  0
     }
 63  
 
 64  
     /* Create a DataSource from a String */
 65  0
     public ByteArrayDataSource(String data, String type) {
 66  
         try {
 67  
             // Assumption that the string contains only ASCII
 68  
             // characters! Otherwise just pass a charset into this
 69  
             // constructor and use it in getBytes()
 70  0
             this.data = data.getBytes("iso-8859-1");
 71  0
         } catch (UnsupportedEncodingException uex) {}
 72  0
         this.type = type;
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Return an InputStream for the data. Note - a new stream must be returned
 77  
      * each time.
 78  
      */
 79  
     public InputStream getInputStream() throws IOException {
 80  0
         if (data == null)
 81  0
             throw new IOException("no data");
 82  0
         return new ByteArrayInputStream(data);
 83  
     }
 84  
 
 85  
     public OutputStream getOutputStream() throws IOException {
 86  0
         throw new IOException("cannot do this");
 87  
     }
 88  
 
 89  
     public String getContentType() {
 90  0
         return type;
 91  
     }
 92  
 
 93  
     public String getName() {
 94  0
         return "dummy";
 95  
     }
 96  
 }