1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.encoder

File AbstractBinaryStringEncoder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
19
8
1
103
65
8
0.42
2.38
8
1

Classes

Class Line # Actions
AbstractBinaryStringEncoder 38 19 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 24 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.crypto.internal.encoder;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.io.FilterInputStream;
24    import java.io.FilterOutputStream;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.io.OutputStream;
28   
29    import org.xwiki.crypto.BinaryStringEncoder;
30    import org.xwiki.crypto.internal.LineWrapperOutputStream;
31   
32    /**
33    * Abstract base class for encoder based on Bouncy Castle encoders.
34    *
35    * @version $Id: 912d62bca739b748a16c2617e0f20dd78d7ef0b1 $
36    * @since 5.4M1
37    */
 
38    public abstract class AbstractBinaryStringEncoder implements BinaryStringEncoder
39    {
40    /** Charset used for String <-> byte[] conversion. */
41    private static final String CHARSET = "UTF-8";
42   
43    abstract InternalBinaryStringEncoder getEncoder();
44   
 
45  3 toggle @Override
46    public String encode(byte[] input) throws IOException
47    {
48  3 return encode(input, 0, input.length);
49    }
50   
 
51  3 toggle @Override
52    public String encode(byte[] input, int wrapAt) throws IOException
53    {
54  3 return encode(input, 0, input.length, wrapAt);
55    }
56   
 
57  6 toggle @Override
58    public String encode(byte[] input, int off, int len) throws IOException
59    {
60  6 ByteArrayOutputStream baos = new ByteArrayOutputStream();
61  6 getEncoder().encode(input, off, len, baos);
62  6 baos.close();
63  6 return baos.toString(CHARSET);
64    }
65   
 
66  6 toggle @Override
67    public String encode(byte[] input, int off, int len, int wrapAt) throws IOException
68    {
69  6 ByteArrayOutputStream baos = new ByteArrayOutputStream();
70  6 LineWrapperOutputStream lwos = new LineWrapperOutputStream(baos, wrapAt);
71  6 getEncoder().encode(input, off, len, lwos);
72  6 lwos.close();
73  6 return baos.toString(CHARSET);
74    }
75   
 
76  100 toggle @Override
77    public byte[] decode(String input) throws IOException
78    {
79  100 ByteArrayOutputStream baos = new ByteArrayOutputStream();
80  100 byte[] inBytes = input.getBytes();
81  100 getEncoder().decode(inBytes, 0, inBytes.length, baos);
82  100 baos.close();
83  100 return baos.toByteArray();
84    }
85   
 
86  12 toggle @Override
87    public FilterInputStream getDecoderInputStream(InputStream is)
88    {
89  12 return new BcBinaryStringEncoderInputStream(is, getEncoder());
90    }
91   
 
92  3 toggle @Override
93    public FilterOutputStream getEncoderOutputStream(OutputStream os)
94    {
95  3 return new BcBinaryStringEncoderOutputStream(os, getEncoder());
96    }
97   
 
98  3 toggle @Override
99    public FilterOutputStream getEncoderOutputStream(OutputStream os, int wrapAt)
100    {
101  3 return new BcBinaryStringEncoderOutputStream(new LineWrapperOutputStream(os, wrapAt), getEncoder());
102    }
103    }