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

File LineWrapperOutputStream.java

 

Coverage histogram

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

Code metrics

12
23
3
1
95
51
9
0.39
7.67
3
3

Classes

Class Line # Actions
LineWrapperOutputStream 32 23 0% 9 5
0.868421186.8%
 

Contributing tests

This file is covered by 10 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;
21   
22    import java.io.FilterOutputStream;
23    import java.io.IOException;
24    import java.io.OutputStream;
25   
26    /**
27    * Filter output stream that wraps line after a fixed number of bytes.
28    *
29    * @version $Id: 23ad749c116f120eb8edaae13da4a292fa792071 $
30    * @since 5.4M1
31    */
 
32    public class LineWrapperOutputStream extends FilterOutputStream
33    {
34    /** New line bytes used to wrap lines. */
35    private static final byte[] NEWLINE = System.getProperty("line.separator", "\n").getBytes();
36   
37    /** A one byte buffer to optimize one byte writing. */
38    private byte[] oneByte = new byte[1];
39   
40    /** The count of bytes at which wrapping should occur. */
41    private final int wrapAt;
42   
43    /** The count of bytes on the last written line. */
44    private int count;
45   
46    /**
47    * Constructs a LineWrapperOutputStream from an OutputStream and a line size.
48    *
49    * @param outputStream the output stream.
50    * @param wrapAt the count of bytes at which wrapping should occur.
51    */
 
52  13 toggle public LineWrapperOutputStream(OutputStream outputStream, int wrapAt)
53    {
54  13 super(outputStream);
55  13 this.wrapAt = wrapAt;
56  13 this.count = 0;
57    }
58   
 
59  2266 toggle @Override
60    public void write(byte[] bytes, int offset, int length) throws IOException
61    {
62  2266 if ((offset | length | (bytes.length - (length + offset)) | (offset + length)) < 0) {
63  0 throw new IndexOutOfBoundsException();
64    }
65   
66  2266 if (length > 0) {
67  2266 if (this.count + length >= this.wrapAt) {
68  37 int off = offset;
69  37 int len = length;
70  37 int curlen = this.wrapAt - this.count;
71  37 do {
72  37 this.out.write(bytes, off, curlen);
73  37 this.out.write(NEWLINE, 0, NEWLINE.length);
74  37 off += curlen;
75  37 len -= curlen;
76  37 curlen = (len > this.wrapAt) ? this.wrapAt : len;
77  37 } while (curlen == this.wrapAt);
78  37 if (curlen > 0) {
79  2 this.out.write(bytes, off, curlen);
80    }
81  37 this.count = curlen;
82    } else {
83  2229 this.count += length;
84  2229 this.out.write(bytes, offset, length);
85    }
86    }
87    }
88   
 
89  2257 toggle @Override
90    public void write(int i) throws IOException
91    {
92  2257 this.oneByte[0] = (byte) i;
93  2257 write(this.oneByte, 0, 1);
94    }
95    }