1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.filter.xml.internal.parameter

File InputStreamConverter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
13
3
1
79
47
7
0.54
4.33
3
2.33

Classes

Class Line # Actions
InputStreamConverter 42 13 0% 7 2
0.9090909490.9%
 

Contributing tests

This file is covered by 627 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.filter.xml.internal.parameter;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.IOException;
24    import java.io.InputStream;
25   
26    import org.apache.commons.lang3.ArrayUtils;
27   
28    import com.thoughtworks.xstream.converters.ConversionException;
29    import com.thoughtworks.xstream.converters.Converter;
30    import com.thoughtworks.xstream.converters.MarshallingContext;
31    import com.thoughtworks.xstream.converters.UnmarshallingContext;
32    import com.thoughtworks.xstream.core.util.Base64Encoder;
33    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
34    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
35   
36    /**
37    * Converts an {@link InputStream} to XML.
38    *
39    * @version $Id: 73933c22d8396292738183653aec1c3928c142c9 $
40    * @since 5.2RC1
41    */
 
42    public class InputStreamConverter implements Converter
43    {
44    private static final Base64Encoder BASE64 = new Base64Encoder();
45   
 
46  3571 toggle @Override
47    public boolean canConvert(Class type)
48    {
49  3571 return InputStream.class.isAssignableFrom(type);
50    }
51   
 
52  38 toggle @Override
53    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
54    {
55  38 InputStream inpuStream = (InputStream) source;
56  38 byte[] buffer = new byte[4096];
57  38 int readSize;
58  38 do {
59  446 try {
60  446 readSize = inpuStream.read(buffer);
61    } catch (IOException e) {
62  0 throw new ConversionException("Failed to read input stream", e);
63    }
64  446 if (readSize > 0) {
65  446 if (readSize == 4096) {
66  408 writer.setValue(BASE64.encode(buffer));
67    } else {
68  38 writer.setValue(BASE64.encode(ArrayUtils.subarray(buffer, 0, readSize)));
69    }
70    }
71  446 } while (readSize == 4096);
72    }
73   
 
74  14 toggle @Override
75    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
76    {
77  14 return new ByteArrayInputStream(BASE64.decode(reader.getValue()));
78    }
79    }