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