• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / sitemesh2 / 59

22 Mar 2026 02:30AM UTC coverage: 40.347%. Remained the same
59

push

github

hazendaz
[mvn] Update maven wrapper

698 of 1891 branches covered (36.91%)

Branch coverage included in aggregate %.

1555 of 3693 relevant lines covered (42.11%)

0.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/opensymphony/module/sitemesh/util/FastByteArrayOutputStream.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
/*
6
 * Title:        FastByteArrayOutputStream
7
 * Description:
8
 *
9
 * This software is published under the terms of the OpenSymphony Software
10
 * License version 1.1, of which a copy has been included with this
11
 * distribution in the LICENSE.txt file.
12
 */
13

14
package com.opensymphony.module.sitemesh.util;
15

16
import java.io.ByteArrayOutputStream;
17
import java.io.IOException;
18
import java.io.OutputStream;
19
import java.io.UnsupportedEncodingException;
20
import java.nio.charset.Charset;
21
import java.nio.charset.StandardCharsets;
22
import java.util.Iterator;
23
import java.util.LinkedList;
24

25
/**
26
 * A speedy implementation of ByteArrayOutputStream. It's not synchronized, and it does not copy buffers when it's
27
 * expanded. There's also no copying of the internal buffer if it's contents is extracted with the writeTo(stream)
28
 * method.
29
 *
30
 * @author Rickard �berg
31
 * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
32
 */
33
public class FastByteArrayOutputStream extends ByteArrayOutputStream {
34

35
    /** The Constant DEFAULT_BLOCK_SIZE. */
36
    private static final int DEFAULT_BLOCK_SIZE = 8192;
37

38
    /** Internal buffer. */
39
    private byte[] buffer;
40

41
    /** The buffers. */
42
    private LinkedList<byte[]> buffers;
43

44
    /** The index. */
45
    private int index;
46

47
    /** The size. */
48
    private int size;
49

50
    /** The block size. */
51
    private int blockSize;
52

53
    /**
54
     * Instantiates a new fast byte array output stream.
55
     */
56
    public FastByteArrayOutputStream() {
57
        this(DEFAULT_BLOCK_SIZE);
×
58
    }
×
59

60
    /**
61
     * Instantiates a new fast byte array output stream.
62
     *
63
     * @param aSize
64
     *            the a size
65
     */
66
    public FastByteArrayOutputStream(int aSize) {
×
67
        blockSize = aSize;
×
68
        buffer = new byte[blockSize];
×
69
    }
×
70

71
    @Override
72
    public void writeTo(OutputStream out) throws IOException {
73
        // check if we have a list of buffers
74
        if (buffers != null) {
×
75
            Iterator<byte[]> iterator = buffers.iterator();
×
76
            while (iterator.hasNext()) {
×
77
                byte[] bytes = (byte[]) iterator.next();
×
78
                out.write(bytes, 0, blockSize);
×
79
            }
×
80
        }
81

82
        // write the internal buffer directly
83
        out.write(buffer, 0, index);
×
84
    }
×
85

86
    @Override
87
    public int size() {
88
        return size + index;
×
89
    }
90

91
    @Override
92
    public byte[] toByteArray() {
93
        byte[] data = new byte[size()];
×
94

95
        // check if we have a list of buffers
96
        int pos = 0;
×
97
        if (buffers != null) {
×
98
            Iterator<byte[]> iterator = buffers.iterator();
×
99
            while (iterator.hasNext()) {
×
100
                byte[] bytes = (byte[]) iterator.next();
×
101
                System.arraycopy(bytes, 0, data, pos, blockSize);
×
102
                pos += blockSize;
×
103
            }
×
104
        }
105

106
        // write the internal buffer directly
107
        System.arraycopy(buffer, 0, data, pos, index);
×
108

109
        return data;
×
110
    }
111

112
    @Override
113
    public void write(int datum) {
114
        if (index == blockSize) {
×
115
            // Create new buffer and store current in linked list
116
            if (buffers == null) {
×
117
                buffers = new LinkedList<byte[]>();
×
118
            }
119

120
            buffers.addLast(buffer);
×
121

122
            buffer = new byte[blockSize];
×
123
            size += index;
×
124
            index = 0;
×
125
        }
126

127
        // store the byte
128
        buffer[index++] = (byte) datum;
×
129
    }
×
130

131
    @Override
132
    public void write(byte[] data, int offset, int length) {
133
        if (data == null) {
×
134
            throw new NullPointerException();
×
135
        }
136
        if (offset < 0 || offset + length > data.length || length < 0) {
×
137
            throw new IndexOutOfBoundsException();
×
138
        } else if (index + length >= blockSize) {
×
139
            // Write byte by byte
140
            // FIXME optimize this to use arraycopy's instead
141
            for (int i = 0; i < length; i++) {
×
142
                write(data[offset + i]);
×
143
            }
144
        } else {
145
            // copy in the subarray
146
            System.arraycopy(data, offset, buffer, index, length);
×
147
            index += length;
×
148
        }
149
    }
×
150

151
    @Override
152
    public synchronized void reset() {
153
        buffer = new byte[blockSize];
×
154
        buffers = null;
×
155
    }
×
156

157
    @Override
158
    public String toString(String enc) throws UnsupportedEncodingException {
159
        return new String(toByteArray(), Charset.forName(enc));
×
160
    }
161

162
    @Override
163
    public String toString() {
164
        return new String(toByteArray(), StandardCharsets.UTF_8);
×
165
    }
166

167
    @Override
168
    public void flush() throws IOException {
169
        // does nothing
170
    }
×
171

172
    @Override
173
    public void close() throws IOException {
174
        // does nothing
175
    }
×
176
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc