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

File BcBinaryStringEncoderOutputStream.java

 

Coverage histogram

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

Code metrics

18
37
5
1
137
77
14
0.38
7.4
5
2.8

Classes

Class Line # Actions
BcBinaryStringEncoderOutputStream 32 37 0% 14 7
0.883333388.3%
 

Contributing tests

This file is covered by 6 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.FilterOutputStream;
23    import java.io.IOException;
24    import java.io.OutputStream;
25   
26    /**
27    * An output stream wrapper that will encode binary data to string data in block.
28    *
29    * @version $Id: d3e7434f307e75028f7c3b9191226e854ff04725 $
30    * @since 5.4M1
31    */
 
32    class BcBinaryStringEncoderOutputStream extends FilterOutputStream
33    {
34    /** Number of bytes of binary data that should be encoded together. */
35    private final int blockSize;
36   
37    /** A one byte buffer to optimize one byte writing. */
38    private final byte[] oneByte = new byte[1];
39   
40    /** An overflow buffer to collate pending output data. */
41    private byte[] ofBuf;
42   
43    /** Current length in byte of valid data in the overflow buffer. */
44    private int ofLen;
45   
46    /** Encoder to process data. */
47    private final InternalBinaryStringEncoder encoder;
48   
49    /**
50    * Construct a filter stream for the given encoder, following the specifications.
51    *
52    * @param outputStream the underlying output stream that will receive encoded data.
53    * @param encoder the encoder used to encode data.
54    */
 
55  6 toggle BcBinaryStringEncoderOutputStream(OutputStream outputStream, InternalBinaryStringEncoder encoder)
56    {
57  6 super(outputStream);
58  6 this.encoder = encoder;
59  6 this.blockSize = encoder.getEncodingBlockSize();
60  6 this.ofBuf = new byte[this.blockSize];
61    }
62   
 
63  0 toggle @Override
64    public void write(int i) throws IOException
65    {
66  0 this.oneByte[0] = (byte) i;
67  0 write(this.oneByte, 0, 1);
68    }
69   
 
70  48 toggle @Override
71    public void write(byte[] input, int offset, int length) throws IOException
72    {
73  48 if ((offset | length | (input.length - (length + offset)) | (offset + length)) < 0) {
74  0 throw new IndexOutOfBoundsException();
75    }
76   
77  48 if (length == 0) {
78  0 return;
79    }
80   
81  48 int off = offset;
82  48 int len = length;
83   
84    // Is some pending data of a previous call available ?
85  48 if (this.ofLen > 0) {
86    // Complete the overflow buffer, trying to reach a full buffer.
87  20 int underflow = this.blockSize - this.ofLen;
88  20 if (underflow > len) {
89  8 underflow = len;
90    }
91  20 System.arraycopy(input, off, this.ofBuf, this.ofLen, underflow);
92  20 this.ofLen += underflow;
93  20 off += underflow;
94  20 len -= underflow;
95   
96    // Encode and output the overflow buffer if full.
97  20 if (this.ofLen == this.blockSize) {
98  12 this.encoder.encode(this.ofBuf, 0, this.blockSize, this.out);
99  12 this.ofLen = 0;
100    }
101    }
102   
103    // Still some data to write ?
104  48 if (len > 0) {
105   
106    // Store overflow in the overflow buffer
107  36 int overflow = len - ((len / this.blockSize) * this.blockSize);
108  36 if (overflow > 0) {
109  16 System.arraycopy(input, (off + len - overflow), this.ofBuf, 0, overflow);
110  16 this.ofLen += overflow;
111  16 len -= overflow;
112    }
113   
114    // If there is still some data to write, encode and write them
115  36 if (len > 0) {
116  28 this.encoder.encode(input, off, len, this.out);
117    }
118    }
119    }
120   
 
121  12 toggle @Override
122    public void flush() throws IOException
123    {
124  12 if (this.ofLen > 0) {
125  4 this.encoder.encode(this.ofBuf, 0, this.ofLen, this.out);
126  4 this.ofLen = 0;
127    }
128  12 this.out.flush();
129    }
130   
 
131  6 toggle @Override
132    public void close() throws IOException
133    {
134  6 flush();
135  6 super.close();
136    }
137    }