Coverage Report - org.mule.config.spring.util.CachedResource
 
Classes in this File Line Coverage Branch Coverage Complexity
CachedResource
0%
0/12
0%
0/2
1
 
 1  
 /*
 2  
  * $Id: CachedResource.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.config.spring.util;
 12  
 
 13  
 import org.mule.util.IOUtils;
 14  
 
 15  
 import java.io.ByteArrayInputStream;
 16  
 import java.io.IOException;
 17  
 import java.io.InputStream;
 18  
 import java.io.Reader;
 19  
 import java.io.UnsupportedEncodingException;
 20  
 
 21  
 import org.springframework.core.io.AbstractResource;
 22  
 
 23  
 /**
 24  
  * Spring 2.x is picky about open/closed input streams, as it requires a closed
 25  
  * stream (fully read resource) to enable automatic validation detection (DTD or
 26  
  * XSD). Otherwise, a caller has to specify the mode explicitly. <p/> Code relying on
 27  
  * Spring 1.2.x behavior may now break with
 28  
  * {@link org.springframework.beans.factory.BeanDefinitionStoreException}. This
 29  
  * class is called in to remedy this and should be used instead of, e.g.
 30  
  * {@link org.springframework.core.io.InputStreamResource}. <p/> The resource is
 31  
  * fully stored in memory.
 32  
  */
 33  
 public class CachedResource extends AbstractResource
 34  
 {
 35  
 
 36  
     private static final String DEFAULT_DESCRIPTION = "cached in-memory resource";
 37  
 
 38  
     private final byte[] buffer;
 39  
     private final String description;
 40  
 
 41  
     public CachedResource(byte[] source)
 42  
     {
 43  0
         this(source, null);
 44  0
     }
 45  
 
 46  
     public CachedResource(String source, String encoding) throws UnsupportedEncodingException
 47  
     {
 48  0
         this(source.trim().getBytes(encoding), DEFAULT_DESCRIPTION);
 49  0
     }
 50  
 
 51  
     public CachedResource(byte[] source, String description)
 52  0
     {
 53  0
         this.buffer = source;
 54  0
         this.description = description;
 55  0
     }
 56  
 
 57  
     public CachedResource(Reader reader, String encoding) throws IOException
 58  
     {
 59  0
         this(IOUtils.toByteArray(reader, encoding), DEFAULT_DESCRIPTION);
 60  0
     }
 61  
 
 62  
     public String getDescription()
 63  
     {
 64  0
         return (description == null) ? "" : description;
 65  
     }
 66  
 
 67  
     public InputStream getInputStream() throws IOException
 68  
     {
 69  
         // This HAS to be a new InputStream, otherwise SAX
 70  
         // parser breaks with 'Premature end of file at line -1"
 71  
         // This behavior is not observed with Spring pre-2.x
 72  0
         return new ByteArrayInputStream(buffer);
 73  
     }
 74  
 }